TensorFlow in_top_k 评估输入参数 [英] TensorFlow in_top_k evaluation input argumants

查看:30
本文介绍了TensorFlow in_top_k 评估输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习此链接中的教程 并尝试更改模型的评估方法(在底部).我想获得前 5 名的评价,我正在尝试使用以下代码:

I am following the tutorial in this link and trying to change the evaluation method for the model (at the bottom). I would like to get a top-5 evaluation and I'm trying to use to following code:

topFiver=tf.nn.in_top_k(y, y_, 5, name=None)

然而,这会产生以下错误:

However, this yields the following error:

File "AlexNet.py", line 111, in <module>
    topFiver = tf.nn.in_top_k(pred, y, 5, name=None)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 346, in in_top_k
    targets=targets, k=k, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 486, in apply_op
    _Attr(op_def, input_arg.type_attr))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 59, in _SatisfiesTypeConstraint
    ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: DataType float32 for attr 'T' not in list of allowed values: int32, int64

据我所知,问题是 tf.nn.in_top_k() 仅适用于 tf.int32tf.int64 数据,但我的数据采用 tf.float32 格式.有什么解决方法吗?

As far as I can tell, the problem is that tf.nn.in_top_k() only works for tf.int32 or tf.int64 data, but my data is in tf.float32 format. Is there any workaround for this?

推荐答案

targets 参数 tf.nn.in_top_k(predictions, targets, k) 必须是类 ID 的向量(即列的索引)在 predictions 矩阵中).这意味着它只适用于单类分类问题.

The targets argument to tf.nn.in_top_k(predictions, targets, k) must be a vector of class IDs (i.e. indices of columns in the predictions matrix). This means that it only works for single-class classification problems.

如果您的问题是单类问题,那么我假设您的 y_ 张量是示例的真实标签的单热编码(例如,因为您还将它们传递给op like tf.nn.softmax_cross_entropy_with_logits().在这种情况下,您有两个选择:

If your problem is a single-class problem, then I assume that your y_ tensor is a one-hot encoding of the true labels for your examples (for example because you also pass them to an op like tf.nn.softmax_cross_entropy_with_logits(). In that case, you have two options:

  • 如果标签最初存储为整数标签,则将它们直接传递给 tf.nn.in_top_k() 而不将它们转换为 one-hot.(另外,考虑使用 tf.nn.sparse_softmax_cross_entropy_with_logits() 作为你的损失函数,因为它可能更有效.)
  • 如果标签最初以 one-hot 格式存储,您可以使用 tf.argmax() 将它们转换为整数:

  • If the labels were originally stored as integer labels, pass them directly to tf.nn.in_top_k() without converting them to one-hot. (Also, consider using tf.nn.sparse_softmax_cross_entropy_with_logits() as your loss function, because it may be more efficient.)
  • If the labels were originally stored in the one-hot format, you can convert them to integers using tf.argmax():

labels = tf.argmax(y_, 1)
topFiver = tf.nn.in_top_k(y, labels, 5)

这篇关于TensorFlow in_top_k 评估输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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