使用Keras和seq2seq模型无法调用'Tensor'对象

时间:2019-02-24 14:21:49

标签: python tensorflow keras

我一直遵循这个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,
)

现在,它工作正常!

1 个答案:

答案 0 :(得分:0)

在本节中提到解决方案(即使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,
)