seq2seq模型中的Keras ValueError,“预期看到2个数组,但得到了以下1个数组的列表”

时间:2018-12-19 16:59:13

标签: python arrays keras concatenation lstm

我正在为聊天机器人使用seq2seq模型,当我运行它时,出现以下错误:

  

ValueError:检查模型输入时出错:Numpy数组的列表   您传递给模型的信息不是模型期望的大小。   预期会看到2个数组,但得到以下1个列表   数组:[array([[[1016,627,1016,...,0,0,0],          [798,287,422,...,0,0,0],          [777,0,0,...,0,0,0],          ...,          [1863,709,370,...,0,...

我怀疑这与“连接”层有关,因此我尝试在没有额外括号的情况下提供被串联的两个项目,并将它们放在括号中的对直接传送到下一个Dense层,但没有一个起作用。我输入的数据是表示单词的整数序列,这些单词被共享的Embedding层映射到Word2Vec向量空间。

padded_context = sequence.pad_sequences(context,maxlen=maxlen_input,padding='post',truncating='post')
padded_replies = sequence.pad_sequences(replies,maxlen=maxlen_input,padding='post',truncating='post')

input_context = Input(shape=(maxlen_input,),dtype='int32',name='input_context')
input_reply = Input(shape=(maxlen_input,),dtype='int32',name='input_reply')

LSTM_encoder = LSTM(sentence_embedding_size,init='lecun_uniform')
LSTM_decoder = LSTM(sentence_embedding_size,init='lecun_uniform')

Shared_Embedding = Embedding(output_dim=word_embedding_size, input_dim=vocab_size,weights=[embedding_mtx])

word_embedding_context = Shared_Embedding(input_context)
context_embedding = LSTM_encoder(word_embedding_context)

word_embedding_reply = Shared_Embedding(input_reply)
reply_embedding = LSTM_decoder(word_embedding_reply)

merge_layer = Concatenate(axis=1)([context_embedding,reply_embedding])
out = Dense(vocab_size/2,activation='relu')(merge_layer)
out = Dense(vocab_size,activation='softmax')(out)

model = Model(input=[input_context,input_reply],output=[out])

model.compile(loss='categorical_crossentropy',optimizer='Adam')

print model.summary()

model.fit(x=padded_context,y=padded_replies,batch_size=32,epochs=10)

我也尝试通过Keras文档追踪错误回溯,但是什么也做不了。如果有帮助,这里也是模型摘要:

enter image description here

0 个答案:

没有答案