用于传递 y_true 和 y_pred 以外的参数的 Keras 自定义损失函数 [英] Keras Custom loss function to pass arguments other than y_true and y_pred

查看:54
本文介绍了用于传递 y_true 和 y_pred 以外的参数的 Keras 自定义损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 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.kernelsomeLayer.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).

这里的答案显示了如果您的外部变量随批次变化时如何处理:在Keras中使用ImageDataGenerator时如何定义依赖于输入的自定义成本函数?

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?

这篇关于用于传递 y_true 和 y_pred 以外的参数的 Keras 自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆