我正在尝试构建一维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,).
我不明白我在做错什么,任何帮助都会很棒!
答案 0 :(得分:0)
您数据的实际形状是:
X = (outputs, n_timesteps, n_features)
Y = (1, 1, n_features)
要使其正常工作,应将dtrainX
和Ntrainy
的形状重塑为:
X = (n_features, n_timesteps, outputs)
Y = (n_features, 1, 1)
您可以同时对dtrainX
和Ntrainy
进行操作,以使模型正常工作:
# X
dtrainX = np.transpose(dtrainX, (2,1,0))
# Y
Ntrainy = np.transpose(Ntrainy, (1,0))