TensorFlow入门(一)基本用法
2017-03-06 11:47
435 查看
TensorFlow入门(一)基本用法
原文链接:http://blog.csdn.net/jerr__y/article/details/52955148refer to: http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/basic_usage.html
@author: huangyongye
@date: 2017-02-25
本例子主要是按照 tensorflow的中文文档来学习 tensorflow 的基本用法。按照文档说明,主要存在的一些问题:
1.就是 Session() 和 InteractiveSession() 的用法。后者用 Tensor.eval() 和 Operation.run() 来替代了 Session.run(). 其中更多的是用 Tensor.eval(),所有的表达式都可以看作是 Tensor.
2.另外,tf的表达式中所有的变量或者是常量都应该是 tf 的类型。
3.只要是声明了变量,就得用 sess.run(tf.global_variables_initializer()) 或者 x.initializer.run() 方法来初始化才能用。
例一:平面拟合
通过本例可以看到机器学习的一个通用过程:1.准备数据-> 2.构造模型(设置求解目标函数) -> 3.求解模型
import tensorflow as tf import numpy as np # 1.准备数据:使用 NumPy 生成假数据(phony data), 总共 100 个点. x_data = np.float32(np.random.rand(2, 100)) # 随机输入 y_data = np.dot([0.100, 0.200], x_data) + 0.300 # 2.构造一个线性模型 b = tf.Variable(tf.zeros([1])) W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0)) y = tf.matmul(W, x_data) + b # 3.求解模型 # 设置损失函数:误差的均方差 loss = tf.reduce_mean(tf.square(y - y_data)) # 选择梯度下降的方法 optimizer = tf.train.GradientDescentOptimizer(0.5) # 迭代的目标:最小化损失函数 train = optimizer.minimize(loss) ############################################################ # 以下是用 tf 来解决上面的任务 # 1.初始化变量:tf 的必备步骤,主要声明了变量,就必须初始化才能用 init = tf.global_variables_initializer() # 设置tensorflow对GPU的使用按需分配 config = tf.ConfigProto() config.gpu_options.allow_growth = True # 2.启动图 (graph) sess = tf.Session(config=config) sess.run(init) # 3.迭代,反复执行上面的最小化损失函数这一操作(train op),拟合平面 for step in xrange(0, 201): sess.run(train) if step % 20 == 0: print step, sess.run(W), sess.run(b) # 得到最佳拟合结果 W: [[0.100 0.200]], b: [0.300]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4000
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
0 [[ 0.27467242 0.81889796]] [-0.13746099] 20 [[ 0.1619305 0.39317462]] [ 0.18206716] 40 [[ 0.11901411 0.25831661]] [ 0.2642329] 60 [[ 0.10580806 0.21761954]] [ 0.28916073] 80 [[ 0.10176832 0.20532639]] [ 0.29671678] 100 [[ 0.10053726 0.20161074]] [ 0.29900584] 120 [[ 0.100163 0.20048723]] [ 0.29969904] 140 [[ 0.10004941 0.20014738]] [ 0.29990891] 160 [[ 0.10001497 0.20004457]] [ 0.29997244] 180 [[ 0.10000452 0.20001349]] [ 0.29999167] 200 [[ 0.10000138 0.2000041 ]] [ 0.29999748]
例二:两个数求和
input1 = tf.constant(2.0) input2 = tf.constant(3.0) input3 = tf.constant(5.0) intermd = tf.add(input1, input2) mul = tf.multiply(input2, input3) with tf.Session() as sess: result = sess.run([mul, intermd]) # 一次执行多个op print result print type(result) print type(result[0])1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
[15.0, 5.0] <type 'list'> <type 'numpy.float32'>
1.变量,常量
1.1 用 tensorflow 实现计数器,主要是设计了 在循环中调用加法实现计数
# 创建变量,初始化为0 state = tf.Variable(0, name="counter") # 创建一个 op , 其作用是时 state 增加 1 one = tf.constant(1) # 直接用 1 也就行了 new_value = tf.add(state, 1) update = tf.assign(state, new_value) # 启动图之后, 运行 update op with tf.Session() as sess: # 创建好图之后,变量必须经过‘初始化’ sess.run(tf.global_variables_initializer()) # 查看state的初始化值 print sess.run(state) for _ in range(3): sess.run(update) # 这样子每一次运行state 都还是1 print sess.run(state)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0 1 2 3
1.2 用 tf 来实现对一组数求和,再计算平均
h_sum = tf.Variable(0.0, dtype=tf.float32) # h_vec = tf.random_normal(shape=([10])) h_vec = tf.constant([1.0,2.0,3.0,4.0]) # 把 h_vec 的每个元素加到 h_sum 中,然后再除以 10 来计算平均值 # 待添加的数 h_add = tf.placeholder(tf.float32) # 添加之后的值 h_new = tf.add(h_sum, h_add) # 更新 h_new 的 op update = tf.assign(h_sum, h_new) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # 查看原始值 print 's_sum =', sess.run(h_sum) print "vec = ", sess.run(h_vec) # 循环添加 for _ in range(4): sess.run(update, feed_dict={h_add: sess.run(h_vec[_])}) print 'h_sum =', sess.run(h_sum) # print 'the mean is ', sess.run(sess.run(h_sum) / 4) # 这样写 4 是错误的, 必须转为 tf 变量或者常量 print 'the mean is ', sess.run(sess.run(h_sum) / tf.constant(4.0))1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
s_sum = 0.0 vec = [ 1. 2. 3. 4.] h_sum = 1.0 h_sum = 3.0 h_sum = 6.0 h_sum = 10.0 the mean is 2.5
2. InteractiveSession() 的用法
InteractiveSession() 主要是避免 Session(会话)被一个变量持有a = tf.constant(1.0) b = tf.constant(2.0) c = a + b # 下面的两种情况是等价的 with tf.Session(): print c.eval() sess = tf.InteractiveSession() print c.eval() sess.close()1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
3.0 3.0
a = tf.constant(1.0) b = tf.constant(2.0) c = tf.Variable(3.0) d = a + b sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) ################### # 这样写是错误的 # print a.run() # print d.run() #################### # 这样才是正确的 print a.eval() print d.eval() # run() 方法主要用来 x = tf.Variable(1.2) # print x.eval() # 还没初始化,不能用 x.initializer.run() # x.initializer 就是一个初始化的 op, op才调用run() 方法 print x.eval() sess.close()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1.0 3.0 1.2
2.1 怎样使用 tf.InteractiveSession() 来完成上面 1.2 中 求和 、平均 的操作呢?
h_sum = tf.Variable(0.0, dtype=tf.float32) # h_vec = tf.random_normal(shape=([10])) h_vec = tf.constant([1.0,2.0,3.0,4.0]) # 把 h_vec 的每个元素加到 h_sum 中,然后再除以 10 来计算平均值 # 待添加的数 h_add = tf.placeholder(tf.float32) # 添加之后的值 h_new = tf.add(h_sum, h_add) # 更新 h_new 的 op update = tf.assign(h_sum, h_new) sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) print 's_sum =', h_sum.eval() print "vec = ", h_vec.eval() print "vec = ", h_vec[0].eval() for _ in range(4): update.eval(feed_dict={h_add: h_vec[_].eval()}) print 'h_sum =', h_sum.eval() sess.close()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
s_sum = 0.0 vec = [ 1. 2. 3. 4.] vec = 1.0 h_sum = 1.0 h_sum = 3.0 h_sum = 6.0 h_sum = 10.0
3.使用feed来对变量赋值
这些需要用到feed来赋值的操作可以通过tf.placeholder()说明,以创建占位符。下面的例子中可以看出 session.run([output], …) 和 session.run(output, …) 的区别。前者输出了 output 的类型等详细信息,后者只输出简单结果。
input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.multiply(input1, input2) with tf.Session() as sess: print sess.run([output], feed_dict={input1:[7.0], input2:[2.0]})1
2
3
4
5
6
1
2
3
4
5
6
[array([ 14.], dtype=float32)]
with tf.Session() as sess: result = sess.run(output, feed_dict={input1:[7.0], input2:[2.0]}) print type(result) print result1
2
3
4
1
2
3
4
<type 'numpy.ndarray'> [ 14.]
with tf.Session() as sess: result = sess.run(output, feed_dict={input1:7.0, input2:2.0}) print type(result) print result1
2
3
4
1
2
3
4
<type 'numpy.float32'> 14.0
with tf.Session() as sess: print sess.run([output], feed_dict={input1:[7.0, 3.0], input2:[2.0, 1.0]})1
2
1
2
[array([ 14., 3.], dtype=float32)]
with tf.Session() as sess: print sess.run(output, feed_dict={input1:[7.0, 3.0], input2:[2.0, 1.0]})1
2
1
2
[ 14. 3.]
顶
0
相关文章推荐
- TensorFlow入门(一)基本用法
- tensorflow入门1 基本用法和最近邻算法
- TensorFlow入门(二)——基本用法
- Linq入门演练---(1)基本用法-分组,排序,内连接
- jquery.validate.js的基本用法入门
- jquery.validate.js的基本用法入门
- jquery.validate.js的基本用法入门
- [转载]jquery.validate.js的基本用法入门
- Oracle入门:Order By 和Group By 的基本用法
- jquery validate.js表单验证的基本用法入门
- jquery validate.js表单验证的基本用法入门
- Silverlight新手入门教程(主要介绍一些效果的基本实现和用法)1
- [转载]jquery.validate.js的基本用法入门
- [转载]jquery.validate.js的基本用法入门
- jquery validate.js表单验证的基本用法入门
- jquery.validate.js的基本用法入门
- [转载] jQuery.Validate.js的基本用法入门
- json入门及基本用法
- jquery.validate.js的基本用法入门
- Mac入门(一)基本用法