用于没有 all() 的多标签分类的 keras 自定义指标

keras custom metrics for multi-label classification without all()

我正在使用 sigmoid 和 binary_crossentropy 进行多标签分类。一个非常相似的问题问 。并建议使用以下自定义指标:

from keras import backend as K

def full_multi_label_metric(y_true, y_pred):
    comp = K.equal(y_true, K.round(y_pred))
    return K.cast(K.all(comp, axis=-1), K.floatx())

但我不想使用 all() 因为对于一个真实标签为 [1, 0, 0, 1, 1] 且预测标签为 [0, 0, 0, 1, 1] 我不认为预测准确度为零(因为最后四个 类 的标签已被正确预测)。

这是我的模型:

# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(Masking(mask_value=-9999, input_shape=(197, 203)))
model.add(LSTM(512, return_sequences=True))
model.add(Dense(20, activation='sigmoid')) 
model.compile(loss='binary_crossentropy',
              optimizer=SGD(lr=1e-3, decay=1e-4, momentum=0.9, nesterov=True),
              metrics = ['accuracy'])
print(model.summary())

这是我的 y_pred 一个例子:

pred = model.predict(X_test)
y_pred = pred[0,196,:]
y_pred
array([2.6081860e-01, 9.9079555e-01, 1.4816311e-01, 8.6009043e-01,
       2.6759505e-04, 3.0792636e-01, 2.6738405e-02, 8.5339689e-01,
       5.1105350e-02, 1.5427300e-01, 6.7039116e-05, 1.7909735e-02,
       6.4140558e-04, 3.5133284e-01, 5.3054303e-02, 1.2765944e-01,
       2.9298663e-04, 6.3041472e-01, 5.8620870e-03, 5.9656668e-01],
      dtype=float32)

这是我的 y_true 举个例子:

y_true = Y_test[0,0,:]
y_true
array([1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 1., 0., 0.,
       0., 0., 1.])

我的问题是:如何设置 Keras 自定义度量函数,以便将 y_pred 中的每个元素与 y_true 中的每个元素进行比较,然后在期间给出准确度度量训练?我想在 metrics = [X])?

中使用这个指标

除非我弄错了,否则默认的 binary_crossentropy metric/loss 已经满足了您的需要。以你为例

import tensorflow as tf
from tensorflow import keras

y_true = tf.constant([[1, 0, 0, 1, 1]], dtype=tf.int32)
y_pred = tf.constant([[0.6, 0, 0, 1, 1]], dtype=tf.float32)

m = keras.metrics.binary_crossentropy(y_true, y_pred)
m.numpy()

输出为 [-log(0.6) / 5]。

即批次的 metric/loss 考虑了模型 20 个输出中每个输出的损失。我假设它代表时间步长。

作为度量标准,使用 binary_accuracy 更为常见。 示例:

y_true = tf.constant([[1, 0, 0, 1, 1]], dtype=tf.int32)
y_pred = tf.constant([[0.1, 0, 0, 1, 1]], dtype=tf.float32)
keras.metrics.binary_accuracy(tf.cast(y_true, tf.float32), y_pred)

人们可以通过 ROC 指标(https://www.tensorflow.org/api_docs/python/tf/keras/metrics/AUC) which measures the curve at various thresholds see an explanation at https://towardsdatascience.com/understanding-auc-roc-curve-68b2303cc9c5)更好地了解模型的性能。就我个人而言,我倾向于在训练时使用准确性指标,然后查看 precision/recall 曲线训练模型以检查其行为是否符合预期和 select 预测阈值。