L2正则化—tensorflow实现
2017-06-20 10:34
323 查看
L2正则化是一种减少过拟合的方法,在损失函数中加入刻画模型复杂程度的指标。假设损失函数是J(θ),则优化的是J(θ)+λR(w),<
4000
span class="MathJax" id="MathJax-Element-5-Frame" role="textbox" aria-readonly="true">R(w)=∑ni=0|w2i|。
在tensorflow中的具体实现过程如下:#coding:utf-8 import tensorflow as tf def get_weight(shape,lambda): var = tf.Variable(tf.random_normal(shape),dtype=tf.float32) tf.add_to_collection("losses",tf.contrib.layers.l2_regularizer(lambda)(var))#把正则化加入集合losses里面 return var x = tf.placeholder(tf.float32,shape=(None,2)) y_ = tf.placeholder(tf.float32,shape=(none,1))#真值 batcg_size = 8 layer_dimension = [2,10,10,10,1]#神经网络层节点的个数 n_layers = len(layer_dimension)#神经网络的层数 cur_layer = x in_dimension = layer_dimension[0] for i in range (1,n_layers): out_dimension = layer_dimension[i] weight = get_weight([in_dimension,out_dimension],0.001) bias = tf.Variable(tf.constant(0.1,shape(out_dimension))) cur_layer = tf.nn.relu(tf.matmul(x,weight)) + bias) in_dimension = layer_dimension[i] ses_loss = tf.reduce_mean(tf.square(y_ - cur_layer))#计算最终输出与标准之间的loss tf.add_to_collenction("losses",ses_loss)#把均方误差也加入到集合里 loss = tf.add_n(tf.get_collection("losses")) #tf.get_collection返回一个列表,内容是这个集合的所有元素 #add_n()把输入按照元素相加
相关文章推荐
- 【tensorflow 学习】给LSTM加上L2正则化
- 使用 TensorFlow 在卷积神经网络上实现 L2 约束的 softmax 损失函数
- tensorflow:3.1)add_to_collection和L2正则化
- L2正则化的TnesorFlow实现
- 深度学习3:手动实现L2正则化(L2 Regularization)
- tensorflow 实现神经网络带正则化
- L0,L1,L2正则化浅析
- 机器学习:L1与L2正则化项
- L0,L1,L2范数,正则化,过拟合
- 正则化方法/防止过拟合提高泛化能力的方法:L1和L2 regularization、数据集扩增、dropout
- L1 L2正则化
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- L1,L2正则化理解-奥卡姆剃刀(Occam's razor)原理
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- 对过拟合的处理:正则化方法:L1和L2 regularization、数据集扩增、dropout
- 使用TensorFlow实现一个文本分类的卷积神经网络Implementing a CNN for Text Classification in TensorFlow
- Tensorflow 实现稠密输入数据的逻辑回归二分类
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- 【机器学习】AlexNet 的tensorflow 实现
- 正则化方法:L1和L2 regularization、数据集扩增、dropout