Tensorflow tf.nn.in_top_k错误目标[0]超出范围 [英] Tensorflow tf.nn.in_top_k Error targets[0] is out of range

查看:106
本文介绍了Tensorflow tf.nn.in_top_k错误目标[0]超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有四个输出标签的tensorflow程序.我训练了模型,现在正在使用它评估单独的数据.

I have a tensorflow program with four output labels. I trained the model and am now evaluating separate data with it.

问题是,在我使用代码后

The issue is that after I use the code

import tensorflow as tf

import main
import Process
import Input

eval_dir = "/Users/Zanhuang/Desktop/NNP/model.ckpt-30"
checkpoint_dir = "/Users/Zanhuang/Desktop/NNP/checkpoint"


def evaluate():
  with tf.Graph().as_default() as g:
    images, labels = Process.eval_inputs()
    forward_propgation_results = Process.forward_propagation(images)
    init_op = tf.initialize_all_variables()
    saver = tf.train.Saver()
    top_k_op = tf.nn.in_top_k(forward_propgation_results, labels, 1)

  with tf.Session(graph=g) as sess:
    sess.run(init_op)
    saver.restore(sess, eval_dir)
    tf.train.start_queue_runners(sess=sess)
    print(sess.run(top_k_op))

def main(argv=None):
    evaluate()

if __name__ == '__main__':
  tf.app.run()

我总共只有一堂课.

我的错误率代码是在一个热矩阵中引入标签的地方:

My code for the error rate, where I introduce the labels in a one hot matrix is here:

def error(forward_propagation_results, labels):
    labels = tf.one_hot(labels, 4)
    tf.transpose(labels)
    labels = tf.cast(labels, tf.float32)
    mean_squared_error = tf.square(tf.sub(labels, forward_propagation_results))
    cost = tf.reduce_mean(mean_squared_error)
    train = tf.train.GradientDescentOptimizer(learning_rate = 0.05).minimize(cost)
    tf.histogram_summary('accuracy', mean_squared_error)
    tf.add_to_collection('losses', cost)

    tf.scalar_summary('LOSS', cost)

    return train, cost

推荐答案

问题是您的labels张量中的数据无效.来自您的注释labels张量是一个包含单个值:[40]的向量.值40大于forward_propagation_result中的列数(为4).

The problem is invalid data in your labels tensor. From your comment, the labels tensor is a vector containing a single value: [40]. The value 40 is larger than the number of columns in the forward_propagation_result (which is 4).

tf.nn.in_top_k(predictions, targets, k)操作具有以下行为:

  • 对于每行predictions[i, :]:
      如果predictions[i, targets[i]]是该行中k个最大元素之一,则
    • result[i]为true.否则,它是错误的.
    • For each row predictions[i, :]:
      • result[i] is true if predictions[i, targets[i]] is one of the k largest elements in that row; otherwise it is false.

      没有值predictions[0, 40],因为(如您的注释所示)该参数是1 x 4矩阵.因此,TensorFlow给您一个out of range错误.这表明您的评估数据有误,或者您应该使用其他评估函数.

      There is no value predictions[0, 40], because (as your comment shows) that argument is a 1 x 4 matrix. Therefore TensorFlow gives you an out of range error. This suggests that either your evaluation data are wrong, or you should be using a different evaluation function.

      这篇关于Tensorflow tf.nn.in_top_k错误目标[0]超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆