首先,牢记几个函数,tf.equal(),tf.argmax(array, 1),tf.reduce_mean()
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)))
A == B
[ True True True False False]
False
A 和 B是 list,tensorflow支持list。
import numpy as np
A = [1,3,4,5,6]
B = [1,3,4,3,2]
A = np.array(A)
B = np.array(B)
A == B
array([ True, True, True, False, False], dtype=bool)
tf.argmax(array, 1) 指的是 axis=1
tf.argmax(array)默认axis=0
代码核心结构如下
images = tf.placeholder(tf.float32, [None, 256, 256, 3], name='input_images')
labels = tf.placeholder(tf.int64, [None], name='labels')
logits, _ = MyNet(images, num_classes=N_CLASSES)
correct_prediction = tf.equal(tf.argmax(logits, 1), labels)
evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
init=tf.global_variabels_initializer()
sess.run(init)
validation_accuracy = sess.run(evaluation_step, feed_dict={
images: validation_images, labels: validation_labels})
numpy 也能计算
X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,n_classes])
out = one_layer_net(w,b,X)
evaluation_step = np.mean(np.argmax(out,axis=1) == np.argmax(train_Y,axis=1))
with tf.Session() as sess:
init = tf.global_variables_intializer()
sess.run(init)
validation_accuracy = sess.run(evaluation_step, feed_dict={
X:validation_images,Y:validation_labels})
这里值得一提的是输入的数据,我们熟悉的输入数据通常是训练集的一个batch,而计算accuracy则是把全部数据输入计算,因此,人们弄出一个验证集validation set,通常比batch大而比训练集小。验证集可以是训练集中挑选和重复运用,而测试集test set一般不能和训练集重复。常用的比喻是:验证集是平时的模拟考试,而测试集是高考。