我尝试修改代码,来自TensorFlow卷积神经网络教程,以便从每个测试图像中获取每个类别的单个概率值。
我可以使用什么替代tf.nn.in_top_k
的方法?因为这个方法只返回一个布尔张量,但我希望保留各个值。
我使用的是TensorFlow 1.4和Python 3.5,我认为第62-82行和第121-129/142行可能是需要修改的部分。有人能给我一些提示吗?
第62-82行:
def eval_once(saver, summary_writer, top_k_op, summary_op): """Run Eval once. Args: saver: Saver. summary_writer: Summary writer. top_k_op: Top K op. summary_op: Summary op. """ with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) # Assuming model_checkpoint_path looks something like: # /my-favorite-path/cifar10_train/model.ckpt-0, # extract global_step from it. global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] else: print('No checkpoint file found')return
第121-129 + 142行
[....] images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) [....]
回答:
您可以从原始logits
计算类别概率:
# 每个批次中每个示例的概率向量prediction = tf.nn.softmax(logits)
作为奖励,这里是如何获取精确的准确率:
correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))