首页 > 解决方案 > 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?

标签: pythontensorflowkerasdeep-learning

解决方案


假设您有一个使用以下函数构建的模型:

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层为同一个编码器提供单独的输入。


推荐阅读