Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in AI and Deep Learning by (120 points)

Hi. I have a code which is concatenating two models. The code used the old version of Keras. I am using Keras 2.2.4. I am trying to concatenate both the models but I am getting an error while training. And if I use the pre-train model and try to execute the test file I get the following error:

ValueError: You are trying to load a weight file containing 6 layers into a model with 0 layers.

Because of this error, I am assuming that I am not concatenating the models correctly. Here is my code snippet. How do I concatenate my model using Keras 2.2.4 on line 

"model.add(Merge([image_model, lang_model], mode='concat'))"      .... Below is the complete model code snippet. 

def create_model(self, ret_model = False):

        image_model = Sequential()

        image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))

        image_model.add(RepeatVector(self.max_length))

        lang_model = Sequential()

        lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_length))

        lang_model.add(LSTM(256,return_sequences=True))

        lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))

        model = Sequential()

        model.add(Merge([image_model, lang_model], mode='concat'))

        model.add(LSTM(1000,return_sequences=False))

        model.add(Dense(self.vocab_size))

        model.add(Activation('softmax'))

        print ("Model created!")

        if(ret_model==True):

            return model

      model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

      return model

Here is my code snippet in pic: How do I concatenate my model using Keras 2.2.4 on line 103.
 

This is my model code snippet pic. Please help me out with it.

1 Answer

0 votes
by (50.2k points)
Just remove the InputLayer as it seems that it is not serialized to HDF5 correctly.

And also Merge(mode='concat') is now concatenate(axis=-1).

You want to merge models not layers, this will not work in your situation. Just use the functional model as this behavior has no longer stayed with the basic Sequential model type.
How to concatenate two models in Keras 2.2.4?
Intellipaat-community
by (120 points)
https://dpaste.de/dWSy  (this is my train.py file)   and     https://dpaste.de/7MNH   (this is my file where I create the model. the file name is "SceneDesc.py").

I can't seem to remove the error.
by (50.2k points)
Instead of using InputLayer you can use input shape like the follows:
model = Sequentional()
model.add(Conv2D(filters=20,..., input_shape=(36, 120, 1)))
and for concatenating two models you can refer the following syntax:
concatenated = concatenate([model1_out, model2_out])
out = Dense(1, activation='softmax', name='output_layer')(concatenated)
by (120 points)
Ashely can you please properly write it for me the code.. I mean the model. I am getting a bit confused. As i am a beginner to all this.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...