为什么我在 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 

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

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

最新更新