Python 多维数组作为嵌入层的输入,Keras中的LSTM得到嵌入层的维数误差
我有一个多维数组X_train,形状(1000,3162)如下:Python 多维数组作为嵌入层的输入,Keras中的LSTM得到嵌入层的维数误差,python,tensorflow,multidimensional-array,keras,Python,Tensorflow,Multidimensional Array,Keras,我有一个多维数组X_train,形状(1000,3162)如下: [[[ 0 0 0 ... 0 0 0] [ 0 0 0 ... 0 0 0] [22175 0 0 ... 0 0 0]] [[ 0 0 0 ... 0 0 0] [22175 0
[[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[22175 0 0 ... 0 0 0]]
[[ 0 0 0 ... 0 0 0]
[22175 0 0 ... 0 0 0]
[37363 39010 0 ... 0 0 0]]
[[22175 0 0 ... 0 0 0]
[37363 39010 0 ... 0 0 0]
[31559 42695 0 ... 0 0 0]]
...
[[20651 31559 43650 ... 0 0 0]
[20651 12733 40329 ... 0 0 0]
[31559 15394 31559 ... 0 0 0]]]
main_input = Input(shape=(162,)) # only pass in the indexes
emb = Embedding(input_dim=3, output_dim = 162)(main_input)
doc_output = Bidirectional(LSTM(64, return_sequences=False))(emb)
doc_output = Dense(units=43, activation='softmax')(doc_output)
model = Model(inputs=main_input, outputs=doc_output)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
history=model.fit(X_train,y_train,epochs=1,batch_size=128,validation_data=(X_test, y_test))
我的模型如下所示:
[[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[22175 0 0 ... 0 0 0]]
[[ 0 0 0 ... 0 0 0]
[22175 0 0 ... 0 0 0]
[37363 39010 0 ... 0 0 0]]
[[22175 0 0 ... 0 0 0]
[37363 39010 0 ... 0 0 0]
[31559 42695 0 ... 0 0 0]]
...
[[20651 31559 43650 ... 0 0 0]
[20651 12733 40329 ... 0 0 0]
[31559 15394 31559 ... 0 0 0]]]
main_input = Input(shape=(162,)) # only pass in the indexes
emb = Embedding(input_dim=3, output_dim = 162)(main_input)
doc_output = Bidirectional(LSTM(64, return_sequences=False))(emb)
doc_output = Dense(units=43, activation='softmax')(doc_output)
model = Model(inputs=main_input, outputs=doc_output)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
history=model.fit(X_train,y_train,epochs=1,batch_size=128,validation_data=(X_test, y_test))
我得到了这个错误
检查输入时出错:预期输入_7有2个维度,但得到了具有形状的数组(1000,3,162)
模型的摘要在这里
当列车和测试数据是三维数据时,如何添加此输入数组以将其送入嵌入层?在拟合之前,您可以共享模型摘要吗?我已在帖子中添加了摘要。根据此代码,您只希望您的输入层获得162维向量,这应该更改,这就是形状冲突的原因。你能指导我如何更改输入层形状吗?尝试将main_input=input(shape=(162,))更改为main_input=input(shape=(3162))你能在拟合之前共享模型摘要吗?我在帖子中添加了摘要。根据此代码,你只希望你的输入层得到162维向量,这应该改变,这就是形状冲突的原因。你能告诉我如何改变输入层的形状吗?试着把main_input=input(shape=(162,))改为main_input=input(shape=(3162))