python - How to use the same layer/model twice in one model in Keras?
问题描述
I am trying to make a combined model which passes two different images through a sub-model (an encoder) one at a time and then contacenates the two results and feeds them to a final sub-model, which makes a decision based on these two latent representations. I want to use the same encoder for both images to reduce training time, since surely if I need to encode the images I only need one encoder? (I should note that the images are similar).
After making the encoder and final sub-model, though, I tried making the final combined model with the following line:
combinedModel = keras.Model(inputs=[encoder.input, encoder.input], outputs=finalSubModel)
Keras didn't like me using the same model twice like this though and it gave me the following error:
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>]
Is it possible to use the same submodel twice in one model at all in keras, or will I have to use seperate encoders for the 2 different classes of images im using?
解决方案
假设您有一个使用以下函数构建的模型:
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
层为同一个编码器提供单独的输入。
推荐阅读
- seo - AMP 故事在 Google AMP 验证中被标记为 noindex,但 AMP 故事中没有 noindex 标签
- sql - 包含 CLR 的 CTE 的 Sql 服务器查询在加入超过 5 个表后返回空字符串
- xml-parsing - TYPO3 从文件中流体解码 XML
- angular - 在调用 AppComponent.ngOnInit 之前加载数据
- postgresql - postgresql全文搜索,把自然数“01”当作“1”
- typescript - 打字稿:组合类型上不存在属性?
- command-line - 用更好的压缩版本替换我所有的旧 xcf 文件
- react-native - 在 inputToolbar 下 React-native-gifted-chat 改变颜色
- wordpress - 双击收音机,切换数值
- r - 在 dplyr 链中使用 if_else/summarize 添加数千列?