How L2 regularization use to penalize weights in tensorflow?

I know that L2 regularization technique to used to reduce over-fitting and penalize large weights. In more than one place I saw that it used like the code below in tensorflow library:

reg = tf.nn.l2_loss(w_conv1) + tf.nn.l2_loss(w_conv2) + \
tf.nn.l2_loss(w_conv3) + tf.nn.l2_loss(w_conv4) + \
tf.nn.l2_loss(w_conv5) + tf.nn.l2_loss(w_fc1) + \
tf.nn.l2_loss(w_out)

loss = tf.reduce_mean(loss + reg * beta)


I am confused about how this code penalize weights? the code adds L2 to weights then adding the result to loss. could anyone please explain this for me?

• Can you somehow back up your statement regarding outliers with literature? From my understanding $L_2$ represents a gaussian prior on weights and results in more equal weighting of features (in contrast to $L_1$) and therefore reduces sensitivity to outliers. I think you can find this statement in the Deep Learning book from Goodfellow I. – Andreas Look Jun 6 '19 at 20:56