PCA 在卷积神经网络上的实现
PCA Implementation on a Convolutional Neural Network
在 MNIST 数据上应用 PCA 后,我确定了 CNN 模型和层。在拟合 CNN 模型 (X_train_PCA, Y_train) 之后,我在评估阶段遇到了维度问题。这是消息
"ValueError: Error when checking input: expected conv2d_1_input to have shape (1, 10, 10) but got array with shape (1, 28, 28)"。当我尝试将 X_test 重塑为 10X10 格式时,我得到的分数很低
首先,我应用了最小-最大正则化,然后将 PCA 应用于 X_train。然后,我从 X_train 生成了验证数据。问题是;我可以以 100 维格式拟合数据(应用 PCA 后),我的输入数据变为 10X10。当我尝试使用 X_test 从拟合模型中获取分数时,它仍然是 (10000, 1, 28, 28))。我收到如上所述的错误。我怎样才能解决尺寸问题。我还尝试用 minmaxscaler 和 PCA 转换 X_test。分数没有变化
pca_3D = PCA(n_components=100)
X_train_pca = pca_3D.fit_transform(X_train)
X_train_pca.shape
cnn_model_1_scores = cnn_model_1.evaluate(X_test, Y_test, verbose=0)
# Split the data into training, validation and test sets
X_train1 = X_pca_proj_3D[:train_size]
X_valid = X_pca_proj_3D[train_size:]
Y_train1 = Y_train[:train_size]
Y_valid = Y_train[train_size:]
# We need to convert the input into (samples, channels, rows, cols) format
X_train1 = X_train1.reshape(X_train1.shape[0], 1, 10,
10).astype('float32')
X_valid = X_valid.reshape(X_valid.shape[0], 1, 10, 10).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
X_train1.shape, X_valid.shape, X_test.shape
((51000, 1, 10, 10), (9000, 1, 10, 10), (10000, 1, 28, 28))
#create model
cnn_model_1=Sequential()
#1st Dense Layer
cnn_model_1.add(Conv2D(32, kernel_size=(5,5),
data_format="channels_first",
input_shape=(1,10,10),
activation='relu'))
#Max-Pooling
cnn_model_1.add(MaxPooling2D(pool_size=(2,2)))
#Max pooling is a sample-based discretization process. The objective is to
down-sample an input representation (image, hidden-layer output matrix,
etc.), reducing its dimensionality
# the number of layers, remains unchanged in the pooling operation
#cnn_model_1.add(BatchNormalization())
#Dropout
cnn_model_1.add(Flatten())
#cnn_model_1.add(BatchNormalization())
#2nd Dense Layer
cnn_model_1.add(Dense(128, activation='relu'))
#final softmax layer
cnn_model_1.add(Dense(10, activation='softmax'))
# print a summary and check if you created the network you intended
cnn_model_1.summary()
#Compile Model
cnn_model_1.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
#Fit the model
cnn_model_1_history=cnn_model_1.fit(X_train1, Y_train1,
validation_data=(X_valid, Y_valid), epochs=5, batch_size=100, verbose=2)
# Final evaluation of the model
cnn_model_1_scores = cnn_model_1.evaluate(X_test, Y_test, verbose=0)
print("Baseline Test Accuracy={0:.2f}% (categorical_crossentropy) loss=
{1:.2f}".format(cnn_model_1_scores[1]*100, cnn_model_1_scores[0]))
cnn_model_1_scores
我解决了这个问题,更新了 post 以让其他编码人员直观地调试他们的代码。首先,我在 X_test 数据上应用了 PCA,在得到低分后我尝试不应用。正如@Scott 所建议的,这是错误的。仔细检查我的代码后,我发现我在构建 CNN 模型时对测试数据应用 PCA 后忘记将 X_test 更改为 X_test_pca。我还在 X_train 上安装了 PCA,同时在 X_test 数据上应用了 PCA。
在 MNIST 数据上应用 PCA 后,我确定了 CNN 模型和层。在拟合 CNN 模型 (X_train_PCA, Y_train) 之后,我在评估阶段遇到了维度问题。这是消息 "ValueError: Error when checking input: expected conv2d_1_input to have shape (1, 10, 10) but got array with shape (1, 28, 28)"。当我尝试将 X_test 重塑为 10X10 格式时,我得到的分数很低
首先,我应用了最小-最大正则化,然后将 PCA 应用于 X_train。然后,我从 X_train 生成了验证数据。问题是;我可以以 100 维格式拟合数据(应用 PCA 后),我的输入数据变为 10X10。当我尝试使用 X_test 从拟合模型中获取分数时,它仍然是 (10000, 1, 28, 28))。我收到如上所述的错误。我怎样才能解决尺寸问题。我还尝试用 minmaxscaler 和 PCA 转换 X_test。分数没有变化
pca_3D = PCA(n_components=100)
X_train_pca = pca_3D.fit_transform(X_train)
X_train_pca.shape
cnn_model_1_scores = cnn_model_1.evaluate(X_test, Y_test, verbose=0)
# Split the data into training, validation and test sets
X_train1 = X_pca_proj_3D[:train_size]
X_valid = X_pca_proj_3D[train_size:]
Y_train1 = Y_train[:train_size]
Y_valid = Y_train[train_size:]
# We need to convert the input into (samples, channels, rows, cols) format
X_train1 = X_train1.reshape(X_train1.shape[0], 1, 10,
10).astype('float32')
X_valid = X_valid.reshape(X_valid.shape[0], 1, 10, 10).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
X_train1.shape, X_valid.shape, X_test.shape
((51000, 1, 10, 10), (9000, 1, 10, 10), (10000, 1, 28, 28))
#create model
cnn_model_1=Sequential()
#1st Dense Layer
cnn_model_1.add(Conv2D(32, kernel_size=(5,5),
data_format="channels_first",
input_shape=(1,10,10),
activation='relu'))
#Max-Pooling
cnn_model_1.add(MaxPooling2D(pool_size=(2,2)))
#Max pooling is a sample-based discretization process. The objective is to
down-sample an input representation (image, hidden-layer output matrix,
etc.), reducing its dimensionality
# the number of layers, remains unchanged in the pooling operation
#cnn_model_1.add(BatchNormalization())
#Dropout
cnn_model_1.add(Flatten())
#cnn_model_1.add(BatchNormalization())
#2nd Dense Layer
cnn_model_1.add(Dense(128, activation='relu'))
#final softmax layer
cnn_model_1.add(Dense(10, activation='softmax'))
# print a summary and check if you created the network you intended
cnn_model_1.summary()
#Compile Model
cnn_model_1.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
#Fit the model
cnn_model_1_history=cnn_model_1.fit(X_train1, Y_train1,
validation_data=(X_valid, Y_valid), epochs=5, batch_size=100, verbose=2)
# Final evaluation of the model
cnn_model_1_scores = cnn_model_1.evaluate(X_test, Y_test, verbose=0)
print("Baseline Test Accuracy={0:.2f}% (categorical_crossentropy) loss=
{1:.2f}".format(cnn_model_1_scores[1]*100, cnn_model_1_scores[0]))
cnn_model_1_scores
我解决了这个问题,更新了 post 以让其他编码人员直观地调试他们的代码。首先,我在 X_test 数据上应用了 PCA,在得到低分后我尝试不应用。正如@Scott 所建议的,这是错误的。仔细检查我的代码后,我发现我在构建 CNN 模型时对测试数据应用 PCA 后忘记将 X_test 更改为 X_test_pca。我还在 X_train 上安装了 PCA,同时在 X_test 数据上应用了 PCA。