我正在尝试构建一个 1D CNN 模型,但在尝试了很多方法之后似乎无法破解数据形状问题。
这是我的代码:
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)
向我抛出这个错误:
Error when checking target: expected dense_2 to have shape (1,) but got array with shape (11627,).
我不明白我做错了什么任何帮助都会很棒!