gpt4 book ai didi

python - 即使在编译调用之后,Keras 模型也要求编译

转载 作者:行者123 更新时间:2023-11-30 09:43:54 25 4
gpt4 key购买 nike

我有一个简单的 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 模型实例,但只编译第 3 个实例(合并后的实例)。对于多模式网络使用不同的结构可能会更容易:

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

希望这有帮助!

关于python - 即使在编译调用之后,Keras 模型也要求编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55161452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com