为什么我在Keras中遇到尺寸错误的错误?

时间:2018-10-12 20:05:39

标签: python keras

我正在尝试在keras中创建一个简单的ANN模型。但是,出现以下错误。

Error when checking target: expected dense_8 to have 4 dimensions, but got array with shape (36069, 1)

这是我的模型和输入数据。

x = x.reshape(48074,1,18,1)
x_train = x[0:36069]
x_val = x[36069:38472]
x_test = x[38472:48074]
y_train = y[0:36069]#36069
y_val = y[36069:38472]
y_test = y[38472:48074]
model = Sequential()
model.add(Dense(50),input_shape=(1,18,1))
model.add(Dense(25))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(x_train,y_train, epochs=200, batch_size=10, verbose=1,
          validation_data=(x_val, y_val))

我尝试了model.summary(),但是找不到我的错误。

dense_9 (Dense)              (None, 1, 18, 50)         100       
_________________________________________________________________
dense_10 (Dense)             (None, 1, 18, 25)         1275      
_________________________________________________________________
dense_11 (Dense)             (None, 1, 18, 1)          26 

1 个答案:

答案 0 :(得分:0)

您应该对数据进行展平,因为默认情况下,“密集”层将仅对数据的最后一个维度进行操作。

x = x.reshape(48074,1 * 18 * 1)
x_train = x[0:36069]
x_val = x[36069:38472]
x_test = x[38472:48074]
y_train = y[0:36069]#36069
y_val = y[36069:38472]
y_test = y[38472:48074]

model = Sequential()
model.add(Dense(50),input_shape=(1 * 18 * 1,))
model.add(Dense(25))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(x_train,y_train, epochs=200, batch_size=10, verbose=1,
          validation_data=(x_val, y_val))