0

我正在尝试构建一个 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,).

我不明白我做错了什么任何帮助都会很棒!

4

1 回答 1

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)

你可以这样做dtrainXNtrainy让模型工作:

# X
dtrainX = np.transpose(dtrainX, (2,1,0))
# Y
Ntrainy = np.transpose(Ntrainy, (1,0))
于 2020-05-17T19:40:13.120 回答