ValueError:您传递给模型的 Numpy 数组不是模型预期的大小

数据挖掘 Python 深度学习 喀拉斯 张量流
2021-10-14 19:23:00

我正在尝试在 Bidirectinal LSTM 层上执行连接。我的模型定义如下:

embedding_layer=Embedding(words,300,input_length=trainDataVecs.shape[1],weights=[embedding_matrix])

LSTM_word_1 = Bidirectional(LSTM(80, activation='tanh', dropout_W = 0.25, dropout_U = 0.25,return_sequences=True))
model1 = Input(shape=(trainDataVecs.shape[1],),dtype='int32')
embedded_sequences1=embedding_layer(model1)
lstm_word_out_1 = LSTM_word_1(embedded_sequences1)

LSTM_word_2 = Bidirectional(LSTM(80, activation='tanh', dropout_W = 0.25, dropout_U = 0.25,return_sequences=True))
model2 = Input(shape=(trainDataVecs.shape[1],),dtype='int32')
embedded_sequences2=embedding_layer(model2)
lstm_word_out_2 = LSTM_word_2(embedded_sequences2)

conc=concatenate([MaxPooling1D()(lstm_word_out_1),AveragePooling1D()(lstm_word_out_2)])

outp = Dense(classes_num, activation='sigmoid')(conc)
model=Model(input=[model1,model2],output=outp)

我适合这样:

history=model.fit(trainDataVecs,Y_train,shuffle=True,batch_size=512,epochs=10,validation_data=(valDataVecs,valid_Y))

但我收到如下错误:

ValueError: Error when checking model input: the list of Numpy 
arrays that you are passing to your model is not the size the 
model expected. Expected to see 2 array(s), but instead got the 
following list of 1 arrays

PS:训练和验证向量是数组。但我仍然收到错误消息。我认为这可能是因为模型定义,但我不确定。

1个回答

您的网络有两个输入,因此您需要传入两个数组。从其余代码中,也许您想通过两条链传递相同的数据,在这种情况下,您可以复制输入:

history=model.fit([trainDataVecs, trainDataVecs], ...