如何像Caffe一样在张量流中实现重量衰减

use*_*512 5 neural-network deep-learning tensorflow

在Caffe中,我们有一个衰减率,通常设置为0.0005。然后,在将梯度应用于所有可训练参数(例如,FC6中的W矩阵)后,将衰减:W = W *(1- 0.0005)。

我经历了许多教程tensorflow代码,但看不到人们如何实现这种权重衰减以防止出现数值问题(绝对值很大)

根据我的经验,我经常在训练过程中遇到10万次迭代的数值问题。

我还在stackoverflow上遇到了相关问题,例如, 如何在TensorFlow中设置重量成本强度? 但是,该解决方案似乎与在Caffe中实现的有点不同。

有人有类似的担忧吗?谢谢。

Ste*_*ven 3

这是一个重复的问题:

如何定义 TensorFlow 中各个层的权重衰减?

# Create your variables
weights = tf.get_variable('weights', collections=['variables'])

with tf.variable_scope('weights_norm') as scope:
  weights_norm = tf.reduce_sum(
  input_tensor = WEIGHT_DECAY_FACTOR*tf.pack(
      [tf.nn.l2_loss(i) for i in tf.get_collection('weights')]
  ),
  name='weights_norm'
)

# Add the weight decay loss to another collection called losses
tf.add_to_collection('losses', weights_norm)

# Add the other loss components to the collection losses     
# ...

# To calculate your total loss
tf.add_n(tf.get_collection('losses'), name='total_loss')
Run Code Online (Sandbox Code Playgroud)

您可以将任意 lambda 值设置为权重衰减。上面只是添加了 l2 范数。