Error when checking target: expected dense_2 to have shape (1,) but got array with shape (11627,)

60 Views Asked by At

I'm trying to build a 1D CNN model and can't seem to crack the data shape problem after trying a lot of ways to go about it.

Here is my code:

scaler = StandardScaler()
x_scaled = scaler.fit_transform(data) 
# data.shape = (16611, 6001)


trainX, testX, trainy, testy = train_test_split(x_scaled, target, test_size=0.3)

# converting to 3D for input
dtrainX, dtestX, dtrainy, dtesty = dstack(trainX), dstack(testX), dstack(trainy), dstack(testy)

# dtrainX.shape = (1, 6001, 11627)
# dtrainy.shape = (1, 1, 11627)

verbose, epochs, batch_size = 0, 10, 32
n_timesteps, n_features, n_outputs = dtrainX.shape[1], dtrainX.shape[2], dtrainy.shape[0]

Ntrainy = np.array(dtrainy)
Ntrainy = np.squeeze(Ntrainy, axis=1)

# Ntrainy.shape = (1,11627)

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(dtrainX, Ntrainy, epochs=epochs, batch_size=batch_size, verbose=verbose)

Throws me this error:

Error when checking target: expected dense_2 to have shape (1,) but got array with shape (11627,).

I don't understand what I'm doing wrong any help would be great!!

1

There are 1 best solutions below

0
On

The actual shape is of your data is:

  • X = (outputs, n_timesteps, n_features)
  • Y = (1, 1, n_features)

To make it work, dtrainX and Ntrainy shape should be reshaped to:

  • X = (n_features, n_timesteps, outputs)
  • Y = (n_features, 1, 1)

You could do that to both dtrainX and Ntrainy to make the model work:

# X
dtrainX = np.transpose(dtrainX, (2,1,0))
# Y
Ntrainy = np.transpose(Ntrainy, (1,0))