我已经有一个具有多个输入层的模型。现在,我想在这些输入图层之一之前添加图层。我该怎么办?
# this is my current model, it has two inputs m1_in and aux_in
model = Model(inputs=[m1_in, aux_in], outputs=[m2_out])
# build some new layers
x = Input(shape=(32,))
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
output = Dense(1, activation='sigmoid', name='main_output')(x)
# now I want feed the result of the output layer to the input layer m1_in
我的问题是我不能在这里使用功能性API,例如new_output = model(output)
,因为model
有多个输入。我无法指定要连接的输入层。
答案 0 :(得分:0)
您需要为新图层创建一个模型,定义新的输入,然后定义一个新的组合模型。确保不丢失原始图层(x
)的输入。
# Model for new layers
comb_model = Model(x, output)
# New inputs
new_m1_in = Input(...)
new_aux_in = Input(...)
# Apply new model for new layers
comb_m1 = comb_model(new_m1_in)
# Apply old model with new model input
final_out = model(comb_m1, new_aux_in)
# Build final model
final_model = Model([new_m1_in, new_aux_in], final_out)
希望它能起作用。这就是功能性API的优点:)
答案 1 :(得分:0)
首先,您应将所有图层都命名为x
,但将相关名称命名为dense_1
我没有低估您想要的东西,但也许像这样的事情
# this is my current model
# build some new layers
in_ = Input(shape=(32,))
dense_1 = Dense(64, activation='relu')(in_)
dense_2 = Dense(64, activation='relu')(dense_1)
dense_3 = Dense(64, activation='relu')(dense_2)
dense_4 = Dense(1, activation='sigmoid', name='main_output')(dense_3)
model = Model(inputs=[m1_in, aux_in, in_], outputs=[dense_4])
或最后一行
model = Model(inputs=[in_], outputs=[dense_4])
但是您应该更具体些吗?