来自tfreocords的图片和标签
...
train_images, train_labels = shuffle(train_all_images, train_all_labels)
...
但是train_labels
无效,如下所示:
numpy.sum(numpy.argmax(predictions, 1) == train_labels)
结果总是错误的,因为它根本无法从train_labels
获取值。
以下是一些细节:
train_all_images, train_all_labels = read_and_decode("train")
train_images, train_labels = shuffle(train_all_images, train_all_labels)
......一些训练模式
optimizer = tf.train.MomentumOptimizer(learning_rate,
0.9).minimize(loss,
global_step=batch)
train_prediction = tf.nn.softmax(logits)
with tf.Session() as sess:
tf.global_variables_initializer().run()
tf.train.start_queue_runners(sess)
print('Initialized!')
for step in xrange(int(num_epochs * train_size) // BATCH_SIZE):
sess.run(optimizer)
if step % EVAL_FREQUENCY == 0:
l, lr, predictions = sess.run([loss, learning_rate, train_prediction])
print('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))
print('Minibatch error: %.1f%%' % error_rate(predictions, train_labels))
sys.stdout.flush()
def error_rate(predictions, labels):
return 100.0 - ( 100.0 *
numpy.sum(numpy.argmax(predictions, 1) == labels) /
predictions.shape[0])
答案 0 :(得分:0)
原因是你必须使用数不多的mothods而不是numpy,以下是适当的。
def correct_rate(out, labels):
arg = tf.argmax(out, 1)
arg = tf.cast(arg, tf.int32)
correct_prediction = tf.equal(labels, arg)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
return accuracy
accr = correct_rate(logits, train_labels)
print(sess.run(accr))