我正在尝试创建一个组合模型,该模型一次将两个不同的图像通过一个子模型(一个编码器)传递,然后污染这两个结果,并将它们提供给最终的子模型,然后根据这两个模型做出决定潜在表示。我想对两个图像使用相同的编码器以减少训练时间,因为如果我确实需要对图像进行编码,我肯定只需要一个编码器吗?(我应该注意图像是相似的)。
但是,在制作了编码器和最终子模型之后,我尝试使用以下代码制作最终的组合模型:
combinedModel = keras.Model(inputs=[encoder.input, encoder.input], outputs=finalSubModel)
Keras并不喜欢我两次使用相同的模型,这给了我以下错误:
ValueError: The list of inputs passed to the model is redundant. All inputs should only appear once. Found: [<tf.Tensor 'input_2:0' shape=(None, 32, 32, 1) dtype=float32>, <tf.Tensor 'input_2:0' shape=(None, 32, 32, 1) dtype=float32>]
是否可以在keras中在一个模型中一次使用同一子模型两次,还是必须为im使用的两种不同类别的图像使用单独的编码器?
假设您具有使用以下功能构建的模型:
def make_encoder(h, w, c):
inp = Input((h, w, c))
x = SomeLayer()(inp)
x = SomeLayer()(x)
....
out = OutLayer()(x)
return Model(inputs=[inp], outputs=[out])
现在,要创建组合模型,您需要Input
为每个调用使用相同的编码器,并在不同的层上使用。要了解最后一行,请看以下内容:
def make_combined(h, w, c):
inp1 = Input((h, w, c))
inp2 = Input((h, w, c))
encoder = make_encoder(h, w, c)
encoded_1 = encoder(inp1)
encoded_2 = encoder(inp2)
# Concatenate the result
encoded_out = Concatenate()([encoded_1, encoded_2])
return Model(inputs=[inp1, inp2], outputs=[encoded_out])
注意,我正在使用两个不同的虚拟Input
层为同一编码器提供单独的输入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句