【问题标题】:PCA Implementation on a Convolutional Neural Network卷积神经网络上的 PCA 实现
【发布时间】:2019-05-31 13:58:15
【问题描述】:

在 MNIST 数据上应用 PCA 后,我确定了 CNN 模型和层。在拟合 CNN 模型(X_train_PCA,Y_train)后,我在评估阶段遇到了维度问题。这是消息 “ValueError:检查输入时出错:预期 conv2d_1_input 的形状为 (1, 10, 10),但得到的数组的形状为 (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

【问题讨论】:

  • “当我尝试将 X_test 重塑为 10X10 格式时。” X_train 是如何变成 10x10 的?当然,您应该对测试数据使用相同的过程...
  • @Scott 我选择 100 作为 PCA 的 K 值。为了在将 X_train 和 X_valid 和 X_test 输入 CNN 模型之前将输入转换为(样本、通道、行、列)格式,只有 10X10 的行和列满足了要求我 size*100 值的错误。我将对测试数据应用 PCA 以及建议
  • @Scott 我可以解决这个问题。您的建议限制了选项,当我调试代码时,我意识到我正在为模型提供 X_test 而不是 X_test_pca。解决了:)

标签: python pandas conv-neural-network pca


【解决方案1】:

我解决了这个问题,更新了帖子,让其他编码人员能够直观地调试他们的代码。首先,我在 X_test 数据上应用了 PCA,在得到低分后我尝试不应用。正如@Scott 建议的那样,这是错误的。仔细检查我的代码后,我发现我在构建 CNN 模型时对测试数据应用 PCA 后忘记将 X_test 更改为 X_test_pca。我还在 X_train 上安装了 PCA,同时在 X_test 数据上应用了 PCA。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2017-08-06
    • 2019-07-20
    • 1970-01-01
    • 2020-11-01
    • 2020-10-07
    相关资源
    最近更新 更多