【发布时间】: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 作为我的损失,但这并不能解决根本问题。
【问题讨论】:
-
请提供相关代码、模型架构等以及一些示例数据,以便对您有所帮助。
标签: python tensorflow keras autoencoder loss-function