[英]tf.keras custom metric is giving incorrect results
I have implemented a custom metric in tf.keras for a multi label classification problem.我在 tf.keras 中为多 label 分类问题实现了一个自定义指标。
def multilabel_TP(y_true, y_pred, thres = 0.4):
return (
tf.math.count_nonzero(
tf.math.logical_and(tf.cast(y_true, tf.bool),
tf.cast(y_pred >= thres, tf.bool))
)
)
count_zero function produces integer results but while running the model it gives me float values. count_zero function 产生 integer 结果,但在运行 model 时它给了我浮点值。 The custom function gives me correct results when tried outside the scope of the keras model.
自定义 function 在 keras Z20F35E630DAF394DBFA4CZ3F8.DAF8 的 scope 之外尝试时给了我正确的结果
8/33 [======>.......................] - ETA: 27s - loss: 0.4294 - multilabel_TP: **121.6250**
model.compile(loss = 'binary_crossentropy', metrics = multilabel_TP, optimizer= 'adam')
model.fit(train_sentences, y_train, batch_size= 128, epochs = 20, validation_data= (test_sentences, y_test))
Why is this happenning?为什么会这样?
What is presented in the keras progress bar is a running mean of your loss/metrics over batches, since the model is being trained on batches and the weights are changing after each batch. keras 进度条中显示的内容是批次损失/指标的运行平均值,因为 model 正在批次上进行训练,并且每批次后权重都在变化。 This is why you get a floating point value.
这就是为什么你得到一个浮点值。
Your metric should also return a floating point value, maybe by taking a division over the number of elements in the batch.您的指标还应该返回一个浮点值,可能是对批处理中的元素数量进行除法。 Then the metric values will make more sense.
然后度量值将更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.