tensorflow中使用L2正则化损失
-
方法一:tf.nn.l2_loss
self.l2_loss += tf.nn.l2_loss(weights)*0.5 self.loss += self.l2_loss
-
方法二:tf.get_variable
l2_reg=tf.contrib.layers.l2_regularizer(weight_decay) tmp=tf.constant([0,1,2,3],dtype=tf.float32) a=tf.get_variable("I_am_a",regularizer=l2_reg,initializer=tmp) #regularizer定义会将a加入REGULARIZATION_LOSSES集合 reg_set=tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) l2_loss=tf.add_n(reg_set)
-
方法三:
tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(0.5)(weights)) tf.add_to_collection('losses', mess_loss) loss = tf.add_n(tf.get_collection('losses'))
-
方法四:使用slim
with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu, weights_regularizer=slim.l2_regularizer(weight_decay)): pass