使用TensorFlow和MNISt数据库进行深度神经网络的主成分分析,遇到数据形状错误

我在尝试对MNIST数据库应用主成分分析(PCA)后进行神经网络训练,但由于应用PCA后的数据形状问题不断遇到错误。我不确定如何将所有部分整合在一起,以及如何遍历整个数据库,而不仅仅是其中的一小部分。

这是我的代码:

    <pre> <code>import tensorflow as tfimport matplotlib.pyplot as pltimport numpy as npimport randomfrom sklearn.preprocessing import StandardScalerfrom tensorflow.examples.tutorials.mnist import input_datafrom sklearn.decomposition import PCAdatadir='/data' data= input_data.read_data_sets(datadir, one_hot=True)train_x = data.train.images[:55000]train_y= data.train.labels[:55000]test_x = data.test.images[:10000]test_y = data.test.labels[:10000]print("original shape:   ", data.train.images.shape)percent=600pca=PCA(percent)train_x=pca.fit_transform(train_x)test_x=pca.fit_transform(test_x)print("transformed shape:", data.train.images.shape)train_x=pca.inverse_transform(train_x)test_x=pca.inverse_transform(test_x)c=pca.n_components_plt.figure(figsize=(8,4));plt.subplot(1, 2, 1);image=np.reshape(data.train.images[3],[28,28])plt.imshow(image, cmap='Greys_r')plt.title("Original Data")plt.subplot(1, 2, 2);image1=train_x[3].reshape(28,28)image.shapeplt.imshow(image1, cmap='Greys_r')plt.title("Original Data after 0.8 PCA")plt.figure(figsize=(10,8))plt.plot(range(c), np.cumsum(pca.explained_variance_ratio_))plt.grid()plt.title("Cumulative Explained Variance")plt.xlabel('number of components')plt.ylabel('cumulative explained variance');num_iters=10hidden_1=1024hidden_2=1024input_l=percentout_l=10'''input layer'''x=tf.placeholder(tf.float32, [None, 28,28,1])x=tf.reshape(x,[-1, input_l])w1=tf.Variable(tf.random_normal([input_l,hidden_1])) w2=tf.Variable(tf.random_normal([hidden_1,hidden_2]))w3=tf.Variable(tf.random_normal([hidden_2,out_l]))b1=tf.Variable(tf.random_normal([hidden_1]))b2=tf.Variable(tf.random_normal([hidden_2]))b3=tf.Variable(tf.random_normal([out_l]))Layer1=tf.nn.relu_layer(x,w1,b1)Layer2=tf.nn.relu_layer(Layer1,w2,b2)y_pred=tf.matmul(Layer2,w3)+b3y_true=tf.placeholder(tf.float32,[None,out_l])loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_pred, labels=y_true))optimizer= tf.train.AdamOptimizer(0.006).minimize(loss)correct_pred=tf.equal(tf.argmax(y_pred,1), tf.argmax(y_true,1))accuracy= tf.reduce_mean(tf.cast(correct_pred, tf.float32))store_training=[]store_step=[]m = 10000init=tf.global_variables_initializer()with tf.Session() as sess:    sess.run(init)    for epoch in range(num_iters):        indices = random.sample(range(0, m), 100)        batch_xs = train_x[indices]        batch_ys = train_y[indices]        sess.run(optimizer, feed_dict={x:batch_xs, y_true:batch_ys})        training=sess.run(accuracy, feed_dict={x:test_x, y_true:test_y})        store_training.append(training)      testing=sess.run(accuracy, feed_dict={x:test_x, y_true:test_y})print('Accuracy :{:.4}%'.format(testing*100))z_reg=len(store_training)x_reg=np.arange(0,z_reg,1)y_reg=store_trainingplt.figure(1)plt.plot(x_reg, y_reg,label='Regular Accuracy')

这是我得到的错误信息:

     "Traceback (most recent call last):

File "<ipython-input-2-ff57ada92ef5>", line 135, in <module>sess.run(optimizer, feed_dict={x:batch_xs, y_true:batch_ys}) File "C:\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 929, in run run_metadata_ptr) File "C:\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run str(subfeed_t.get_shape()))) ValueError: Cannot feed value of shape (100, 784) for Tensor 'Reshape:0', which has shape '(?, 600)'"

回答:

首先,我建议仅对训练集进行PCA拟合,因为训练集和测试集可能会得到不同的PCA分量。最简单的修复方法是更改以下代码段:

percent=600pca=PCA(percent)train_x=pca.fit_transform(train_x)test_x=pca.fit_transform(test_x)

改为

percent=.80pca=PCA(percent)pca.fit(train_x)train_x=pca.transform(train_x)test_x=pca.transform(test_x)

其次,你在进行PCA时使用了percent=600,然后应用了PCA的逆变换,这意味着你回到了原始特征数量的空间。为了开始学习减少后的PCA分量,你也可以尝试更改这段代码:

train_x=pca.inverse_transform(train_x)test_x=pca.inverse_transform(test_x)c=pca.n_components_<plotting code>    input_l=percent

改为:

c=pca.n_components_#plotting commented out   input_l=c

这应该会为后续的优化过程提供正确的张量维度。

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注