正则作用的对象是目标函数,如图对均方误差使用
loss = tf.reduce_mean(tf.square(y-y_) + tf.contrib.layers.l2_regularizer(lambda)(w))
1. 基本工作原理
weights = tf.constant([[1., -2.], [-3., 4.]])
with tf.Session() as sess:
print(sess.run(tf.contrib.layers.l1_regularizer(.5)(weights)))
# (1+2+3+4)*.5 ⇒ 5
print(sess.run(tf.contrib.layers.l2_regularizer(.5)(weights)))
# (1+4+9+16)*.5*.5 ⇒ 7.5