在seq2seq模型中,编码器last应该是输入的初始状态。
但是我遇到尺寸错误。
inp=Input(shape=(train_X.shape[0], train_X.shape[1]))
inp=tf.reshape(inp,[train_X.shape[0],train_X.shape[1]])
print(inp.shape)
encoder = Embedding(input_dim=8,output_dim=64, mask_zero=True,embeddings_initializer='uniform')(inp)
encoder = LSTM(64, input_shape=train_X.shape, return_sequences=True, unroll=True)(encoder)
encoder_last = Lambda(lambda x: x[:,-1,:])(encoder)
print(encoder_last.shape)
encoder=tf.reshape(encoder,[-1,5739])
print(encoder)
结果
(5739, 8)
(5739, 64)
Tensor("Reshape_42:0", shape=(512, 5739), dtype=float32)
和解码器
decoder = Embedding(8,64,mask_zero=True)(encoder)
print(decoder.shape)
initial_state=[encoder_last, encoder_last]
print(initial_state)
解码器的形状和下面的initial_state
(512, 5739, 64)
[<tf.Tensor 'lambda_22/strided_slice:0' shape=(5739, 64) dtype=float32>, <tf.Tensor 'lambda_22/strided_slice:0' shape=(5739, 64) dtype=float32>]
我将解码器和initial_state连接到LSTM层。
decoder = LSTM(64, input_shape= encoder_last.shape, return_sequences=True, unroll=True)(decoder, initial_state)
说
ValueError: Dimensions must be equal, but are 512 and 5739 for 'lstm_93/add' (op: 'Add') with input shapes: [512,64], [5739,64].
我知道尺寸必须相等,但是我不明白值错误的意思是什么。我也想知道如何将解码器和initial_state与LSTM层连接起来。
答案 0 :(得分:0)
在official Keras blog中,有关于seq2seq模型的详尽教程。
您可以使用以下代码轻松地通过 encoder-state 来初始化解码器:
# encoder
x = Embedding(num_encoder_tokens, latent_dim)(encoder_inputs)
x, state_h, state_c = LSTM(latent_dim, return_state=True)(x)
encoder_states = [state_h, state_c]
# decoder
decoder_inputs = Input(shape=(None,))
x = Embedding(num_decoder_tokens, latent_dim)(decoder_inputs)
x = LSTM(latent_dim, return_sequences=True)(x, initial_state=encoder_states)
如您所见,无需使用Lambda
层。只需将return_state
标志设置为 True 并将状态传递给解码器构造函数(initial_state
)。