我在 Keras 中使用编码器解码器 seq2seq 架构,我正在传递一个单热的形状数组(num_samples、max_sentence_length、max_words)进行训练,并使用教师强制。
#Encoder
latent_dim = 256
encoder_inputs = Input(shape=(None, max_words))
encoder = LSTM(latent_dim, return_state = True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
#Decoder
decoder_inputs = Input(shape=(None, max_words))
decoder_lstm = LSTM(latent_dim, return_state = True, return_sequences =
True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=
encoder_states)
decoder_dense = Dense(max_words, activation = 'softmax')
decoder_outputs = decoder_dense(decoder_outputs)
对于推理模型:
# Inference model
encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_input_h = Input(shape=(latent_dim,))
decoder_state_input_c = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(
decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs] + decoder_states)
我尝试打印出 encoder_model 状态,但它总是为任何输入返回相同的状态。任何帮助,将不胜感激!