tstseby
tstseby

Reputation: 1319

Keras Model asks for compiling even after compile call

I have a simple Keras model:

model_2 = Sequential()
model_2.add(Dense(32, input_shape=(500,)))
model_2.add(Dense(4))
#answer = concatenate([response, question_encoded])

model_1 = Sequential()
model_1.add(LSTM(32, dropout_U = 0.2, dropout_W = 0.2, return_sequences=True, input_shape=(None, 2048)))
model_1.add(LSTM(16, dropout_U = 0.2, dropout_W = 0.2, return_sequences=False))
#model.add(LSTM(16, return_sequences=False))

merged = Merge([model_1, model_2])
model = Sequential()
model.add(merged)
model.add(Dense(8, activation='softmax'))

#model.build()

#print(model.summary(90))
print("Compiled")
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

The code fails with the error when calling fit():

    raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.

Clearly, I have called compile. How could I resolve the error?

Upvotes: 0

Views: 70

Answers (1)

nickyfot
nickyfot

Reputation: 2019

It looks like the problem is that you are creating 3 instances of the Sequential model but only compile the 3rd one (the merged). It might be easier to use a different structure for a multi-modal network:

input_2 = Input(shape=(500,))
model_2 = Dense(32)(input_2 )
model_2 = Dense(4)(model_2)

input_1 = Input(shape=(None, 2048))
model_1 = LSTM(32, dropout_U = 0.2, dropout_W = 0.2, return_sequences=True)(input_1 )
model_1 = LSTM(16, dropout_U = 0.2, dropout_W = 0.2, return_sequences=False)(model_1)

merged = concatenate([model_2, model_1])
merged = Dense(8, activation='softmax')(merged)

model = Model(inputs=[input_2 , input_1], outputs=merged)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Hope this helps!

Upvotes: 1

Related Questions