问题
Every instance of my data is an array with 72 elements. I am trying to construct a 1D cnn to do some classification but I got this error: Error when checking target: expected dense_31 to have 3 dimensions, but got array with shape (3560, 1)
This is my code:
training_features = np.load('features.npy')
training_labels = np.load('labels.npy')
training_features = training_features.reshape(-1, 72, 1)
model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(72, 1)))
model.add(MaxPooling1D(2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(2))
model.add(Dense(64, activation='relu'))
model.add(Dense(28, activation='softmax'))
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(training_features, training_labels, batch_size=32, epochs=3, validation_split=0.1)
I am a beginner. Sorry if I have poor understanding.
回答1:
Check whether your inputs in correct form. Can you share the two *.npy files (or at least shapes of your inputs).
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, Dense, MaxPooling1D, Flatten
from tensorflow.keras.optimizers import Adam
model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(72, 1)))
model.add(MaxPooling1D(2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(2))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(28, activation='softmax'))
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
#model.fit(training_features, training_labels, batch_size=32, epochs=3, validation_split=0.1)
model.summary()
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_7 (Conv1D) (None, 70, 64) 256
_________________________________________________________________
max_pooling1d_6 (MaxPooling1 (None, 35, 64) 0
_________________________________________________________________
conv1d_8 (Conv1D) (None, 33, 64) 12352
_________________________________________________________________
max_pooling1d_7 (MaxPooling1 (None, 16, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 1024) 0
_________________________________________________________________
dense_4 (Dense) (None, 64) 65600
_________________________________________________________________
dense_5 (Dense) (None, 28) 1820
=================================================================
Total params: 80,028
Trainable params: 80,028
Non-trainable params: 0
_________________________________________________________________
Hope this helps. Thanks!
回答2:
The problem is that you start with a three dimensional layer but never reduce the dimensionality in any of the following layers.
Try adding model.add(Flatten())
before the first Dense layer.
来源:https://stackoverflow.com/questions/61030068/keras-cnn-with-1d-data