Tensorflow tf.nn.in_top_k报错 Error targets[0] is out of range

用cifar10在做分类问题的的时候遇到了这个问题

在国内网站上貌似没有相关的信息

但是在stackoverflow上面有人在讨论:

https://stackoverflow.com/questions/37587622/tf-nn-in-top-k-targets-out-of-range

其中有个人的回复很有意思

--------------------------------------------

To sum it up, the function tf.nn.in_top_k(predictions, targets, k) (see the doc) has arguments:

  • predictions: shape [batch_size, num_classes], type float32
  • targets (the correct label): shape [batch_size], type int32 or int64

The function raises the error InvalidArgumentError: targets[i] is out of range when the element targets[i] is out of range in predictions[i].

For instance, there are 2 classes (num_classes=2) and targets=[1, 3]. With these targets, you will see an error InvalidArgumentError: targets[1] is out of range because targets[1] = 3 is out of range for predictions[1] which has only shape 2.


To check that your labels are correct, you can print the max of them:

labels = ...
labels_max = tf.reduce_max(labels)

sess = tf.Session()
print sess.run(labels_max)

If the value printed is superior to num_classes, you have a problem.

--------------------------------------------

以上是一个很好的分析思路

猜你喜欢

转载自blog.csdn.net/kakangel/article/details/90040936