【问题标题】:How to get the class probabilities during the evaluation of CIFAR-10 in TensorFlow?如何在 TensorFlow 中评估 CIFAR-10 时获得类概率?
【发布时间】:2018-06-16 02:30:44
【问题描述】:

我尝试从Convolutional Neural Network TensorFlow Tutorial 修改code 以从每个测试图像中获取每个类的单一概率。

我可以使用 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)
    [....]

【问题讨论】:

    标签: python python-3.x tensorflow machine-learning conv-neural-network


    【解决方案1】:

    您可以从原始logits 计算类别概率:

    # The vector of probabilities per each example in a batch
    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))
    

    【讨论】:

    • 感谢您的回答,当我必须致电 tf.nn.softmax(logits) 时?
    • 是的,但谁知道呢? sess.run(prediction) 不起作用。
    • 您是开始新会话还是将其传递给eval_once
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 2019-10-25
    • 2010-10-26
    • 2016-06-24
    • 2020-10-09
    • 2018-08-28
    • 2011-09-22
    相关资源
    最近更新 更多