How to add regularizations in TensorFlow?

后端 未结 10 1350
慢半拍i
慢半拍i 2020-12-07 06:55

I found in many available neural network code implemented using TensorFlow that regularization terms are often implemented by manually adding an additional term to loss valu

10条回答
  •  生来不讨喜
    2020-12-07 07:39

    As you say in the second point, using the regularizer argument is the recommended way. You can use it in get_variable, or set it once in your variable_scope and have all your variables regularized.

    The losses are collected in the graph, and you need to manually add them to your cost function like this.

      reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
      reg_constant = 0.01  # Choose an appropriate one.
      loss = my_normal_loss + reg_constant * sum(reg_losses)
    

    Hope that helps!

提交回复
热议问题