我正在尝试在应用PCA后使用神经网络训练mnist数据库。并且由于应用PCA后的数据形状,我不断地得到错误。我不知道如何把所有的东西组合在一起。以及如何浏览整个数据库,而不仅仅是一个小补丁。
这是我的代码:
<pre> <code>
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import random
from sklearn.preprocessing import StandardScaler
from tensorflow.examples.tutorials.mnist import input_data
from sklearn.decomposition import PCA
datadir='/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=600
pca=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.shape
plt.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=10
hidden_1=1024
hidden_2=1024
input_l=percent
out_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)+b3
y_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 = 10000
init=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_training
plt.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:anaconda3libsite-packagestensorflowpythonclientsession.py",
line 929, in run
run_metadata_ptr)
File "C:anaconda3libsite-packagestensorflowpythonclientsession.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=600
pca=PCA(percent)
train_x=pca.fit_transform(train_x)
test_x=pca.fit_transform(test_x)
至
percent=.80
pca=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
它应该为后续的优化过程提供正确的张量维度。
错误表明您正在重塑张量x
,其形状为(None, 28 , 28, 1)
到(None, percent)
你给percent
的值是600,然后你给x的值是(100, 28*28*1)
,并将其提供给x
,它的形状是(None, 600)
,但不匹配。