第一章
1、什么是占位符和变量?
无论是占位符还是变量,都是tensor,tensor是tensorflow计算的节点。
占位符和变量是不同类型的tensor。占位符的值由用户自行传递,不依赖于其他tensor,通常用来存储样本数据和标签。
tf.Tensor类是核心类,占位符(tf.placeholder)和变量(tf.Variable)都可以看作特殊的tensor。
可以参阅http://www.tensorfly.cn/tfdoc/how_tos/variables.html
2、什么是会话?变量和占位符在会话中如何传递?
会话是一个核心概念,tensor是图计算的节点,会话是对这些节点进行计算的上下文。
变量是计算过程中可以改变的值的tensor,变量的值会被保存下来。在对变量进行操作前必须进行变量初始化,即在会话中保存变量的初始值。
训练时,每次提取一部分数据进行训练,把他们放入对应的占位符中,在会话中,不需要计算占位符的值,而是直接把占位符的值传递给会话。
会话中,变量的值会被保存下来,占位符的值不会被保存,每次可以给占位符传递不同的值。
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# x是一个占位符,表示待识别的图片
# 形状是[None, 784],None表示这一维的大小可以是任意的
x = tf.placeholder(tf.float32, [None, 784])
# 变量参数用tf.Variable
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
# y_是一个占位符,表示实际的图像标签,独热表示
y_ = tf.placeholder(tf.float32, [None, 10])
# 交叉熵
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)))
# 梯度下降,学习率是0.01
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# 创建session,只有在session中才能运行优化步骤train_step
sess = tf.InteractiveSession()
# 运行之前必须要初始化所有变量,分配内存
tf.global_variables_initializer().run()
print('start training...')
for _ in range(1000):
# batch_xs: (100, 784), batch_ys: (100, 10)
batch_xs, batch_ys = mnist.train.next_batch(100)
# sess中运行train_step,运行时要使用feed_dict传入对应占位符的值
sess.run(train_step, feed_dict={x: batch_xs, y_:batch_ys})
3、计算图流程(画出思维导图)
# 独热表示的y_ 需要通过sess.run(y_)才能获取此tensor的值
print(tf.argmax(y, 1))
# output: Tensor("ArgMax:0", shape=(?,), dtype=int64)
print(tf.argmax(y_, 1))
# output: Tensor("ArgMax_1:0", shape=(?,), dtype=int64)
# tf.equal 比较是否相等,输出true和false
# tf.argmax(y,1), tf.argmax(y_,1), 取出数组中最大值的下标,可以用独热表示以及模型输出转换为数字标签
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
# tf.cast 将比较值转换为float32型的变量,true转换为1,false转换为0
# tf.reduce_mean 计算数组中的所有元素的平均值,得到模型的预测准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 使用全体测试样本预测,mnist.test.images, mnist.test.labels
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
# 只有输入了x,y_,通过sess.run才可以计算出correct_prediction,accuracy
4、扩展阅读
- 本章的两个MNIST 程序实际上来自于TensorFlow 官方的两个新手教程,地址为https://www.tensorflow.org/get_started/mnist/beginners 和 https://www.tensorflow.org/get_started/mnist/pros 。读者可以将本书的内容和官方的教程对照起来进行阅读。这两个新手教程的中文版地址为http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html 和http://www.tensorfly.cn/tfdoc/tutorials/mnist_pros.html。
- 本章简要介绍了TensorFlow 的tf.Tensor 类。tf.Tensor 类是TensorFlow的核心类,常用的占位符(tf.placeholder)、变量(tf.Variable)都可以看作特殊的Tensor。读者可以参阅https://www.tensorflow.org/programmers_guide/tensors 来更深入地学习它的原理。
- 常用tf.Variable 类来存储模型的参数, 读者可以参阅https://www.tensorflow.org/programmers_guide/variables 详细了解它的运行机制, 文档的中文版地址为http://www.tensorfly.cn/tfdoc/how_tos/ variables.html。
- 只有通过会话(Session)才能计算出tf.Tensor 的值。强烈建议读者 在学习完tf.Tensor 和tf.Variable 后,阅读https://www.tensorflow.org/programmers_guide/graphs 中的内容,该文档描述了TensorFlow 中 计算图和会话的基本运行原理,对理解TensorFlow 的底层原理有很 大帮助。
第二章
tensorflow的数据读取原理
画出思维导图