Keras多输出定制损失,中间层输出 [英] Keras multioutput custom loss with intermediate layers output

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

问题描述

我在keras中有一个模型,该模型需要两个输入,并返回3个输出,我想计算自定义损失.我的问题是我不知道如何在损耗中使用中间层的输出.到目前为止,该模型由两个子模型(图中的子模型1和子模型2)组成,最终损失由loss1和loss2之和组成.这很容易,因为loss1将output1与数据生成器的label1进行比较,并将output2与数据生成器的label2进行比较.

I have a model in keras which takes two inputs and it returns 3 outputs and I want to compute a custom loss. The problem I have is that I don't know how to use the output of intermediate layers in the loss. So far the model consisted of two submodels (submodel1 and submodel2 in the picture) and the final loss consisted of the sum of loss1 and loss2. This was easy because loss1 compared output1 with label1 of the data generator and output2 with label2 of the data generator.

当我在模型中包括子模型3时,问题就来了,因为loss3将output1与output3进行比较,将output1作为模型层的输出,而不是将其作为数据生成器的label3的输出.我已经尝试过这种方式:

The problem comes when I include the submodel3 in the model, because the loss3 compares the output1 with the output3, being output1 the output of a layer of the model, and not the one that would be the label3 of the data generator. I have tried this way:

input1 = Input(shape=input1_shape)
input2 = Input(shape=input2_shape)
output1 = submodel1()([input1,input2]) #do not pay attention to the code notation, as it is a code to explain the problem.
output2 = submodel2()(output1)
output3 =  submodel3()(output1)
@tf.function
def MyLoss(y_true, y_pred):
    out1, out2, out3 = y_pred
    inp1, inp2 = y_true
            
    loss1 = tf.keras.losses.some_loss1(out1,inp1)
    loss2 = tf.keras.losses.some_loss2(out2, inp2)
    loss3 = tf.keras.losses.some_loss3(out2,out3)

    loss = loss1 + loss2 + loss3
    return loss

model = Model([input1,input2],[output1,output2,output3])
model.compile(optimizer='adam',loss = MyLoss)

但是我得到这个错误:

 OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.

我正在使用TensorFlow 2.3.0-rc0版本.

I am working with TensorFlow 2.3.0-rc0 version.

推荐答案

您可以使用 add_loss 将多层输出传递给自定义函数.下面我在虚拟回归任务中复制您的案例

You can use add_loss to pass multiple layers output to your custom function. below I replicate your case in a dummy regression task

X1 = np.random.uniform(0,1, (100,5))
X2 = np.random.uniform(0,1, (100,5))

y1 = np.random.uniform(0,1, 100)
y2 = np.random.uniform(0,1, 100)


def MyLoss(true1, true2, out1, out2, out3):

    loss1 = tf.keras.losses.mse(out1, true1)
    loss2 = tf.keras.losses.mse(out2, true2)
    loss3 = tf.keras.losses.mse(out2, out3)

    loss = loss1 + loss2 + loss3
    return loss


input1 = Input(shape=(5,))
input2 = Input(shape=(5,))

output1 = Dense(1)(Concatenate()([input1,input2]))
output2 = Dense(1)(output1)
output3 = Dense(1)(output1)

true1 = Input(shape=(1,))
true2 = Input(shape=(1,))

model = Model([input1,input2,true1,true2], [output1,output2,output3])
model.add_loss(MyLoss(true1, true2, output1, output2, output3))
model.compile(optimizer='adam', loss=None)

model.fit(x=[X1,X2,y1,y2], y=None, epochs=3)

以推理模式使用模型(删除 y1 y2 作为输入):

to use the model in inference mode (remove y1, y2 as input):

final_model = Model(model.inputs[:2], model.output)
final_model.predict([X1,X2])

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

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