您可以编写自定义的损失函数,并将缺失值暂时替换为零。然后在计算交叉熵损失后,将标签缺失的位置的损失值替换为零。
import numpy as np
import tensorflow as tf
tf.enable_eager_execution()
def missing_values_cross_entropy_loss(y_true, y_pred):
epsilon = tf.constant(1.0e-30, dtype=np.float32)
y_pred = tf.debugging.assert_all_finite(y_pred, 'y_pred contains NaN')
y_true_not_nan_mask = tf.logical_not(tf.math.is_nan(y_true))
y_true_nan_replaced = tf.where(tf.math.is_nan(y_true), tf.zeros_like(y_true), y_true)
positive_predictions_cross_entropy = y_true_nan_replaced * tf.math.log(y_pred + epsilon)
negative_predictions_cross_entropy = (1.0 - y_true_nan_replaced) * tf.math.log(1.0 - y_pred + epsilon)
cross_entropy_loss = -(positive_predictions_cross_entropy + negative_predictions_cross_entropy)
cross_entropy_loss_discarded_nan_labels = cross_entropy_loss * tf.cast(y_true_not_nan_mask, tf.float32)
mean_loss_per_row = tf.reduce_mean(cross_entropy_loss_discarded_nan_labels, axis=1)
mean_loss = tf.reduce_mean(mean_loss_per_row)
return mean_loss
y_true = tf.constant([
[0, 1, np.nan, 0],
[0, 1, 1, 0],
[np.nan, 1, np.nan, 0],
[1, 1, 0, np.nan],
])
y_pred = tf.constant([
[0.1, 0.7, 0.1, 0.3],
[0.2, 0.6, 0.1, 0],
[0.1, 0.9, 0.3, 0.2],
[0.1, 0.4, 0.4, 0.2],
])
loss = weighted_cross_entropy_loss(y_true, y_pred)
print(loss.numpy())
输出:
0.4945919
在编译keras模型时,请按照文档中指定的损失函数进行操作:
model.compile(loss=missing_values_cross_entropy_loss, optimizer='sgd')
nans
,还是在 x 中也有? - Marcin Możejkox
怎么样?对于prediction
训练来说,它们是无用的。你只能在pretrain
中使用它们。 - Marcin Możejko