我刚开始使用TensorFlow,并按照MNIST数据集的教程示例进行操作。进展顺利,准确率达到了约90%。
但当我用自己的版本替换next_batch
后,结果比之前差很多,通常只有50%。
我没有使用TensorFlow下载并解析的数据,而是从这个网站下载数据集,并使用numpy获取所需数据。
df = pd.read_csv('mnist_train.csv', header=None)X = df.drop(0,1)Y = df[0]temp = np.zeros((Y.size, Y.max()+1))temp[np.arange(Y.size),Y] = 1np.save('X',X)np.save('Y',temp)
对测试数据进行同样的处理,然后按照教程进行,没有任何更改
x = tf.placeholder(tf.float32, shape=[None, 784])y_ = tf.placeholder(tf.float32, shape=[None, 10])X = np.load('X.npy')Y = np.load('Y.npy')X_test = np.load('X_test.npy')Y_test = np.load('Y_test.npy')BATCHES = 1000W = tf.Variable(tf.truncated_normal([784,10], stddev=0.1))# W = tf.Variable(tf.zeros([784,10]))b = tf.Variable(tf.zeros([10]))y = tf.nn.softmax(tf.matmul(x, W) + b)cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))sess = tf.InteractiveSession()tf.global_variables_initializer().run()
这里是我自己的 get_mini_batch,我对原始数据的索引进行洗牌,然后每次从中取出100个数据,这似乎与示例代码做的一模一样。唯一的区别是我丢弃了一些尾部的数据。
pos = 0idx = np.arange(X.shape[0])np.random.shuffle(idx)for _ in range(1000): batch_xs, batch_ys = X[idx[range(pos,pos+BATCHES)],:], Y[idx[range(pos,pos+BATCHES)],] if pos+BATCHES >= X.shape[0]: pos = 0 idx = np.arange(X.shape[0]) np.random.shuffle(idx) pos += BATCHES sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})print(sess.run(accuracy, feed_dict={x: X_test, y_: Y_test}))
我很困惑为什么我的版本比教程中的差很多。
回答: