Dataset shape mismatch Conv1D input layer

心不动则不痛 提交于 2021-02-10 14:33:28

问题


Trying to set up a Conv1D layer to be the input layer in keras.

The dataset is 1000 timesteps, and each timestep has 1 feature.

After reading a bunch of answers I reshaped my dataset to be in the following format of (n_samples, timesteps, features), which corresponds to the following in my case:

train_data   = (78968, 1000, 1)
test_data    = (19742, 1000, 1)
train_target = (78968,)
test_target  = (19742,)

I later create and compile the code using the following lines

model = Sequential()
model.add(Conv1D(64, (4), input_shape = (1000,1) ))

model.add(MaxPooling1D(pool_size=2))
model.add(Dense(1))
optimizer = opt = Adam(decay = 1.000-0.999)
model.compile(optimizer=optimizer,
              loss='mean_squared_error',
              metrics=['mean_absolute_error','mean_squared_error'])

Then I try to fit, note, train_target and test_target are pandas series so i'm calling DataFrame.values to convert to numpy array, i suspect there might be an issue there?

training = model.fit(train_data, 
                 train_target.values,
                 validation_data=(test_data, test_target.values),
                 epochs=epochs,
                 verbose=1)

The model compiles but I get an error when I try to fit

Error when checking target: expected dense_4 to have 3 dimensions, 
but got array with shape (78968, 1)

I've tried every combination of reshaping the data and can't get this to work.

I've used keras with dense layers only before for a different project where the input_dimension was specificied instead of the input_shape, so I'm not sure what I'm doing wrong here. I've read almost every stack overflow question about data shape issues and I'm afraid the problem is elsewhere, any help is appreciated, thank you.


回答1:


Under the line model.add(MaxPooling1D(pool_size=2)), add one line model.add(Flatten()), your problem will be solved. Flatten function will help you convert your data into correct shape, please see this site for more information https://www.tensorflow.org/api_docs/python/tf/keras/layers/Flatten



来源:https://stackoverflow.com/questions/56845144/dataset-shape-mismatch-conv1d-input-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!