简体   繁体   English

ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(393613,50)

[英]ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (393613, 50)

I have a error in Keras and I can't find the solution. 我在Keras中出错,找不到解决方案。 I have searched the whole internet and I have still no answer ^^ Here is my Code. 我已经搜索了整个互联网,但仍然没有答案^^这是我的代码。

model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=X.values.shape))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(32))
model.add(Dense(10, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])

The error is the second line. 错误是第二行。 It says "ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (393613, 50)" The Shape of my Dataframe X is correct. 它说:“ ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(393613,50)”我的数据框X的形状正确。 And when I try to train the model the Error pops up 当我尝试训练模型时会弹出错误

model.fit(X.values, Y.values, batch_size=200, epochs=10, validation_split=0.05)

I hope someone could help me :-) 我希望有人可以帮助我:-)

[EDIT] Btw. [编辑]顺便说一句。 here is the model.summary() 这是model.summary()


Layer (type) Output Shape Param # 图层(类型)输出形状参数

lstm_1 (LSTM) (None, 393613, 32) 10624 lstm_1(LSTM)(无,393613,32)10624


lstm_2 (LSTM) (None, 393613, 32) 8320 lstm_2(LSTM)(无,393613,32)8320


lstm_3 (LSTM) (None, 32) 8320 lstm_3(LSTM)(无,32)8320


dense_1 (Dense) (None, 10) 330 density_1(密集)(无,10)330

Total params: 27,594 Trainable params: 27,594 Non-trainable params: 0 总参数:27,594可训练参数:27,594非可训练参数:0


Kind regards Niklas. 亲切的问候尼古拉斯。

While initialising the first layer you are passing 2 values as input_shape = X.values.shape 在初始化第一层时,您要传递2个值作为input_shape = X.values.shape

keras already expects number of rows per batch as NONE. keras已经期望每批的行数为NONE。 At runtime this value is determined by batch_size= (200 in your case) 在运行时,此值由batch_size= (在您的情况下为200)确定

So basically it internally changed shape of input for layer 1 as NO_OF_FEATURES, NO_OF_ROWS_IN_DATA_SET, NO_OF_ROWS_PER_BATCH 因此,基本上,它在内部将第1层的输入形状更改为NO_OF_FEATURES, NO_OF_ROWS_IN_DATA_SET, NO_OF_ROWS_PER_BATCH

To fix this all you have to do is pass 1 parameter as input_shape , which is no of features. 要解决此问题,您要做的就是将1参数作为input_shape ,该参数不包含任何功能。 Keras already accepts NONE as a placeholder for no of rows per batch. Keras已经接受NONE作为占位符,每批没有行。

So input_shape=(X.values.shape[1],) should do the trick. 因此, input_shape=(X.values.shape[1],)应该可以解决问题。

model.add(LSTM(32, return_sequences=True, input_shape=(X.values.shape[1],)))

You have made two mistakes: 您犯了两个错误:

  1. LSTM processes sequential data. LSTM处理顺序数据。 That is the input data to LSTM is 3 dimensional. 也就是说,LSTM的输入数据是3维的。 In, your 1st lstm layer you set input_shape = X.values.shape which is wrong because input_shape you have to specify number of timesteps and number of features ie input_shape = (num_time_steps,num_features). 在第一个lstm层中,您设置了input_shape = X.values.shape,这是错误的,因为input_shape必须指定时间步数和要素数量,即input_shape =(num_time_steps,num_features)。 Your rows of array are not your time steps. 您的阵列行不是您的时间步。 The rows basically indicate the batch number. 这些行基本上表示批号。 As far as syntax is concerned, this is still right and you will not get any error while compiling the code. 就语法而言,这仍然是正确的,并且在编译代码时不会出现任何错误。

  2. You have to reshape your data set so that it is 3 dimensional ie batch_size,time_step,feature. 您必须调整数据集的形状,使其为3维,即batch_size,time_step,feature。 You can easily that using numpy.reshape as follows: 您可以使用numpy.reshape轻松地进行如下操作:

    X_train_arr = numpy.reshape(X.values,(X.values.shape[0],1,X.values.shape[1]))

    This is assuming you have only one time steps. 这是假设您只有一个时间步长。 if you have more time steps than you will need to do more tweaking. 如果您有更多的时间步长,则需要进行更多的调整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(10,1) - ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (10, 1) 检查输入时出错:预期 lstm_1_input 有 3 个维度,但得到形状为 (5, 3) 的数组 - Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (5, 3) 检查模型输入时发生错误:预期lstm_1_input具有3维,但数组的形状为(339732,29) - Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29) ValueError:检查输入时出错:预期lstm_1_input具有形状(973,215),但数组的形状为(61,215) - ValueError: Error when checking input: expected lstm_1_input to have shape (973, 215) but got array with shape (61, 215) 检查输入时出错:预期 lstm_1_input 具有 3 个维度,但仅在 epoch>1 和特定数据集拆分时获得形状为 (0, 1) 的数组 - Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (0, 1) only on epoch>1 and at a specific dataset split ValueError:检查输入时出错:预期 input_1 有 5 个维度,但得到形状为 (1221, 50, 50, 1) 的数组 - ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (1221, 50, 50, 1) 预期 lstm_1_input 有 3 个维度,但得到了形状为 (0, 1) 的数组 - expected lstm_1_input to have 3 dimensions, but got array with shape (0, 1) !! ValueError:检查输入时出错:预期lstm_2_input具有3个维,但数组的形状为(4982,12) - !! ValueError: Error when checking input: expected lstm_2_input to have 3 dimensions, but got array with shape (4982, 12) 检查输入时出错:预期 lstm_1_input 有 3 个维度 - Error when checking input: expected lstm_1_input to have 3 dimensions 检查输入时出错:预期 lstm_1_input 的形状为 (71, 768) 但得到的数组形状为 (72, 768) - Error when checking input: expected lstm_1_input to have shape (71, 768) but got array with shape (72, 768)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM