Keras custom loss implementation : ValueError: An operation has `None` for gradient
Keras custom loss implementation : ValueError: An operation has `None` for gradient
我正在尝试实现这个损失函数:MCFD_loss_function
来自本文档 (P6):Loss functions
所以我创建了一个这样的新函数:
def mcfd_loss(y_true, y_pred):
return K.sum( # ∑
K.cast(
K.greater( # only values greater than 0 (+ float32 cast)
K.dot(K.sign(y_pred), # π
K.sign(y_true))
, 0)
, 'float32')
)
但是当我开始训练时出现了这个错误:
ValueError: An operation has None
for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
我不知道我错过了哪一点。该错误似乎是因为我使用了更大的功能而引发的。我不知道这个错误是什么意思以及如何解决我的问题。
谢谢。
您希望损失函数检查 sign(f_(t,1))*sign(Y_(t+1))
是否大于 0。由于 sign
在 0 处不可微,我建议改用 softsign
。
由于大于函数也是不可微分的,因此可以使用以下近似值(参见 here):maxϵ(x,y):= 0.5(x + y + absϵ(x − y))
,其中 absϵ(x):=sqrt(x^2 + ϵ)
和 ϵ > 0
。为简单起见,我将在下面的代码示例中将此近似值称为 greater_approx
。 (注意,您只需插入上面的计算)
查看损失函数的定义,您必须将总和除以预测数 (K.get_variable_shape(y_pred)[0]
)(还要加上负号)。 P
对应根据Loss Functions in Time Series Forecasting paper.
预测的个数
总而言之,你的损失函数应该是这样的:
def mcfd_loss(y_true, y_pred):
return - (1/K.get_variable_shape(y_pred)[0]) * K.sum( # ∑
K.cast(
greater_approx( # only values greater than 0 (+ float32 cast)
K.dot(K.softsign(y_pred), # π
K.softsign(y_true))
, 0)
, 'float32')
)
最后一句话:要在 Keras 中使用自定义损失函数,请查看此
我正在尝试实现这个损失函数:MCFD_loss_function 来自本文档 (P6):Loss functions
所以我创建了一个这样的新函数:
def mcfd_loss(y_true, y_pred):
return K.sum( # ∑
K.cast(
K.greater( # only values greater than 0 (+ float32 cast)
K.dot(K.sign(y_pred), # π
K.sign(y_true))
, 0)
, 'float32')
)
但是当我开始训练时出现了这个错误:
ValueError: An operation has
None
for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
我不知道我错过了哪一点。该错误似乎是因为我使用了更大的功能而引发的。我不知道这个错误是什么意思以及如何解决我的问题。
谢谢。
您希望损失函数检查 sign(f_(t,1))*sign(Y_(t+1))
是否大于 0。由于 sign
在 0 处不可微,我建议改用 softsign
。
由于大于函数也是不可微分的,因此可以使用以下近似值(参见 here):maxϵ(x,y):= 0.5(x + y + absϵ(x − y))
,其中 absϵ(x):=sqrt(x^2 + ϵ)
和 ϵ > 0
。为简单起见,我将在下面的代码示例中将此近似值称为 greater_approx
。 (注意,您只需插入上面的计算)
查看损失函数的定义,您必须将总和除以预测数 (K.get_variable_shape(y_pred)[0]
)(还要加上负号)。 P
对应根据Loss Functions in Time Series Forecasting paper.
总而言之,你的损失函数应该是这样的:
def mcfd_loss(y_true, y_pred):
return - (1/K.get_variable_shape(y_pred)[0]) * K.sum( # ∑
K.cast(
greater_approx( # only values greater than 0 (+ float32 cast)
K.dot(K.softsign(y_pred), # π
K.softsign(y_true))
, 0)
, 'float32')
)
最后一句话:要在 Keras 中使用自定义损失函数,请查看此