Keras: concatenate models (not layers)

Created on 19 Jun 2016  路  3Comments  路  Source: keras-team/keras

I've written a model and I want to plug it as the input to another model that is exactly the same. Running by intuition I wrote the following:

model_half = Model(input=inputs, output=conv4)
model = Sequential([model_half, model_half])

which compiles fine but as soon as the code starts fitting I get the following ValueError:

ValueError: ('this shared variable already has an update expression', (convolution2d_28_W, Elemwise{sub,no_inplace}.0))

Just to make it clear, the code runs perfectly when model = model_half and output dimensions match input. Is there a systematic, clean way to do what I'm trying to do? Is there a reason why my approach should simply not work?

Thanks a lot in advance for any help. I think there's no need for more details from my code but I can upload it on GitHub if needed.

Most helpful comment

@AdityaGudimella! Thanks a lot, I found it finally. As you said, the info in the functional API was enough. I was inspired by the "Shared vision model" in

http://keras.io/getting-started/functional-api-guide/

Basically I define first the link_model which has its own input_to_link modelled and then I define the chain_model with a new input_to_chain layer (the key aspect that was missing in my previous attempt)

    #______________________________________________________ link definition
    input_to_link = Input((1, img_rows, img_cols))
    ...several convolutions...
    link_model = Model(input=input_to_link, output=conv4)   # a chain is made of links

    #______________________________________________________ chain definition
    input_to_chain = Input((1, img_rows, img_cols))
    output_link_1 = link_model(input_to_chain)
    output_link_2 = link_model(output_link_1)
    chain_model = Model(input=input_to_chain, output=output_link_2)

All 3 comments

You can use the functional api for this.

Hei @AdityaGudimella! I checked and I couldn't find an answer, probably it's because I'm kind of new to keras as well. Can you point me to where in the API specifically I can have a look at?

Thanks for the answer ;)

@AdityaGudimella! Thanks a lot, I found it finally. As you said, the info in the functional API was enough. I was inspired by the "Shared vision model" in

http://keras.io/getting-started/functional-api-guide/

Basically I define first the link_model which has its own input_to_link modelled and then I define the chain_model with a new input_to_chain layer (the key aspect that was missing in my previous attempt)

    #______________________________________________________ link definition
    input_to_link = Input((1, img_rows, img_cols))
    ...several convolutions...
    link_model = Model(input=input_to_link, output=conv4)   # a chain is made of links

    #______________________________________________________ chain definition
    input_to_chain = Input((1, img_rows, img_cols))
    output_link_1 = link_model(input_to_chain)
    output_link_2 = link_model(output_link_1)
    chain_model = Model(input=input_to_chain, output=output_link_2)
Was this page helpful?
0 / 5 - 0 ratings