tensorflow mnist新手文档

官方文档
https://www.tensorflow.org/versions/r1.1/get_started/mnist/beginners
minist数据库
每一张图片对应28x28大小的灰度图,也就是大小为784,55,000张训练数据[55000, 784],10,000测试数据,每个数据对应一个label标签(0到9)
但是label用的是one-hot vectors(独热编码)格式[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],那么训练数据的标签库就是[55000, 10]数据为浮点。
tensorflow.placeholder(dtype, shape=None, name=None), 
dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
name:名称

import tensorflow as tf

读mnist数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

Wx + b = y
因为y是55000x10,所以W要是784x10, b要为1x10
公式用tf表示就是y = tf.nn.softmax(tf.matmul(x, W) + b)
这里建议用tf.nn.softmax_cross_entropy_with_logits (tf.matmul(x, W) + b) ,更稳定

常规的损失函数
y为实际输出,label为理论的
差的平方和 sum((y - label)^2)
交叉熵 -sum(label * log(y))   -- 最常用的计算标签(label)与输出(y)之间差别的方法
[0, 0, 1] 与 [0.1, 0.3, 0.6]的交叉熵为 -log(0.6) = 0.51
[0, 0, 1] 与 [0.2, 0.2, 0.6]的交叉熵为 -log(0.6) = 0.51
[0, 0, 1] 与 [0.1, 0, 0.9]的交叉熵为 -log(0.9) = 0.10
当label为0时,交叉熵为0,label为1时,交叉熵为-log(y),交叉熵只关注独热编码中有效位的损失。这样屏蔽了无效位值的变化(无效位的值的变化并不会影响最终结果),并且通过取对数放大了有效位的损失。当有效位的值趋近于0时,交叉熵趋近于正无穷大。

label数据 y_ = tf.placeholder(tf.float32, [None, 10])
reduce_mean即求正确率,值越低说明交叉熵越低说明越相似
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

训练,用梯度下降法,使得cross_entropy尽量最小,学习率为0.5,这里tf还有很多其他的方法
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

加载模型
sess = tf.InteractiveSession()
初始化我们创建的变量
tf.global_variables_initializer().run()

把batch_xs读入x成为训练数据 , batch_ys读入y_成为训练的标签
for _ in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

计算相似性
# argmax 返回最大值的下标,最大值的下标即答案
# 例如 [0,0,0,0.9,0,0.1,0,0,0,0] 代表数字3
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
这里返回一串的bool值,比如[true, true, true, false, false, true]

# correct_prediction  -> [true, true, true, false, false, true]  这个总6个,4个true就是2/3的正确率
# reduce_mean即求predict的平均数 即 正确个数 / 总数,即正确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

打印出正确率
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
这个模型大概能达到92%识别率,但是永远不够,好的能达到99%,此模型还是太简单。
作者:帅得不敢出门

发布了201 篇原创文章 · 获赞 20 · 访问量 41万+

猜你喜欢

转载自blog.csdn.net/zmlovelx/article/details/103244431