作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个组合模型,该模型一次将两个不同的图像传递给一个子模型(编码器),然后将两个结果合并并将它们提供给最终的子模型,该模型基于在这两个潜在表示上。我想对两个图像使用相同的编码器以减少训练时间,因为如果我需要对图像进行编码,我肯定只需要一个编码器? (我应该注意到图像是相似的)。
不过,在制作了编码器和最终的子模型之后,我尝试使用以下行制作最终的组合模型:
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 中是否可以在一个模型中使用两次相同的子模型,或者我是否必须为我使用的 2 种不同类别的图像使用单独的编码器?
最佳答案
假设您有一个使用以下函数构建的模型:
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
层来为同一个编码器提供单独的输入。
关于python - 如何在 Keras 的一个模型中两次使用相同的层/模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60094332/
我是一名优秀的程序员,十分优秀!