tersorflow添加L2 loss,正则化方式

L2正则化,主要防止模型过拟合,在loss function中增加一项对模型参数 W W W的平方之和约束,防止学习的参数权重 W W W过大,以下是tensorflow中添加L2 loss实现方式:

def l2_loss(l2_lambda=0.0001):
	loss = tf.add_n([tf.nn.l2_loss(tf.cast(v, tf.float32)) for v in tf.trainable_variables() if 'bias' not in v.name], name="l2_loss")* l2_lambda
	return loss

猜你喜欢

转载自blog.csdn.net/BGoodHabit/article/details/108563696
今日推荐