首页 > 解决方案 > 我需要帮助为我的二维 CNN 模型找到正确的输入形状

问题描述

我正在尝试为二维数据构建 CNN 模型。我有 1000 行 26 列。这是我拥有的代码,我为我的输入形状尝试了多种组合,但我无法弄清楚我做错了什么。

# CNN
# The known number of output classes.
num_classes = 10

#  label encoding
encoder = LabelEncoder()
y_train = encoder.fit_transform(y_train)
y_test = encoder.fit_transform(y_test)

# one hot encoding
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)

print(X_train.shape)
print(X_test.shape)

# reshape 2D to 3D
x_train = X_train.reshape(670, 26, 1)
x_test = X_test.reshape(330, 26, 1)

print(x_train.shape)
print(x_test.shape)

# build CNN model
model2 = models.Sequential()
model2.add(layers.Conv1D(64, kernel_size=2, input_shape=(26, 1), activation='relu'))  # convolution
model2.add(layers.MaxPool1D(pool_size=2))  # pooling
model2.add(layers.Flatten())  # flatten
model2.add(layers.Dense(128, activation='relu'))  # fc
2.add(layers.Dense(num_classes, activation='softmax'))

# model compile
model2.compile(loss="categorical_crossentropy",
               optimizer=adam,
               metrics=['accuracy'])

# model.summary()
batch_size = 128
epochs = 5000
model = model.fit(x_train, y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  verbose=2,
                  callbacks=early_stopping,
                  validation_split=0.1,
                  )

这给出了:

Error:ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 26 but received input with shape (None, 26, 1)

标签: pythonmachine-learningneural-networkconv-neural-networkreshape

解决方案


如果您正确加载数据并仅在 Train 上安装标签编码器,然后转换训练和测试集,您的实现将完美运行!

df = pd.read_csv('data.csv')
train, test = train_test_split(df, test_size=0.33)
y_train, X_train = train.iloc[:,-1].values, train.iloc[:,1:-1].values
y_test, X_test = test.iloc[:,-1].values, test.iloc[:,1:-1].values
print(X_test.shape, X_train.shape, y_test.shape, y_train.shape)

# CNN
# The known number of output classes.
num_classes = 10

#  label encoding
encoder = LabelEncoder()
y_train = encoder.fit_transform(y_train)
y_test = encoder.transform(y_test)

# one hot encoding
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

print(X_train.shape)
print(X_test.shape)

# reshape 2D to 3D
x_train = X_train.reshape(670, 26, 1)
x_test = X_test.reshape(330, 26, 1)

print(x_train.shape)
print(x_test.shape)

# build CNN model
model2 = models.Sequential()
model2.add(layers.Conv1D(64, kernel_size=2, input_shape=(26, 1), activation='relu'))  # convolution
model2.add(layers.MaxPool1D(pool_size=2))  # pooling
model2.add(layers.Flatten())  # flatten
model2.add(layers.Dense(128, activation='relu'))  # fc
model2.add(layers.Dense(num_classes, activation='softmax'))

# model compile
model2.compile(loss="categorical_crossentropy",
               optimizer=Adam(),
               metrics=['accuracy'])

# model.summary()
batch_size = 128
epochs = 5000
model = model2.fit(x_train, y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  verbose=2,
                  callbacks=EarlyStopping(),
                  validation_split=0.1,
                  )

输出:

(330, 26) (670, 26) (330,) (670,)
(670, 26)
(330, 26)
(670, 26, 1)
(330, 26, 1)
Epoch 1/5000
5/5 - 0s - loss: 244.5596 - accuracy: 0.0829 - val_loss: 151.6749 - val_accuracy: 0.1045

推荐阅读