Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 621
meme mimis Avatar asked Dec 06 '25 04:12

meme mimis


1 Answers

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.

like image 68
a.deshpande012 Avatar answered Dec 08 '25 16:12

a.deshpande012



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!