向张量流 MNIST 教程添加更多层会使准确性下降



深度学习新手。通过gogoel tensorflow(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_softmax.py)的教程MNIST_SOFTMAX.py我添加了两个新层,只是为了看看会发生什么。

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b

将上面的代码更改为

x = tf.placeholder(tf.float32, [None, 784])
W1 = tf.Variable(tf.zeros([784, 256]))
W2 = tf.Variable(tf.zeros([256, 256]))
W3 = tf.Variable(tf.zeros([256, 10]))
B1 = tf.Variable(tf.zeros([256]))
B2 = tf.Variable(tf.zeros([256]))
B3 = tf.Variable(tf.zeros([10]))
Y1 = tf.matmul(x, W1) + B1
Y2 = tf.matmul(Y1, W2) + B2
Y3 = tf.matmul(Y2, W3) + B3
y = Y3

它将精度从 0.9188 下降到 0.1028。 我可以知道它为什么下降吗?

我认为您既需要权重的对称性破坏,也需要层之间的非线性激活:

W = tf.Variable(tf.random_normal([784, 256], stddev=0.1)) 
W1 = tf.Variable(tf.random_normal([256, 256], stddev=0.1))
W2 = tf.Variable(tf.random_normal([256, 10], stddev=0.1))
b = tf.Variable(tf.zeros([256]))
b1 = tf.Variable(tf.zeros([256]))
b2 = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b
y = tf.nn.relu(y)
y = tf.matmul(y, W1) + b1
y = tf.nn.relu(y)
y = tf.matmul(y, W2) + b2

精度为 0.9653。

您遇到了与本文中回答的相同的问题。 从本质上讲,您的第一个隐藏层的学习速度比上一个慢得多。通常,您的网络应该学习正确的权重。然而,在这里,第一层中的权重很可能变化很小,误差会传播到下一层。它是如此之大,以至于后续层不可能纠正它。检查重量。

您需要在层之间添加一个非线性激活函数。试试ReLU。

最新更新