Keras自定义损失与多个输出的关系 [英] Keras custom loss as a function of multiple outputs

查看:577
本文介绍了Keras自定义损失与多个输出的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用keras(一个convnet)构建了一个自定义体系结构。该网络有4个头,每个头输出不同大小的张量。我试图编写一个自定义损失函数作为这4个输出的函数。我以前曾实施过客户损失,但要么是每个头损失不同,要么是每个头损失相同。在这种情况下,我需要组合4个输出来计算损耗。

I built a custom architecture with keras (a convnet). The network has 4 heads, each outputting a tensor of different size. I am trying to write a custom loss function as a function of this 4 outputs. I have been implementing cusutom losses before, but it was either a different loss for each head or the same loss for each head. In this case, I need to combine the 4 outputs to calculate the loss.

我习惯于以下情况:

def custom_loss(y_true, y_pred):
    return something
model.compile(optimizer, loss=custom_loss)

但就我而言,我需要 y_pred 来列出4个输出。我可以用零填充输出,并在模型中添加一个连接层,但是我想知道是否有更简单的方法。

but in my case, I would need y_pred to be a list of the 4 outputs. I can pad the outputs with zeros and add a concatenate layer in my model, but I was wondering if there was an easier way around.

我的损失函数相当复杂,我可以写这样的东西吗:

My loss function is rather complex, can I write something like:

model.add_loss(custom_loss(input1, input2, output1, output2))

其中自定义损失定义为:

where custom loss is defined as:

def custom_loss(input1, input2, output1, output2):
    return loss


推荐答案

该解决方案在TensorFlow和Keras的最新版本中不再有效。

您可以尝试使用 model.add_loss()函数。想法是将自定义损失构造为张量而不是函数,将其添加到模型中,然后在不进一步指定损失的情况下编译模型。另请参见变体自动编码器的此实现使用类似想法的地方。

You could try the model.add_loss() function. The idea is to construct your custom loss as a tensor instead of a function, add it to the model, and compile the model without further specifying a loss. See also this implementation of a variational autoencoder where a similar idea is used.

例如:

import keras.backend as K
from keras.layers import Input, Dense
from keras.models import Model
from keras.losses import mse
import numpy as np

# Some random training data
features = np.random.rand(100,20)
labels_1 = np.random.rand(100,4)
labels_2 = np.random.rand(100,1)

# Input layer, one hidden layer
input_layer = Input((20,))
dense_1 = Dense(128)(input_layer)

# Two outputs
output_1 = Dense(4)(dense_1)
output_2 = Dense(1)(dense_1)

# Two additional 'inputs' for the labels
label_layer_1 = Input((4,))
label_layer_2 = Input((1,))

# Instantiate model, pass label layers as inputs
model = Model(inputs=[input_layer, label_layer_1, label_layer_2], outputs=[output_1, output_2])

# Construct your custom loss as a tensor
loss = K.mean(mse(output_1, label_layer_2) * mse(output_2, label_layer_2))

# Add loss to model
model.add_loss(loss)

# Compile without specifying a loss
model.compile(optimizer='sgd')

model.fit(features, [labels_1, labels_2], epochs=2)

这篇关于Keras自定义损失与多个输出的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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