meme mimis
meme mimis

Reputation: 81

Keras encoder-decoder model RuntimeError: You must compile your model before using it

I am trying to reproduce the results of an image captioning model but I get this error. The code for the two models is the following:

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(Concatenate([image_model, lang_model]))
model.add(LSTM(1000, return_sequences=False))
model.add(Dense(self.vocab_size))
model.add(Activation('softmax'))

print ("Model created!")
model.compile(loss='categorical_crossentropy', 
optimizer='rmsprop', metrics=['accuracy'])

The model is the then called by the following code:

sd = SceneDesc.scenedesc()
model = sd.create_model()
batch_size = 512
model.fit_generator(sd.data_process(batch_size=batch_size), 
    steps_per_epoch=sd.no_samples/batch_size, epochs=epoch, verbose=2, 
    callbacks=None)

However, when the fit_generator is called that particular error is raised. Is there anything wrong with the concatenation of the models?

Upvotes: 3

Views: 823

Answers (2)

a.deshpande012
a.deshpande012

Reputation: 744

In keras, there is a concept called compiling your model.

Basically, this configures the loss function and sets the optimizer for the model you want to train.

For example, model.compile(loss='mse',optimizer='Adam') will configure your model to use the mse loss function, and to use the Adam optimization algorithm. What you use instead of these will heavily depend on the type of problem.

The reason your code throws an error is because the model cannot train, since you have not configured the loss function and optimizer using the compile method. Simple call model.compile() with your choice of loss function and optimizer, and then you will be able to train your model.

Upvotes: 1

CAta.RAy
CAta.RAy

Reputation: 514

You need to call the method model.compile(loss, optimizer) before you can fit it.

Upvotes: 0

Related Questions