在简单的多层FFNN中,只有ReLU激活函数不会收敛

时间:2017-11-11 06:59:17

标签: machine-learning tensorflow deep-learning activation-function

我学习张量流,深度学习和试验各种激活函数。

我为MNIST问题创建了一个多层FFNN。主要基于官方tensorflow网站的教程,除了添加了3个隐藏层。

我尝试过的激活功能包括:tf.sigmoidtf.nn.tanhtf.nn.softsigntf.nn.softmaxtf.nn.relu。只有tf.nn.relu没有收敛,网络输出随机噪声(测试精度约为10%)。以下是我的源代码:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder(tf.float32, [None, 784])

W0 = tf.Variable(tf.random_normal([784, 200]))
b0 = tf.Variable(tf.random_normal([200]))
hidden0 = tf.nn.relu(tf.matmul(x, W0) + b0)

W1 = tf.Variable(tf.random_normal([200, 200]))
b1 = tf.Variable(tf.random_normal([200]))
hidden1 = tf.nn.relu(tf.matmul(hidden0, W1) + b1)

W2 = tf.Variable(tf.random_normal([200, 200]))
b2 = tf.Variable(tf.random_normal([200]))
hidden2 = tf.nn.relu(tf.matmul(hidden1, W2) + b2)

W3 = tf.Variable(tf.random_normal([200, 10]))
b3 = tf.Variable(tf.random_normal([10]))
y = tf.matmul(hidden2, W3) + b3

y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    for _ in range(10000):
        batch_xs, batch_ys = mnist.train.next_batch(128)
        session.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
        if _ % 1000 == 0:
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
            print(_, session.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print('final:', session.run(accuracy, feed_dict={x: mnist.test.images,
            y_: mnist.test.labels}))

代码输出如下:

0 0.098
1000 0.098
2000 0.098
3000 0.098
4000 0.098
5000 0.098
6000 0.098
7000 0.098
8000 0.098
9000 0.098
final: 0.098

如果将tf.nn.relu替换为其他激活函数,则网络准确度会逐渐提高(尽管具有不同的最终准确度),这是预期的。

我已经阅读了教科书/教程,ReLU应该是第一个作为激活功能的候选人。

我的问题是为什么ReLU不能在我的网络中工作?或者我的程序完全错了?

1 个答案:

答案 0 :(得分:1)

您正在使用 Relu 激活功能来计算激活,如下所示,

max(features,0)

由于它输出最大值,这有时会导致爆炸梯度

Gradientdecnt优化器通过以下内容更新权重,

Δwij=-η ∂Ei/ ∂wij

其中η是学习率,∂Ei/∂wij是损失w.r.t权重的部分推导。当最大值变得越来越大时,部分派生也会变大并导致爆炸梯度。因此,正如您可以在等式中观察到的那样,您需要调整学习率(η)以克服这种情况。

一个常见的规则是降低学习率,通常每次降低10倍。

对于您的情况,设置学习率= 0.001并提高准确性。

希望这有帮助。