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

时间:2018-02-23 15:38:29

标签: python tensorflow machine-learning keras

我在Keras遇到错误,但我无法找到解决方案。我搜索了整个互联网,我仍然没有回答^^ 这是我的代码。

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'])

错误是第二行。它说" ValueError:检查输入时出错:预期lstm_1_input有3个维度,但是有阵列形状(393613,50)" 我的Dataframe X的形状是正确的。 当我尝试训练模型时,弹出错误

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

我希望有人可以帮助我: - )

[编辑] 顺便说一句。这是model.summary()

图层(类型)输出形状参数#

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

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

lstm_3(LSTM)(无,32)8320

dense_1(密集)(无,10)330

总参数:27,594 可训练的参数:27,594 不可训练的参数:0

亲切的问候尼克拉斯。

2 个答案:

答案 0 :(得分:1)

初始化第一个图层时,您传递2个值为input_shape = def Interp(datain,xin,yin,xout,yout,interpolation='NearestNeighbour'): """ Interpolates a 2D array onto a new grid (only works for linear grids), with the Lat/Lon inputs of the old and new grid. Can perfom nearest neighbour interpolation or bilinear interpolation (of order 1)' This is an extract from the basemap module (truncated) """ # Mesh Coordinates so that they are both 2D arrays xout,yout = np.meshgrid(xout,yout) # compute grid coordinates of output grid. delx = xin[1:]-xin[0:-1] dely = yin[1:]-yin[0:-1] xcoords = (len(xin)-1)*(xout-xin[0])/(xin[-1]-xin[0]) ycoords = (len(yin)-1)*(yout-yin[0])/(yin[-1]-yin[0]) xcoords = np.clip(xcoords,0,len(xin)-1) ycoords = np.clip(ycoords,0,len(yin)-1) # Interpolate to output grid using nearest neighbour if interpolation == 'NearestNeighbour': xcoordsi = np.around(xcoords).astype(np.int32) ycoordsi = np.around(ycoords).astype(np.int32) dataout = datain[ycoordsi,xcoordsi] # Interpolate to output grid using bilinear interpolation. elif interpolation == 'Bilinear': xi = xcoords.astype(np.int32) yi = ycoords.astype(np.int32) xip1 = xi+1 yip1 = yi+1 xip1 = np.clip(xip1,0,len(xin)-1) yip1 = np.clip(yip1,0,len(yin)-1) delx = xcoords-xi.astype(np.float32) dely = ycoords-yi.astype(np.float32) dataout = (1.-delx)*(1.-dely)*datain[yi,xi] + \ delx*dely*datain[yip1,xip1] + \ (1.-delx)*dely*datain[yip1,xi] + \ delx*(1.-dely)*datain[yi,xip1] return dataout

keras已经预计每批次的行数为NONE。在运行时,此值由X.values.shape(在您的情况下为200)

确定

所以基本上它在内部改变了第1层的输入形状 batch_size=

要解决此问题,您只需将1个参数作为NO_OF_FEATURES, NO_OF_ROWS_IN_DATA_SET, NO_OF_ROWS_PER_BATCH传递,这不是功能。 Keras已经接受input_shape作为占位符,每批次没有行。

所以NONE应该做到这一点。

input_shape=(X.values.shape[1],)

答案 1 :(得分:0)

您犯了两个错误:

  1. LSTM处理顺序数据。也就是说,LSTM的输入数据是3维的。在第一个lstm层中,您设置了input_shape = X.values.shape,这是错误的,因为input_shape必须指定时间步数和要素数量,即input_shape =(num_time_steps,num_features)。您的阵列行不是您的时间步。这些行基本上表示批号。就语法而言,这仍然是正确的,并且在编译代码时不会出现任何错误。

  2. 您必须调整数据集的形状,使其为3维,即batch_size,time_step,功能。您可以使用numpy.reshape轻松地进行如下操作:

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

    这是假设您只有一个时间步长。如果您的时间步长超过 您将需要做更多的调整。