Tensorflow tf.nn.in_top_k错误目标[0]超出范围

时间:2016-07-28 20:40:47

标签: matrix tensorflow

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

问题在于我使用代码后

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()

总的来说,我只有一节课。

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

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

1 个答案:

答案 0 :(得分:0)

问题是labels张量中的数据无效。从your comment开始,labels张量是包含单个值的向量:[40]。值40大于forward_propagation_result中的列数(即4)。

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

  • 每行predictions[i, :]
      如果result[i]是该行中k个最大元素之一,则
    • predictions[i, targets[i]]为真;否则就是假的。

没有值predictions[0, 40],因为(正如您的评论所示)该参数是1 x 4矩阵。因此,TensorFlow会给您out of range错误。这表明您的评估数据是错误的,或者您应该使用不同的评估函数。