1.概念
graph:图,表示具体的计算任务
session:会话,图需要在会话中执行,一个会话可以包含很多图
tensor:张量,在此表示数据,类型是numpy::ndarray
variable:就是本意变量,图的重要组成部分
operation:简称op,是图中计算的节点,输入tensor计算后产生tensor
feed、fetch:意思是给图添加数据和获取图中的数据,因为训练过程中有些数据需要动态获得、临时给予数据
运行:
考虑到python运算的性能,肯定需要使用外部运算库,但是内外环境切换也是个很大的开销,TF如同其他主流机器学习工具,把程序通常组织成一个构建阶段和一个执行阶段。构建就是说明需要一个怎样的网络模型,执行就是按照指定的优化训练模型,也包含检验输出等操作。可以看做先用python程序搭建模型,然后全部在python之外运行。
2. 例子
2.1 平面拟合
需要拟合的平面:y = W1 * x1_data + W2*x2_data + b,其中,已知x1_data、x2_data和y,但是都包含一点噪声。
程序:
import tensorflow as tf
import numpy as np
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x1_data = np.random.rand(100).astype(np.float32)
x2_data = np.random.rand(100).astype(np.float32)
y_data = x1_data * 10 + x2_data * 5 + 3 + tf.random_uniform([100], -0.1, 0.1)
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
# note: W b and y just statement/container before initialization
W1 = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
W2 = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W1 * x1_data + W2*x2_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.AdagradOptimizer(0.6)
train = optimizer.minimize(loss)
# Before starting, initialize the variables. We will 'run' this first.
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in range(20001):
sess.run(train)
#if step % 20 == 0:
#print(step, sess.run(W), sess.run(b),sess.run(loss))
print(step, sess.run(W1), sess.run(W2), sess.run(b),sess.run(loss))
# Learns best fit is W: [0.1], b: [0.3]
2.2 矩阵相乘
两个比较大的矩阵相乘,分别使用GPU和CPU,比较运行时间
import tensorflow as tf
import numpy as np
#when put here the "cpu" is same as "gpu" , because it has been deploied on gpu or cpu
#select the fastest device automatically
#matrix1 = np.random.rand(20000,1500).astype(np.float32)
#matrix2 = np.random.rand(1500,20000).astype(np.float32)
#product = tf.matmul(matrix1, matrix2)
with tf.Session() as sess3:
with tf.device("/gpu:0"):#gpu 11.6s and cpu 20.2s
matrix1 = np.random.rand(20000,1500).astype(np.float32)
matrix2 = np.random.rand(1500,20000).astype(np.float32)
product = tf.matmul(matrix1, matrix2)
result = sess3.run(product)
如果有GPU并且安装的是GPU版本的TF,程序默认是在GPU上运行的。通过指定"/gpu:0"或"/cpu:0"的形式,可以人为改变。GPU运行时间是11.6s,CPU是20.2s,节省了一些时间。
2.3 构建session的另一种方式
以上程序需要显示地使用sess.run(...)运行节点,想法自然,但TF也提供了另一种形式
import tensorflow as tf
import numpy as np
#deploy a session
sess = tf.InteractiveSession()
#design the grape
matrix1 = np.random.rand(2000,1500).astype(np.float32)
matrix2 = np.random.rand(1500,2000).astype(np.float32)
product = tf.matmul(matrix1, matrix2)
#run the operation
print product.eval()
sess.close()
2.4一个计数器--说明构建阶段和运行阶段
TF把很多操作都规定成内部的函数,先显式地规定网络,然后才是运行
import tensorflow as tf
#design the graph
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
#initialization
init_op = tf.initialize_all_variables()
#run
with tf.Session() as sess:
sess.run(init_op)
print sess.run(state)
for _ in range(3):
sess.run(update)
print sess.run(state)
2.5获取数据fetch()
程序运行时需要获取一些数据,每个节点获取的数据可以理解为:对每个点单独有通路,从底部运行过来(实际不是这样,但数据同步行类似)
import tensorflow as tf
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)
with tf.Session() as sess:
result = sess.run([mul, intermed])
print result
result = sess.run([intermed])
print result
2.6填充数据feed()
随时给程序填充一些数据
import tensorflow as tf
import numpy as np
input1 = tf.placeholder(tf.float32,shape=(5, 5))
input2 = tf.placeholder(tf.float32,shape=(5, 5))
output = tf.matmul(input1, input2)#matmul is different mul
with tf.Session() as sess:
rand_array = np.ones([5, 5])
print sess.run([output], feed_dict={input1: rand_array,input2: rand_array})
参考:
官方手册:https://www.tensorflow.org/versions/r0.10/get_started/index.html
中文社区:http://www.tensorfly.cn/