【问题标题】:tensorflow mnist example with my own get_next_minibatchtensorflow mnist 示例与我自己的 get_next_minibatch
【发布时间】:2017-03-25 23:21:39
【问题描述】:

我刚开始使用 tensorflow,并按照 MNIST 数据集上的教程示例进行操作。进展顺利,我得到了大约 90% 的准确率。

但是在我用自己的版本替换next_batch 之后,结果比以前差了很多,通常是 50%。

我没有使用 Tensorflow 下载和解析的数据,而是从 website 下载数据集。使用 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] = 1
np.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 = 1000


W = 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 = 0
idx = 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}))

这让我很困惑,为什么我的版本比教程版本差很多。

【问题讨论】:

  • 从您的数据中打印出 X[0],它是否包含数字 0-255?
  • 是的,X[0] 包含第一个数据实例,而不是标题
  • 我不是在考虑标题而是在考虑值,MNIST 通常被标准化为在 [0, 1] 中具有值,因此如果您的数据是“原始”0-255,您可能希望将其除以255. 在进入网络之前
  • 这正是性能如此差的原因。感谢您的帮助!

标签: python numpy machine-learning tensorflow


【解决方案1】:

就像lejilot 所说,我们应该在将数据推入神经网络之前对其进行归一化。 See this post

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 2016-02-13
    • 2017-11-02
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    相关资源
    最近更新 更多