输入形状为[512,64],[5739,64]的seq2seq模型(操作:“添加")中的尺寸错误 [英] Dimensions error, in seq2seq model (op: 'Add') with input shapes: [512,64], [5739,64]

查看:73
本文介绍了输入形状为[512,64],[5739,64]的seq2seq模型(操作:“添加")中的尺寸错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在seq2seq模型中,编码器last应该是输入的初始状态.

In seq2seq model, encoder last should be initial state of input.

但是我遇到尺寸错误.

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

the shape of decoder and initial_state belows

(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层.

and i connected decoder and initial_state to LSTM layer.

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层连接起来.

i know dimensions have to be equal, but i can't understand what's mean of value error said. also i wonder how can i connect decoder and initial_state with LSTM layer.

推荐答案

In the official Keras blog, there is a thorough tutorial on seq2seq model.

您可以使用以下代码轻松地通过 encoder-state 来初始化解码器:

You can easily pass encoder-state for initializing decoder with the following code:

# 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).

As you can see there is no need to use the Lambda layer. Just set the return_state flag to True and pass the state to the decoder constructor (initial_state).

这篇关于输入形状为[512,64],[5739,64]的seq2seq模型(操作:“添加")中的尺寸错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆