python - 中间层使tensorflow优化器停止工作

标签 python tensorflow machine-learning deep-learning autoencoder

这张图训练了一个简单的信号身份编码器,实际上表明优化器正在改进权重:

import tensorflow as tf
import numpy as np
initia = tf.random_normal_initializer(0, 1e-3)

DEPTH_1 = 16
OUT_DEPTH = 1
I = tf.placeholder(tf.float32, shape=[None,1], name='I') # input
W = tf.get_variable('W', shape=[1,DEPTH_1], initializer=initia, dtype=tf.float32, trainable=True) # weights
b = tf.get_variable('b', shape=[DEPTH_1], initializer=initia, dtype=tf.float32, trainable=True) # biases
O = tf.nn.relu(tf.matmul(I, W) + b, name='O') # activation / output

#W1 = tf.get_variable('W1', shape=[DEPTH_1,DEPTH_1], initializer=initia, dtype=tf.float32) # weights
#b1 = tf.get_variable('b1', shape=[DEPTH_1], initializer=initia, dtype=tf.float32) # biases
#O1 = tf.nn.relu(tf.matmul(O, W1) + b1, name='O1')

W2 = tf.get_variable('W2', shape=[DEPTH_1,OUT_DEPTH], initializer=initia, dtype=tf.float32) # weights
b2 = tf.get_variable('b2', shape=[OUT_DEPTH], initializer=initia, dtype=tf.float32) # biases
O2 = tf.matmul(O, W2) + b2

O2_0 = tf.gather_nd(O2, [[0,0]])

estimate0 = 2.0*O2_0

eval_inp = tf.gather_nd(I,[[0,0]])
k = 1e-5
L = 5.0
distance = tf.reduce_sum( tf.square( eval_inp - estimate0 ) )

opt = tf.train.GradientDescentOptimizer(1e-3)
grads_and_vars = opt.compute_gradients(distance, [W, b, #W1, b1,
  W2, b2])
clipped_grads_and_vars = [(tf.clip_by_value(g, -4.5, 4.5), v) for g, v in grads_and_vars]

train_op = opt.apply_gradients(clipped_grads_and_vars)

saver = tf.train.Saver()
init_op = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init_op)
  for i in range(10000):
    print sess.run([train_op, I, W, distance], feed_dict={ I: 2.0*np.random.rand(1,1) - 1.0})
  for i in range(10):
    print sess.run([eval_inp, W, estimate0], feed_dict={ I: 2.0*np.random.rand(1,1) - 1.0})

但是,当我取消注释中间隐藏层并训练生成的网络时,我发现权重不再进化:

import tensorflow as tf
import numpy as np
initia = tf.random_normal_initializer(0, 1e-3)

DEPTH_1 = 16
OUT_DEPTH = 1
I = tf.placeholder(tf.float32, shape=[None,1], name='I') # input
W = tf.get_variable('W', shape=[1,DEPTH_1], initializer=initia, dtype=tf.float32, trainable=True) # weights
b = tf.get_variable('b', shape=[DEPTH_1], initializer=initia, dtype=tf.float32, trainable=True) # biases
O = tf.nn.relu(tf.matmul(I, W) + b, name='O') # activation / output

W1 = tf.get_variable('W1', shape=[DEPTH_1,DEPTH_1], initializer=initia, dtype=tf.float32) # weights
b1 = tf.get_variable('b1', shape=[DEPTH_1], initializer=initia, dtype=tf.float32) # biases
O1 = tf.nn.relu(tf.matmul(O, W1) + b1, name='O1')

W2 = tf.get_variable('W2', shape=[DEPTH_1,OUT_DEPTH], initializer=initia, dtype=tf.float32) # weights
b2 = tf.get_variable('b2', shape=[OUT_DEPTH], initializer=initia, dtype=tf.float32) # biases
O2 = tf.matmul(O1, W2) + b2

O2_0 = tf.gather_nd(O2, [[0,0]])

estimate0 = 2.0*O2_0

eval_inp = tf.gather_nd(I,[[0,0]])

distance = tf.reduce_sum( tf.square( eval_inp - estimate0 ) )

opt = tf.train.GradientDescentOptimizer(1e-3)
grads_and_vars = opt.compute_gradients(distance, [W, b, W1, b1,
  W2, b2])
clipped_grads_and_vars = [(tf.clip_by_value(g, -4.5, 4.5), v) for g, v in grads_and_vars]

train_op = opt.apply_gradients(clipped_grads_and_vars)

saver = tf.train.Saver()
init_op = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init_op)
  for i in range(10000):
    print sess.run([train_op, I, W, distance], feed_dict={ I: 2.0*np.random.rand(1,1) - 1.0})
  for i in range(10):
    print sess.run([eval_inp, W, estimate0], feed_dict={ I: 2.0*np.random.rand(1,1) - 1.0})

estimate0 的评估快速收敛于某个独立于输入信号的固定值。我不知道为什么会这样

问题:

Any idea what might be wrong with the second example?

最佳答案

TL;DR:神经网络变得越深,您就越应该注意梯度流(参见“梯度消失”的 this discussion)。一种特殊情况是变量初始化


问题分析

我已将变量和梯度的张量板摘要添加到您的两个脚本中,并得到以下内容:

2层网络

2-layer

3层网络

3-layer network

图表显示了 W:0 的分布变量(第一层)以及它们如何从 0 纪元变为 1000(可点击)。事实上,我们可以看到,2 层网络中的变化率要高得多。但我想注意梯度分布,它在 3 层网络中更接近于 0(第一个方差约为 0.005,第二个方差约为 0.000002,即小 1000 倍)。这就是消失梯度问题

如果您有兴趣,这里是辅助代码:

for g, v in grads_and_vars:
  tf.summary.histogram(v.name, v)
  tf.summary.histogram(v.name + '_grad', g)

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('train_log_layer2', tf.get_default_graph())

...

_, summary = sess.run([train_op, merged], feed_dict={I: 2*np.random.rand(1, 1)-1})
if i % 10 == 0:
  writer.add_summary(summary, global_step=i)

解决方案

所有的深度网络都在一定程度上受此影响,并且 没有通用的解决方案可以自动神奇地修复任何 网络。但是有一些技术可以将它推向正确的方向。初始化就是其中之一。

我将您的正常初始化替换为:

W_init = tf.contrib.layers.xavier_initializer()
b_init = tf.constant_initializer(0.1)

Xavier init 教程很多,你可以看看this one , 例如。 请注意,我将 bias init 设置为略微为正,以确保 ReLu 输出对于大多数神经元都是正的,至少在开始时是这样。

这立即改变了图片:

3-layer-improved

权重仍然没有像以前那样快速移动,但它们在移动(注意 W:0 值的比例)并且梯度分布在 0 处变得不那么尖锐,因此好多了。

当然,这还没有结束。为了进一步改进它,你应该实现完整的自动编码器,因为目前损失受到 [0,0] 的影响。元素重建,因此大多数输出​​不用于优化。您还可以使用不同的优化器(Adam 是我的选择)和学习率。

关于python - 中间层使tensorflow优化器停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816873/

相关文章:

使用 Keras 的 python 生成器线程安全

python - TensorFlow 运行时错误 : MetaGraphDef associated with tags serve could not be found in SavedModel

apache-spark - 在 Spark MLlib 中处理不平衡数据集

machine-learning - 多类多标签分类的精度/召回率

python - 在pandas中如何根据相同的ID迭代两个数据框

python - nbconvert:ValueError:在以下路径中找不到名称为 'rst' 的模板子目录:[LS OF PATHS]

swift - 为什么我启用了 Metal API 但我的 Coreml 自定义层仍在 CPU 上运行

c++ - 在 tensorflow 中访问张量数据时出现段错误(在 c++ 中)

r - GBM R函数: get variable importance separately for each class

javascript - 如何使用 javascript 或 selenium 获取给定 url 中的所有 cookie?