简体   繁体   English

Tensorflow Argmax相当于多标记分类

[英]Tensorflow Argmax equivalent for multilabel classification

I want to do evaluation of a classification Tensorflow model. 我想做一个分类Tensorflow模型的评估。

To compute the accuracy, I have the following code : 为了计算准确性,我有以下代码:

predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
accuracy = tf.metrics.accuracy(labels=label_ids, predictions=logits)

It work well in single label classification, but now I want to do multilabel classification, where my labels are Array of Integers instead of Integers. 它在单标签分类中运行良好,但现在我想进行多标签分类,其中我的标签是整数数组而不是整数。

Here is an example of label [0, 1, 1, 0, 1, 0] that are stored in label_ids , and an example of predictions [0.1, 0.8, 0.9, 0.1, 0.6, 0.2] from the Tensor logits 以下是存储在label_ids的标签[0, 1, 1, 0, 1, 0] label_ids [0, 1, 1, 0, 1, 0]的示例,以及来自Tensor logits的预测[0.1, 0.8, 0.9, 0.1, 0.6, 0.2] logits [0.1, 0.8, 0.9, 0.1, 0.6, 0.2]logits

What function should I use instead of argmax to do so ? 我应该使用什么功能而不是argmax呢? (My labels are arrays of 6 Integers with value of either 0 or 1) (我的标签是6个整数的数组,值为0或1)

If needed, we can suppose that there is a threshold of 0.5. 如果需要,我们可以假设存在0.5的阈值。

It is probably better to do this type of post-processing evaluation outside of tensorflow, where it is more natural to try several different thresholds. 在tensorflow之外进行这种类型的后处理评估可能更好,其中尝试几个不同的阈值更自然。

If you want to do it in tensorflow, you can consider: 如果你想在tensorflow中做,你可以考虑:

predictions = tf.math.greater(logits, tf.constant(0.5))

This will return a tensor of the original logits shape with True for all entries greater than 0.5. 对于大于0.5的所有条目,这将返回原始logits形状的张量,并为True。 You can then calculate accuracy as before. 然后,您可以像以前一样计算精度。 This is suitable for cases where many labels can be simultaneously true for a given sample. 这适用于对于给定样品可以同时具有许多标记的情况。

Use below code to caclutae accuracy in multiclass classification: 使用下面的代码来确定多类别中的caclutae准确度:

tf.argmax will return the axis where y value is max for both y_pred and y_true (actual y). 对于y_predy_true (实际y), tf.argmax将返回y值为max的轴。

Further tf.equal is used to find total number of matches (It returns True, False). 进一步使用tf.equal来查找匹配的总数(它返回True,False)。

Convert the boolean into float(ie 0 or 1) and use tf.reduce_mean to calculate the accuracy. 将布尔值转换为float(即0或1)并使用tf.reduce_mean计算精度。

correct_mask = tf.equal(tf.argmax(y_pred,1), tf.argmax(y_true,1))
accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))

Edit 编辑

Example with data: 数据示例:

import numpy as np

y_pred = np.array([[0.1,0.5,0.4], [0.2,0.6,0.2], [0.9,0.05,0.05]])
y_true = np.array([[0,1,0],[0,0,1],[1,0,0]])

correct_mask = tf.equal(tf.argmax(y_pred,1), tf.argmax(y_true,1))
accuracy = tf.reduce_mean(tf.cast(correct_mask, tf.float32))

with tf.Session() as sess:
  # print(sess.run([correct_mask]))
  print(sess.run([accuracy]))

Output: 输出:

[0.6666667]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM