【问题标题】:In simple multi-layer FFNN only ReLU activation function doesn't converge在简单的多层 FFNN 中,只有 ReLU 激活函数不会收敛
【发布时间】:2018-04-24 10:06:27
【问题描述】:

我正在学习 tensorflow、深度学习和实验各种激活函数。

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

我实验过的激活函数有:tf.sigmoid、tf.nn.tanh、tf.nn.softsign、tf.nn.softmax、tf.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 在我的网络中不起作用?还是我的程序完全错误?

【问题讨论】:

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


    【解决方案1】:

    您正在使用 Relu 激活函数,该函数按如下方式计算激活,

    最大值(特征,0)

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

    Gradientdecnt 优化器通过以下方式更新权重,

    Δwij = -η ∂Ei/ ∂wij

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

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

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

    希望这会有所帮助。

    【讨论】:

    • 你是对的,它是由梯度爆炸问题引起的。我实际上测试了 TF 中可用的所有激活函数,没有上限的激活函数不会收敛。顺便说一句,AdagradOptimizer 的激活函数非常收敛。
    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多