Keras自定义损失函数传递y_true和y_pred以外的参数 [英] Keras Custom loss function to pass arguments other than y_true and y_pred
问题描述
我正在编写一个keras自定义损失函数,在其中我希望将以下内容传递给该函数: y_true,y_pred(无论如何这两个都会自动传递),模型内部图层的权重和常量.
I am writing a keras custom loss function where in I want to pass to this function the following: y_true, y_pred (these two will be passed automatically anyway), weights of a layer inside the model, and a constant.
类似以下内容:
def Custom_loss(y_true, y_pred, layer_weights, val = 0.01):
loss = mse(y_true, y_pred)
loss += K.sum(val, K.abs(K.sum(K.square(layer_weights), axis=1)))
return loss
但是上面的实现给了我错误. 如何在keras中实现这一目标?
But the above implementation gives me error. How can I achieve this in keras ?
推荐答案
新答案
我认为您正在寻找L2正则化.只需创建一个正则化器并将其添加到图层中即可:
New answer
I think you're looking exactly for L2 regularization. Just create a regularizer and add it in the layers:
from keras.regularizers import l2
#in the target layers, Dense, Conv2D, etc.:
layer = Dense(units, ..., kernel_regularizer = l2(some_coefficient))
您也可以使用bias_regularizer
.
some_coefficient
var乘以权重的平方值.
You can use bias_regularizer
as well.
The some_coefficient
var is multiplied by the square value of the weight.
PS:如果代码中的val
是恒定的,则不应损害您的损失.但是您仍然可以对val
使用下面的旧答案.
PS: if val
in your code is constant, it should not harm your loss. But you can still use the old answer below for val
.
根据需要将Keras预期函数(带有两个参数)包装为外部函数:
Wrap the Keras expected function (with two parameters) into an outer function with your needs:
def customLoss(layer_weights, val = 0.01):
def lossFunction(y_true,y_pred):
loss = mse(y_true, y_pred)
loss += K.sum(val, K.abs(K.sum(K.square(layer_weights), axis=1)))
return loss
return lossFunction
model.compile(loss=customLoss(weights,0.03), optimizer =..., metrics = ...)
请注意,layer_weights
必须直接作为张量"来自图层,因此不能使用get_weights()
,必须使用someLayer.kernel
和someLayer.bias
. (或者在使用不同名称作为可训练参数的图层的情况下,使用相应的var名称).
Notice that layer_weights
must come directly from the layer as a "tensor", so you can't use get_weights()
, you must go with someLayer.kernel
and someLayer.bias
. (Or the respective var name in case of layers that use different names for their trainable parameters).
The answer here shows how to deal with that if your external vars are variable with batches: How to define custom cost function that depends on input when using ImageDataGenerator in Keras?
这篇关于Keras自定义损失函数传递y_true和y_pred以外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!