原文:https://blog.csdn.net/abc13526222160/article/details/85299901
1、tf.argmax(vector, 1)
返回的是vector中的最大值的索引号,如果vector是一个向量,那就返回一个值,如果是一个矩阵,那就返回一个向量,这个向量的每一个维度都是相对应矩阵行的最大值元素的索引号。
import tensorflow as tf
import numpy as np
A = [[1,3,4,5,6]]
B = [[1,3,4], [2,4,1]]
with tf.Session() as sess:
print(sess.run(tf.argmax(A, 1)))
print(sess.run(tf.argmax(B, 1)))
运行结果:
(tf14) zhangkf@Ubuntu2:~/lenet5$ python one.py
[4]
[2 1]
2、tf.equal()
tf.equal(A, B)是对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,否则返回False,返回的值的矩阵维度和A是一样的
import tensorflow as tf
import numpy as np
A = [[1,3,4,5,6]]
B = [[1,3,4,3,2]]
with tf.Session() as sess:
print(sess.run(tf.equal(A, B)))
运行结果:
(tf14) zhangkf@Ubuntu2:~/lenet5$ python one.py
[[ True True True False False]]
import tensorflow as tf
import numpy as np
A = [[1,3,4,5,6],[1,2,3,4,5]]
B = [[1,3,4,3,2],[2,2,3,4,3]]
with tf.Session() as sess:
print(sess.run(tf.equal(A, B)))
运行结果:
(tf14) zhangkf@Ubuntu2:~/lenet5$ python one.py
[[ True True True False False]
[False True True True False]]
3、二者结合起来
在测试模型的准确率的时候,通常二者结合在一起。
correct_prediction=tf.equal(tf.getmax(y,1),tf.getmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(corrcet_prediction,tf.float32))#求平均获取准确率
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
print(’############’)
print(“Correct_prediction:=”, correct_prediction)
print(’##############’)
print(tf.cast(correct_prediction, “float”))
print(’############’)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, “float”))
print(“Accuracy:”, accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
作者:炊烟袅袅岁月长
来源:CSDN
原文:https://blog.csdn.net/abc13526222160/article/details/85299901
版权声明:本文为博主原创文章,转载请附上博文链接!