Tensorflow MNIST 手写体识别代码注释(4)

Tensorflow MNIST 手写体识别代码注释(4)

tf.argmax

测试模型定义如下:

correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y,1))       

tf.argmax(input,axis) 根据 axis 取值的不同返回每行或者每列最大值的索引。

axis 的作用从维度的角度来看,并不复杂。例如 test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]]) 的维度是 4 x 3, axis = 0,就是把 维度 0 压缩,axis = 1,就是把维度 1 压缩。

t e s t = [ 1 2 3 2 3 4 5 4 3 8 7 2 ] test = \left[ \begin{matrix} 1&2&3\\ 2&3&4\\ 5&4&3\\ 8&7&2\\ \end{matrix} \right] test=125823473432

argmax(test, axis = 0) --> shape(4 x 3) --> shape( 1 x 3 ) --> [3, 3, 1]
argmax(test, axis = 1) --> shape(4 x 3) --> shape( 4 x 1 ) --> [2, 2, 0, 0]

显然,

tf.argmax(pred, 1) --> shape(100 x 10) --> shape(100 x 1)
tf.argmax(y   , 1) --> shape(100 x 10) --> shape(100 x 1)

事实上,他们返回了 100 张图片的分类标识。而

tf.equal()

tf.equal() 比较两个张量的元素是否相等。看个例子:

import tensorflow as tf
a = [[1,2,3],[4,5,6]]
b = [[1,0,3],[1,5,1]]
with tf.Session() as sess:
    print(sess.run(tf.equal(a,b)))

结果:

[[ True False  True]
 [False  True False]]

tf.cast(tf.equal(a,b), tf.float32) 的结果为:

[[ 1.0  0.0  1.0 ]
 [ 0.0  1.0  0.0 ]]

MNIST 的测试模型定义如下:

correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y,1))       
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy: ", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

注意,mnist.test.images、mnist.test.labels,表明测试结果是采用测试数据集测试的。

eval() 其实就是 tf.Tensor的Session.run() 的另外一种写法。上面些的那个代码例子,如果稍微修改一下,加上一个Session context manager:

with tf.Session() as sess:
	print(accuracy.eval({x:mnist.test.images,y_: mnist.test.labels}))

其效果和下面的代码是等价的:

with tf.Session() as sess:
	print(sess.run(accuracy, {x:mnist.test.images,y_: mnist.test.labels}))

但是要注意的是,eval() 只能用于 tf.Tensor 类对象,也就是有输出的 Operation。对于没有输出的 Operation, 可以用.run() 或者 Session.run()。Session.run() 没有这个限制。


到此为止,已经完全读懂 MNIST 的神经网络训练模型了。接下来需要做三件事情,第一件事,写一个自己的例子;第二件事,用 C++ 从头实现 MNIST 算法;第三件事,用 C++ 重构 Tensorflow 的训练结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许野平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值