TensorFlow in_top_k evaluation input argumants

后端 未结 1 577
天命终不由人
天命终不由人 2021-02-14 04:44

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

1条回答
  •  天涯浪人
    2021-02-14 05:25

    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.

    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:

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

    0 讨论(0)
提交回复
热议问题