tensorflow框架学习 (四)—— DNN神经网络的正则化

一、DNN全连接层的正则化

 

说明:对于DNN神经网络的正则化的了解可以参考DNN神经网络正则化,这里主要讲怎么利用tensorflow实现正则化。

 


 

 

二、tensorflow实现L1、L2正则化的内置函数

 

函数介绍:

在tensorflow里面实现L1、L2的正则化有专门的函数,下面介绍四种函数:

1、L1正则化:tf.contrib.layers.l1_regularizer(scale,scope)(weights);L2正则化:tf.contrib.layers.l2_regularizer(scale,scope)(weights)

参数说明:

  • scale:正则表达式的超参数\lambda ,即惩罚系数。
  • scope:作用域的名字scope name,作用是在名字后面加个命名域(后续内容会说明)。
  • weights:需要正则化的参数,如神经网络的权重矩阵。

函数说明:

  这里有两个传参括号,因为这里是函数的多层调用,调用tf.contrib.layers.l1_regularizer()会返回用于计算正则化的一个函数l1(weights, name=None),而调用l1函数返回一个计算weights正则化后的tensor。详情查看官方文档

 

3、tfc.layers.l1_l2_regularizer(scale_l1,scale_l2,float=1.0,scope=None) (weights)

参数说明:

  • scale_l1、scale_l2:一个正则化超参数\lambda。
  • scope:作用域的名字scope name,作用是在名字后面加个命名域(后续内容会说明)。
  • weights:需要正则化的参数,如神经网络的权重矩阵。

函数说明:

  这里是同时使用L1、L2正则化方法。

 

4、tfc.layers.sum_regularizer(regularizer_list,scope)(weights)

参数说明:

  • regularizer_list:定义的正则化方法的函数列表,注意是函数列表,不是tensor列表。
  • scope:作用域的名字scope name,作用是在名字后面加个命名域(后续内容会说明)。

函数说明:

  这里是一个正则化方法的混合体,regularizer_list列表中正则化方法的混合。

 


 

三、实现L1、L2正则化的示例

 

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正则化的函数

 

函数介绍:

tensorflow自带的drop的函数方法:tf.nn.frop_out(x,keep_prop)

参数说明:

  • x:需要drop的权重矩阵(建议对对过拟合影响大的才进行drop处理,可以根据与需求选择)。
  • keep_prop:保留的神经元的比重(建议选0.5)。

 

代码示例:

简单的示例:

#定义第一个隐藏层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}))

 

 

转载于:https://www.cnblogs.com/dwithy/p/11288924.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值