我需要帮助为我的二维CNN模型找到正确的输入形状



我正在尝试为2d数据建立一个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)

如果您正确加载数据并仅在Train上适合标签编码器,那么您的实现将完美地工作,然后转换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

相关内容

  • 没有找到相关文章

最新更新