跳过 NaN 输入的自定义损失函数 [英] Custom loss function that skips the NaN input

查看:35
本文介绍了跳过 NaN 输入的自定义损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个自动编码器,我的数据中有 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)))返回 tf.reduce_mean(per_instance,axis=0)

但收到 NaN 的损失:

<块引用>

纪元 1/50- 25s - 损失:nan

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

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

<块引用>

TypeError: 'Select' Op 的输入 'e' 的类型 float32 与参数 't' 的类型 float64 不匹配.

解决方案

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

 将 tensorflow.compat.v1 导入为 tf从 tensorflow.compat.v1.keras 导入后端为 K将 numpy 导入为 npdef nan_mse(y_actual, y_predicted):堆栈 = tf.stack((tf.is_nan(y_actual),tf.is_nan(y_predicted)),轴=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)))打印(每个实例)返回 tf.reduce_mean(per_instance,axis=0)打印(nan_mse([1.,1.,np.nan,1.,0.], [1.,1.,0.,0.,np.nan]))

出局:

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

I am building an autoencoder, my data has NaN values in it. How do I create a custom (MSE) loss function, that does not compute loss if it encounters a NaN in the validation data?

Got a hint from the web:

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)

But receive loss of NaN:

Epoch 1/50 - 25s - loss: nan

When I try using the custom loss function in my callback function, after each epoch:

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'.

解决方案

I guess, your loss function actually works well. The nan value probably comes from the predictions. Thus the condition tf.is_nan(y_actual) doesn't filter it out. To filter out the prediction's nan you should do as follows:

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]))

Out:

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

这篇关于跳过 NaN 输入的自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆