跳过 NaN 输入的自定义损失函数

Custom loss function that skips the NaN input

我正在构建一个自动编码器,我的数据中有 NaN 值。如何创建自定义 (MSE) 损失函数,如果它在验证数据中遇到 NaN 则不计算损失?

从网上得到提示:

def nan_mse(y_actual, y_predicted):
    per_instance = tf.where(tf.is_nan(y_actual),
                            tf.zeros_like(y_actual),
                            tf.square(tf.subtract(y_predicted, y_actual)))
    return tf.reduce_mean(per_instance, axis=0)

但是收到 NaN 的损失:

Epoch 1/50 - 25s - loss: nan

当我尝试在我的回调函数中使用自定义损失函数时,在每个纪元之后:

predictions = autoencoder.predict(x_pred)
mae = (nan_mse(x_pred, predictions))

TypeError: Input 'e' of 'Select' Op has type float32 that does not match type float64 of argument 't'.

我想,你的损失函数实际上效果很好。 nan 值可能来自预测。因此条件 tf.is_nan(y_actual) 不会过滤掉它。 要过滤掉预测的 nan,您应该执行以下操作:

import tensorflow.compat.v1 as tf
from tensorflow.compat.v1.keras import backend as K
import numpy as np


def nan_mse(y_actual, y_predicted):
    stack = tf.stack((tf.is_nan(y_actual), 
                      tf.is_nan(y_predicted)),
                     axis=1)
    is_nans = K.any(stack, axis=1)
    per_instance = tf.where(is_nans,
                            tf.zeros_like(y_actual),
                            tf.square(tf.subtract(y_predicted, y_actual)))
    print(per_instance)
    return tf.reduce_mean(per_instance, axis=0)

print(nan_mse([1.,1.,np.nan,1.,0.], [1.,1.,0.,0.,np.nan]))

输出:

tf.Tensor(0.2, shape=(), dtype=float32)