【问题标题】:Predictions of Autoencoder are all NaNs because of using custom loss function由于使用了自定义损失函数,自动编码器的预测都是 NaN
【发布时间】:2020-05-11 19:37:39
【问题描述】:

我正在为基因表达数据构建一个自动编码器。一些基因没有表达并且在输入中有NaN。我的输出(预测)都是 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)

和型号:

input_data = Input(shape=(1,num_genes))

#Leaky-Parametric-RelU
#Encoder
x = Dense(num_genes)(input_data)
encoder = PReLU()(x)

#Battleneck layer
encoded = Dense(64, activation = 'sigmoid')(encoder)

#Decoder
x = Dense(num_genes)(encoded)
decoder = PReLU()(x)

autoencoder = Model(input_data, decoder)
autoencoder.compile(loss=nan_mse, optimizer = 'adam') 
autoencoder.summary()

history = autoencoder.fit(x_train,x_train, epochs =50, verbose = 2),                      
 callbacks = [MyCustomCallback()])

我的目标是让网络忽略 NaN 值,但在输入中预先接收它们很重要。这可以通过完成损失函数来实现吗?

现在输出是 NaN。一位用户在这里建议编辑代码:

def nan_mse(y_actual, y_predicted):
    stack = tf.stack((tf.is_nan(y_actual), 
                      tf.is_nan(y_predicted)),
                     axis=1)
    is_nans = tf.keras.backend.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)

现在我得到 0.0000e+00 作为我的损失,但这并不能解决根本问题。

Original question.

【问题讨论】:

  • 请提供相关代码、模型架构等以及一些示例数据,以便对您有所帮助。

标签: python tensorflow keras autoencoder loss-function


【解决方案1】:

x=pd.read_csv("C:/Users/10.csv",index_col=False).groupby(['Column_name_optional']).mean().reset_index()

x = x.replace(np.nan, 0)

这将用零替换所有 NaN

【讨论】:

  • 我认为这不是提问者所需要的。根据问题中提供的代码级别,我怀疑提问者知道如何用零替换nan
猜你喜欢
  • 2018-09-13
  • 2019-07-20
  • 2021-04-05
  • 2019-08-30
  • 1970-01-01
  • 2021-07-21
  • 2020-08-06
  • 2017-08-31
  • 2020-05-11
相关资源
最近更新 更多