Tensorflow 多输入多输出的Keras模型

Tensorflow 多输入多输出的Keras模型,tensorflow,keras,Tensorflow,Keras,我想用两个输入和两个输出构建一个Keras模型,它们都使用相同的架构/权重。然后,这两个输出都用于计算​ 单一损失 这是我想要的建筑的图片 这是我的伪代码: model = LeNet(inputs=[input1, input2, input3],outputs=[output1, output2, output3]) model.compile(optimizer='adam', loss=my_custom_loss_function([output1,outpu2

我想用两个输入和两个输出构建一个Keras模型,它们都使用相同的架构/权重。然后,这两个输出都用于计算​ 单一损失

这是我想要的建筑的图片

这是我的伪代码:

model = LeNet(inputs=[input1, input2, input3],outputs=[output1, output2, output3])

model.compile(optimizer='adam',
          loss=my_custom_loss_function([output1,outpu2,output3],target)
          metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
这种方法有效吗?

我需要使用不同的Keras API吗?

体系结构很好。下面是一个玩具示例,其中包含如何使用keras的函数API定义的培训数据:

from keras.models import Model
from keras.layers import Dense, Input

# two separate inputs
in_1 = Input((10,10))
in_2 = Input((10,10))


# both inputs share these layers
dense_1 = Dense(10)
dense_2 = Dense(10)

# both inputs are passed through the layers
out_1 = dense_1(dense_2(in_1))
out_2 = dense_1(dense_2(in_2))

# create and compile the model
model = Model(inputs=[in_1, in_2], outputs=[out_1, out_2])

model.compile(optimizer='adam', loss='mse')
model.summary()

# train the model on some dummy data
import numpy as np

i_1 = np.random.rand(10, 10, 10)
i_2 = np.random.rand(10, 10, 10)

model.fit(x=[i_1, i_2], y=[i_1, i_2])
如果要一起计算损失,请使用
Concatenate()

传递到
model.compile
的任何损失函数都将在其组合状态下应用于
输出。从预测中获得输出后,您可以将其拆分为原始状态:

f = model.predict(...)

out_1, out_2 = f[:n], f[n:]

这在Keras手册中有很好的记录。你可以在那里找到关于如何建立这样的模型的例子和解释。我已经读过了,但我仍然不清楚。我想在整个网络上运行input1得到output1,然后在同一个网络上运行input2得到output2。然后在最后计算一个综合损失。您能提供一个小的代码示例吗?#创建并编译模型model=model(输入=[in_1,in_2],输出=[out_1,out_2])模型。编译(optimizer='adam',loss='mse')在您的示例中损失是如何计算的?这两个输出是否与单个目标合并并进行评估?=>这就是我需要的。你可以定义一个自定义损失函数,但在这个例子中使用了mse。如果要组合目标,还可以在模型中使用
串联
组合目标,然后只输出一个向量。然后,您可以以任何方式拆分该向量以重建原始输出。
f = model.predict(...)

out_1, out_2 = f[:n], f[n:]