Tensorflow 1.4 tf.metrics.auc 用于 AUC 计算 [英] Tensorflow 1.4 tf.metrics.auc for AUC calculation
问题描述
我正在尝试在我的模型的训练时间期间记录AUC.
I am trying to log AUC during training time of my model.
根据文档,tf.metric.auc
需要 label
和 predictions
,两者的形状相同.
According to the documentation, tf.metric.auc
needs a label
and predictions
, both of same shape.
但在我的二元分类情况下,label
是一个 一维 张量,只包含类.而prediction
是二维,包含每个数据点的每个类别的概率.
But in my case of binary classification, label
is a one-dimensional tensor, containing just the classes. And prediction
is two-dimensional containing probability for each class of each datapoint.
在这种情况下如何计算AUC?
How to calculate AUC in this case?
推荐答案
我们来看看函数中的参数tf.metrics.auc
:
Let's have a look at the parameters in the function tf.metrics.auc
:
labels
:形状与预测匹配的张量.将被强制转换为bool
.predictions
:任意形状的浮点张量,其值在[0, 1]
范围内.
labels
: A Tensor whose shape matches predictions. Will be cast tobool
.predictions
: A floating point Tensor of arbitrary shape and whose values are in the range[0, 1]
.
此操作已假定为二元分类.也就是说,labels
中的每个元素都说明该类对于单个样本是正"还是负".它不是一个 1-hot 向量,它需要一个元素与独占类数量一样多的向量.
This operation already assumes a binary classification. That is, each element in labels
states whether the class is "positive" or "negative" for a single sample. It is not a 1-hot vector, which requires a vector with as many elements as the number of exclusive classes.
同样,predictions
表示具有一定程度确定性的预测二元类(有些人可能称之为概率),并且每个元素也应该引用一个样本.它不是 softmax 向量.
Likewise, predictions
represents the predicted binary class with some level of certainty (some people may call it a probability), and each element should also refer to one sample. It is not a softmax vector.
如果概率来自具有 2 个神经元的全连接层和网络头部的 softmax 激活的神经网络,请考虑将其替换为单个神经元和 sigmoid 激活.现在可以将输出直接馈送到 tf.metrics.auc
.
If the probabilities came from a neural network with a fully connected layer of 2 neurons and a softmax activation at the head of the network, consider replacing that with a single neuron and a sigmoid activation. The output can now be fed to tf.metrics.auc
directly.
否则,您可以将预测张量切片以仅考虑正类,这将与二元类相同:
Otherwise, you can just slice the predictions tensor to only consider the positive class, which will represent the binary class just the same:
auc_value, auc_op = tf.metrics.auc(labels, predictions[:, 1])
这篇关于Tensorflow 1.4 tf.metrics.auc 用于 AUC 计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!