【发布时间】:2021-05-07 17:38:58
【问题描述】:
有没有办法在 keras 中编写自定义 MSE 损失函数?
我的训练样本是 k x n 输入的横截面数据,我的输出是时间 t 的 k x 1,但 t 的范围从 t-1 到 t-120(横截面数据的每月时间戳)。
我想编写一个自定义的 MSE 损失函数,它本质上对训练样本 t-120 赋予较低的权重,而对训练样本 t-1 赋予更高的权重。
有没有办法做到这一点?
这是一些在 keras 中编写自定义损失函数的简单代码。
def my_loss_fn(y_true, y_pred):
squared_difference = tf.square(y_true - y_pred)
return tf.reduce_mean(squared_difference, axis=-1) # Note the `axis=-1`
model.compile(optimizer='adam', loss=my_loss_fn)
【问题讨论】:
标签: python tensorflow keras