tensorflow - 带有中间层输出的 Keras 多输出自定义损失
问题描述
我在 keras 中有一个模型,它需要两个输入并返回 3 个输出,我想计算自定义损失。我遇到的问题是我不知道如何在损失中使用中间层的输出。到目前为止,该模型由两个子模型(图中的 submodel1 和 submodel2)组成,最终损失由 loss1 和 loss2 之和组成。这很容易,因为 loss1 将 output1 与数据生成器的 label1 进行比较,将 output2 与数据生成器的 label2 进行比较。
当我在模型中包含 submodel3 时问题就来了,因为 loss3 将 output1 与 output3 进行比较,输出 1 是模型的一层的输出,而不是数据生成器的 label3 的输出。我试过这样:
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 版本。
解决方案
您可以使用add_loss
将多层输出传递给您的自定义函数。下面我在虚拟回归任务中复制您的案例
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
作为输入):
final_model = Model(model.inputs[:2], model.output)
final_model.predict([X1,X2])
推荐阅读
- rust - 在 Tokio 中向非线程安全的单例对象分派请求
- python - 使用加法器电路将两个 4 位二进制数相加
- flask - 如何将计数器连接到 Jinja2 表达式?
- quickfixj - 重置后是否需要重新订阅市场数据?
- node.js - axios 上传到 node.js API 导致 aws s3 中的文件变得不可读
- sql - 在databricks中使用sql创建最大日期函数
- javascript - 如何让 JavaScript 函数应用的类一直运行到窗口关闭(网页设计)
- python - 在有和没有包依赖的情况下进行测试
- javascript - 带有 Javascript 的 html 中的表格过滤器不起作用
- django - 从 Redis 丢失计划的 Celery 任务