gpt4 book ai didi

python - Keras 自定义损失实现 : ValueError: An operation has `None` for gradient

转载 作者:太空狗 更新时间:2023-10-29 17:51:18 25 4
gpt4 key购买 nike

我正在尝试实现这个损失函数: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 中使用自定义损失函数,请查看此 SO question

关于python - Keras 自定义损失实现 : ValueError: An operation has `None` for gradient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com