Keras模型在调用compile后仍要求编译

我有一个简单的Keras模型:

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'])

当调用fit()时,代码会报错:

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

显然,我已经调用了compile。我该如何解决这个问题呢?


回答:

看起来问题在于你创建了3个Sequential模型的实例,但只编译了第三个(合并后的)。对于多模态网络,可能使用不同的结构会更简单:

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'])

希望这对你有帮助!

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注