首页 > 解决方案 > 设计自定义 keras 损失函数 MSE

问题描述

我想制作一个仅使用输出层中的一项的自定义 MSE 损失函数。

我现在拥有的是这样的:

def new_loss(y_true, y_pred):
   index_pred = K.argmax(K.abs(y_pred), axis = -1)
   pred = tf.gather(y_true, index_pred, axis = 1)

   index_true = K.argmin(K.abs(y_true), axis = -1)
   true = tf.gather(y_true, index_true, axis = 1)

   return K.mean(K.sqrt(K.square(K.log(pred)-K.log(true))))

但它给出了错误,

An operation has `None` for gradient.

我一直在寻找,但在我的场景中找不到任何工作。

我正在使用 Keras 和 Tensorflow 作为后端。

先感谢您。

编辑:

我试过了

def new_loss(y_true, y_pred):
   index_pred = K.argmax(K.abs(y_pred), axis = -1)
   pred = tf.gather(y_pred, index_pred, axis = 1)

   index_true = K.argmin(K.abs(y_true), axis = -1)
   true = tf.gather(y_true, index_true, axis = 1)

   return K.mean(K.sqrt(K.square(K.log(pred)-K.log(true))))

它没有给出错误。所以问题不是 K.argmax/K.argmin。

标签: kerasmse

解决方案


推荐阅读