How to add regularizations in TensorFlow?

后端 未结 10 1347
慢半拍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:35

    cross_entropy = tf.losses.softmax_cross_entropy(
      logits=logits, onehot_labels=labels)
    
    l2_loss = weight_decay * tf.add_n(
         [tf.nn.l2_loss(tf.cast(v, tf.float32)) for v in tf.trainable_variables()])
    
    loss = cross_entropy + l2_loss
    

提交回复
热议问题