说明:对于DNN神经网络的正则化的了解能够参考DNN神经网络正则化,这里主要讲怎么利用tensorflow实现正则化。html
在tensorflow里面实现L一、L2的正则化有专门的函数,下面介绍四种函数:python
一、L1正则化:tf.contrib.layers.l1_regularizer(scale,scope)(weights);L2正则化:tf.contrib.layers.l2_regularizer(scale,scope)(weights)git
参数说明:github
函数说明:正则表达式
这里有两个传参括号,由于这里是函数的多层调用,调用tf.contrib.layers.l1_regularizer()会返回用于计算正则化的一个函数l1(weights, name=None),而调用l1函数返回一个计算weights正则化后的tensor。详情查看官方文档。网络
三、tfc.layers.l1_l2_regularizer(scale_l1,scale_l2,float=1.0,scope=None) (weights)dom
参数说明:函数
函数说明:优化
这里是同时使用L一、L2正则化方法。spa
四、tfc.layers.sum_regularizer(regularizer_list,scope)(weights)
参数说明:
函数说明:
这里是一个正则化方法的混合体,regularizer_list列表中正则化方法的混合。
import tensorflow as tf import tensorflow.contrib as tfc import numpy as np
#创造数据集 x_data=np.linspace(-1,1,10000,dtype=np.float).reshape(10000,1) y_data=2*x_data*x_data+3 #占位符 x_p=tf.placeholder(tf.float32,[None,1]) y_p=tf.placeholder(tf.float32,[None,1]) #定义第一个隐藏层Layer1,输入为x有5个神经元,无激活函数 weights1=tf.Variable(tf.random_normal([1,5])) biases1=tf.Variable(tf.zeros([1,5])+0.001) input1=tf.matmul(x_p,weights1)+biases1 weights1_reg=tfc.layers.l2_regularizer(0.1)(weights1) #构建正则项,这里是直接获得计算正则化后的tensor值 #定义输出层,输入为input1,激活函数为tahn weights2=tf.Variable(tf.random_normal([5,1])) biases2=tf.Variable(tf.zeros([1,5])+0.001) prediction=tf.nn.tanh(tf.matmul(input1,weights2)+biases2) #定义损失函数,loss=均方差+正则化项 loss=tf.reduce_mean(tf.square(y_p-prediction)) + weights1_reg #定义优化方式为梯度降低 train=tf.train.GradientDescentOptimizer(0.1).minimize(loss) init=tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) #训练200次,每隔10次输出一次loss for i in range(200): sess.run(train,feed_dict={x_p:x_data,y_p:y_data}) if i % 9 == 0: print(sess.run(loss,feed_dict={x_p:x_data,y_p:y_data}))
tensorflow自带的drop的函数方法:tf.nn.frop_out(x,keep_prop)
参数说明:
代码示例:
简单的示例:
#定义第一个隐藏层Layer1,输入为x有5个神经元,无激活函数 weights1=tf.Variable(tf.random_normal([1,5])) biases1=tf.Variable(tf.zeros([1,5])+0.001) weights1_drop=tf.nn.dropout(weights1,0.5) #实现drop正则化,通常对过拟合影响高的才使用drop input1=tf.matmul(x_p,weights1_drop)+biases1 #使用定义的drop处理的weight
完整示例:
import tensorflow as tf import tensorflow.contrib as tfc import numpy as np #创造数据集 x_data=np.linspace(-1,1,10000,dtype=np.float).reshape(10000,1) y_data=2*x_data*x_data+3 #占位符 x_p=tf.placeholder(tf.float32,[None,1]) y_p=tf.placeholder(tf.float32,[None,1]) #定义第一个隐藏层Layer1,输入为x有5个神经元,无激活函数 weights1=tf.Variable(tf.random_normal([1,5])) biases1=tf.Variable(tf.zeros([1,5])+0.001) weights1_drop=tf.nn.dropout(weights1,0.5) #实现drop正则化,通常对过拟合影响高的才使用drop input1=tf.matmul(x_p,weights1_drop)+biases1 #使用定义的drop处理的weight #定义输出层,输入为input1,激活函数为tahn weights2=tf.Variable(tf.random_normal([5,1])) biases2=tf.Variable(tf.zeros([1,5])+0.001) prediction=tf.nn.tanh(tf.matmul(input1,weights2)+biases2) #定义损失函数,loss=均方差+正则化项 loss=tf.reduce_mean(tf.square(y_p-prediction)) #定义优化方式为梯度降低 train=tf.train.GradientDescentOptimizer(0.1).minimize(loss) init=tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) #训练200次,每隔10次输出一次loss for i in range(200): sess.run(train,feed_dict={x_p:x_data,y_p:y_data}) if i % 9 == 0: print(sess.run(loss,feed_dict={x_p:x_data,y_p:y_data}))