如何在TensorFlow中评估CIFAR-10时获取类别概率?

我尝试修改代码,来自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))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注