keras中自定义loss层如何接受输入值的案例

2024-03-18

小编给大家分享一下keras中自定义loss层如何接受输入值的案例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!

loss函数如何接受输入值

keras封装的比较厉害,官网给的例子写的云里雾里,

在stackoverflow找到了答案

You can wrap the loss function as a inner function and pass your input tensor to it (as commonly done when passing additional arguments to the loss function).

def custom_loss_wrapper(input_tensor):
 def custom_loss(y_true, y_pred):
  return K.binary_crossentropy(y_true, y_pred) + K.mean(input_tensor)
 return custom_loss