【发布时间】:2020-05-18 17:12:13
【问题描述】:
我在 tf.keras 中为多标签分类问题实现了一个自定义指标。
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 函数产生整数结果,但在运行模型时它给了我浮点值。在 keras 模型范围之外尝试时,自定义函数会给出正确的结果。
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))
为什么会这样?
【问题讨论】:
-
也许this related question 的答案会有所帮助。
标签: python keras tensorflow2.0 tf.keras