要确保已经明白神经网络和卷积神经网络的原理.如果不明白,先学习参考资料1.tensorflow中有很多api,可以分成2大类.1类是比较低层的api(tf.train),叫TensorFlow Core.还有1种相对高层的api(tf.contrib.learn),是建立在TensorFlow Core基础上的,这种api码农用着很方便.

环境

python 3.5.3

tensorflow 1.0.0

Tensors

TensorFlow中的基本数据是tensor. tensor可以直观地理解为把numpy中的数组又包了一层.tensor的runk表示tensor是几维的.比如

[1. ,2., 3.] # runnk为1的tensor,它的shape是[3]
[[1., 2., 3.], [4., 5., 6.]] # runnk为2的tensor,它的shape是[2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # runnk为3的tensor,它的shape是[2, 1, 3]

helloworld级使用

使用tensorflow编程有2个步骤.第1是建立computational graph,第2是运行computational graph.computational graph中的每个结点都有0个或多个tensor作为输入.有一种结点本身是个常量,这种结点没有输入,有固定的输出(即它本身).下面是2个结点:

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # 默认类型就是tf.float32
print(node1, node2)

运行这个代码结果是

Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)

注意输出中没有具体的值3.0,4.0.这可以理解成建立computational graph.在通过Session运行computational graph的时候才会把值填到结点中.比如

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # 默认类型就是tf.float32
sess = tf.Session()
print(sess.run([node1, node2]))

稍微复杂一点的例子.

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # 默认类型就是tf.float32
node3 = tf.add(node1, node2)
print("node3: ", node3)
sess = tf.Session()
print("sess.run(node3): ",sess.run(node3))

placeholder

placeholder有什么作用?placeholder可以用来先定义一种操作,执行的时候再具体赋值.比如python函数的定义

def add(a, b)
return a + b

a,b都没有具体的值,调用的时候才赋值.不严谨但直观地可以把a,b理解为placeholder.下面看tensorflow的placeholder.

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b
print(sess.run(adder_node, {a: 3, b: 4.5})) # 输出为7.5
print(sess.run(adder_node, {a: [1,3], b: [2, 4]})) # 输出为[ 3. 7.]

再看一个例子

import tensorflow as tf
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b
add_and_triple = adder_node * 3
sess = tf.Session()
print(sess.run(add_and_triple, {a: 3, b: 4.5})) # 输出为22.5

Variable

可以简单地认为在训练的各个参数即为Variable.看下面的例子.

import tensorflow as tf
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(linear_model, {x: [1, 2, 3, 4]}))

输出为W * x + b的值.下面看怎么使用损失函数.

import tensorflow as tf

b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})) # 损失函数是23.66

修改w,b的值再看下损失函数.

import tensorflow as tf
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas) fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [1, 2, 3, 4], y:[0, -1, -2, -3]})) # 损失函数是0

训练方法

现在要解决如下问题:

已知向量x=(1, 2, 3, 4),向量y=(0, -1, -2, -3),w,b是标量.求w,b使y=wx+b

import tensorflow as tf

W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
loss = tf.reduce_sum(tf.square(linear_model - y))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})
# print(sess.run(loss, {x: x_train, y: y_train}))输出loss curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

上面代码用梯度下降方法求出w,b.现在来验证下wx+b和y相差多少.

import tensorflow as tf

W = tf.constant([-0.9999969], tf.float32)
b = tf.constant([0.99999082], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(linear_model, {x: [1, 2, 3, 4]}))

输出为

[ -6.07967377e-06  -1.00000298e+00  -1.99999988e+00  -2.99999666e+00]

已经相当接近(0, -1, -2, -3).

上面是用TensorFlow Core中的方法训练.也可以用较高层的api(tf.contrib.learn). 因为这种方法过于抽象,反而会分散初学都注意力.以后再补上.

问题

  • 用较高层的api(tf.contrib.learn)训练

参考资料

1 http://cs231n.github.io/

2 tensorflow官方教程

初次接触tensorflow的更多相关文章

  1. tensorflow初次接触记录,我用python写的tensorflow第一个模型

    tensorflow初次接触记录,我用python写的tensorflow第一个模型 刚用python写的tensorflow机器学习代码,训练60000张手写文字图片,多层神经网络学习拟合17000 ...

  2. 初次接触json...

    这两天发现很多网站显示图片版块都用了瀑布流模式布局的:随着页面滚动条向下滚动,这种布局还会不断加载数据并附加至当前尾部.身为一个菜鸟级的程序员,而且以后可能会经常与网站打交道,我觉得我还是很有必要去尝 ...

  3. 初次接触GWT,知识点总括

    初次接触GWT,知识点概括 前言 本人最近开始研究 GWT(Google Web Toolkit) ,现将个人的一点心得贴出来,希望对刚开始接触 GWT的程序员们有所帮助,也欢迎讨论,共同进步. 先说 ...

  4. [Docker]初次接触

    Docker 初次接触 近期看了不少docker介绍性文章,也听了不少公开课,于是今天去官网逛了逛,发现了一个交互式的小教程于是决定跟着学习下. 仅仅是把认为重点的知识记录下来,不是非常系统的学习和笔 ...

  5. 初次接触:DirectDraw

    第六章 初次接触:DirectDraw 本章,你将初次接触DirectX中最重要的组件:DirectDraw.DirectDraw可能是DirectX中最强大的技术,因为其贯穿着2D图形绘制同时其帧缓 ...

  6. 初次接触scrapy框架

    初次接触这个框架,先订个小目标,抓取QQ首页,然后存入记事本. 安装框架(http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/install.html) ...

  7. javaweb中的乱码问题(初次接触时写)

    javaweb中的乱码问题 在初次接触javaweb中就遇到了乱码问题,下面是我遇到这些问题的解决办法 1. 页面乱码(jsp) 1. 在页面最前方加上 <%@ page language=&q ...

  8. 初次接触Java

    今天初次接触Eclipse,学着用他来建立java工程,话不多说,来看看今天的成果! 熟悉自己手中的开发工具,热热身 刚上手别慌,有问题找度娘 刚刚拿到这个软件的安装包我是一脸懵逼的,因为是从官网下载 ...

  9. -1.记libgdx初次接触

    学习一门技术最难的是开发环境变量配置和工具配置,以下为我初次接触libgdx时遇到的问题 几个难点记录下 gradle 直接用下到本地,然后放到d盘,链接到就行(gradle-wrapper.prop ...

随机推荐

  1. Mybatis动态语句部分收集

    where: <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BL ...

  2. nyoj 35-表达式求值(stack, 栈的应用)

    35-表达式求值 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:37 submit:53 题目描述: ACM队的mdd想做一个计算器,但是,他要做的 ...

  3. nyoj 1071-不可以!(a*b + fabs(a*b))

    1071-不可以! 内存限制:64MB 时间限制:1000ms 特判: No 通过数:10 提交数:18 难度:1 题目描述: 判断:两个数x.y的正负性. 要求:不可以使用比较运算符,即" ...

  4. nyoj 266-字符串逆序输出 (isdigit(), geline(cin, my_string))

    266-字符串逆序输出 内存限制:64MB 时间限制:3000ms 特判: No 通过数:15 提交数:18 难度:0 题目描述: 给定一行字符,逆序输出此行(空格.数字不输出) 输入描述: 第一行是 ...

  5. PhpStudy BackDoor2019 深度分析

    笔者<Qftm>原文发布<合天>:https://mp.weixin.qq.com/s?__biz=MjM5MTYxNjQxOA==&mid=2652852661&am ...

  6. linux命令指令

    1.ls显示目录内容 -a 显示目录下所有文件 -l 显示详细信息 ls *.c  列出当前目录所有的.c文件 2.uname -a  查看内核版本 3.whoami  查看谁登陆虚拟机 4.cd 切 ...

  7. python_day03

    今日所学内容 1.函数部分: #函数的三种定义方式#1.无参函数:不需要外部传入的参数#2.有参函数:需要接受外部传入的参数#3.空函数:def func(): pass#pass代表说明都不用做# ...

  8. linux 如何把一个装好的系统做成镜像(文件备份)

    linux 如何把一个装好的系统做成镜像(文件备份)  我来答 浏览 11851 次来自电脑网络类芝麻团 2016-01-19 案例1(命令式操作) 像'ghost'那些备份系统,系统出了问题就恢复 ...

  9. SCAU-1078 破密-数学的应用

        另外一种方法和该题的题目在这位大佬的博客里 SCAU 1078 破密 还可以参考 https://blog.csdn.net/sinat_34200786/article/details/78 ...

  10. 利用tomcat搭建图片服务器

    今天来教大家如何使用 tomcat 来搭建一个图片的服务器 1.先将tomcat解压一份并改名 2.此时apache-tomcat-8.5.43-windows-x64-file为图片服务器 依次打开 ...