Keras编码器-解码器模型RuntimeError:使用模型之前,必须先对其进行编译 [英] Keras encoder-decoder model RuntimeError: You must compile your model before using it

查看:892
本文介绍了Keras编码器-解码器模型RuntimeError:使用模型之前,必须先对其进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重现图像字幕模型的结果,但出现此错误.这两个模型的代码如下:

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)

但是,当调用fit_generator时,会引发特定错误.模型的串联有什么问题吗?

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

推荐答案

在keras中,有一个称为编译模型的概念.

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.

例如,model.compile(loss='mse',optimizer='Adam')将配置模型以使用 mse 损失函数,并使用 Adam 优化算法.您使用的替代方法在很大程度上取决于问题的类型.

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.

您的代码引发错误的原因是因为无法训练模型,因为您尚未使用compile方法配置损失函数和优化器.只需选择损失函数和优化器,即可调用model.compile(),然后就可以训练模型.

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.

这篇关于Keras编码器-解码器模型RuntimeError:使用模型之前,必须先对其进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆