gpt4 book ai didi

python - “Tensor”对象不能使用 Keras 和 seq2seq 模型调用

转载 作者:行者123 更新时间:2023-12-05 07:25:23 26 4
gpt4 key购买 nike

我在关注这个 tutorial ,我可以像这样编译和训练我的模型:

encoder_inputs = Input(shape=(None,))
encoder_embedding = Embedding(max_words, latent_dim)(encoder_inputs)
encoder_lstm, state_h, state_c = LSTM(latent_dim, return_state=True)(encoder_embedding)
encoder_states = [state_h, state_c]

decoder_inputs = Input(shape=(None,))
decoder_embedding = Embedding(max_words, latent_dim)(decoder_inputs)
decoder_lstm = LSTM(latent_dim, return_sequences=True)(decoder_embedding, initial_state=encoder_states)
decoder_outputs = Dense(decoder_target_data_size, activation='softmax')(decoder_lstm)

model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit([X_train, Y_train], decoder_target_data, batch_size=128, epochs=100,validation_split=0.2, verbose=0)

但是当我想做推理部分的时候

encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_input_h = Input(shape=(None,))
decoder_outputs, decoder_state_h = decoder_lstm(encoder_embedding, initial_state=decoder_state_input_h)
decoder_outputs = decoder_dense(decoder_outputs)

我收到以下错误:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-c87095c25663> in <module>
3 decoder_state_input_h = Input(shape=(None,), name="DecoderStateInput_1")
4
----> 5 decoder_outputs, decoder_state_h = decoder_lstm(encoder_embedding, initial_state=decoder_state_input_h)
6 decoder_outputs = decoder_dense(decoder_outputs)
7

TypeError: 'Tensor' object is not callable

我确定这是关于 Functional API 的问题,但我不知道如何解决它。

我正在使用 keras-2.2.4tensorflow-1.10.0

如果有人能帮助我,我将不胜感激!

编辑

我解决了这个问题:

encoder_model = Model(encoder_inputs, encoder_states)

decoder_hidden_state_inputs = Input(shape=(latent_dim,))
decoder_cell_state_inputs = Input(shape=(latent_dim,))
decoder_state_inputs = [decoder_hidden_state_inputs, decoder_cell_state_inputs]

decoder_lstm_outputs, decoder_hidden_state, decoder_cell_state = decoder_lstm(inputs=decoder_embedding(decoder_inputs),
initial_state=decoder_state_inputs,
)
decoder_state = [decoder_hidden_state, decoder_cell_state]

decoder_outputs = decoder_dense(inputs=decoder_lstm_outputs)

decoder_model = Model(
inputs=[decoder_inputs] + decoder_state_inputs,
outputs=[decoder_dense_outputs] + decoder_state,
)

现在,一切正常!

最佳答案

为了社区的利益,在本节中提及解决方案(即使是 Matias Aravena Gamboa 在问题中提到的)。

问题已使用下面提到的代码解决:

encoder_model = Model(encoder_inputs, encoder_states)

decoder_hidden_state_inputs = Input(shape=(latent_dim,))
decoder_cell_state_inputs = Input(shape=(latent_dim,))
decoder_state_inputs = [decoder_hidden_state_inputs, decoder_cell_state_inputs]

decoder_lstm_outputs, decoder_hidden_state, decoder_cell_state = decoder_lstm(inputs=decoder_embedding(decoder_inputs),
initial_state=decoder_state_inputs,
)
decoder_state = [decoder_hidden_state, decoder_cell_state]

decoder_outputs = decoder_dense(inputs=decoder_lstm_outputs)

decoder_model = Model(
inputs=[decoder_inputs] + decoder_state_inputs,
outputs=[decoder_dense_outputs] + decoder_state,
)

关于python - “Tensor”对象不能使用 Keras 和 seq2seq 模型调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54852820/

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