Tensorflow 在损失函数中加入正则项(Normalization)
来源:互联网 发布:linux挂载samba共享 编辑:程序博客网 时间:2024/05/26 02:52
L1和L2正则:
举例说明,假设有一个数组 nums=[1,2,3,4]
L1 = a*(|1|+|2|+|3|+|4|)
L2 = a*(1^2+2^2+3^2+4^2)/2
其中a是系数,用于平衡正则项与经验损失函数的权重关系,即:C = loss+a*Normalization。
这里说明一下,利用L1经过训练后,会让权重得到稀疏结,即权重中的一部分项为0,这种作用相当于对原始数据进行了特征选择;利用L2进行训练后,会让权重更趋于0,但不会得到稀疏解,这样做可以避免某些权重过大;两种正则做法都可以减轻过拟合,使训练结果更加具有鲁棒性。
下面给出使用TensorFlow计算L1,L2的方法。
import tensorflow as tfalpha = 0.5 #系数设置为0.5,alpha相当于上述aval = tf.constant([[1,2],[3,4]],dtype=tf.float32)l1 = tf.contrib.layers.l1_regularizer(alpha)(val)l2 = tf.contrib.layers.l2_regularizer(alpha)(val)with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print ("l1:",sess.run(l1)) print ("l2:",sess.run(l2))
下面给出使用TensorFlow的整体训练过程:
# -*- coding: utf-8 -*-"""Created on Sun Jun 4 21:51:33 2017@author: Yangyang Deng@Email: yangydeng@163.com"""import tensorflow as tffrom numpy.random import RandomStatew1 = tf.Variable(tf.truncated_normal([2,3],seed=1))w2 = tf.Variable(tf.truncated_normal([3,1],seed=1))x = tf.placeholder(dtype=tf.float32,shape=[None,2])y_real = tf.placeholder(dtype=tf.float32,shape=[None,1])a = tf.nn.relu(tf.matmul(x,w1)) #神经元的激活函数为 reluy_pre = tf.nn.relu(tf.matmul(a,w2))sample_size = 20000 #训练样本总数batch = 500 # 使用mini-batch(批梯度下降),每个batch的大小。rds = RandomState(0)X = rds.rand(sample_size,2)Y = [[int(20*x1+30*x2)]+rds.rand(1) for (x1,x2) in X] #Y = 20*x1+30*x2+noiseglobal_step = tf.Variable(0)#--------------学习速率的设置(学习速率呈指数下降)---------------------learning_rate = tf.train.exponential_decay(1e-2,global_step,decay_steps=sample_size/batch,decay_rate=0.98,staircase=True) #-------------使用均方误差(MSE)作为损失函数,在损失函数中加入L1正则项MSE = tf.reduce_mean(tf.square(y_real-y_pre)+tf.contrib.layers.l1_regularizer(0.1)(w1)) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(MSE,global_step=global_step)step = 20000 #训练的总次数start = 0end = batchsess = tf.Session()sess.run(tf.global_variables_initializer())for i in range(step): sess.run(train_step,feed_dict={x:X[start:end],y_real:Y[start:end]}) if not i%20: H = sess.run(MSE,feed_dict={x:X[start:end],y_real:Y[start:end]}) lr = sess.run(learning_rate) l1 = sess.run(tf.contrib.layers.l1_regularizer(0.1)(w1)) print ("MSE: ",H," learning_rate: ",lr," l1:",l1) if H<1e-1: #采用stop early 的方法防止过拟合,节省训练时间。 break strat = end if end<sample_size else 0 end = start+batchy1 = sess.run(y_pre,feed_dict={x:X[start:end]})y2 = Y[start:end]sess.close() #训练结果的部分展示for i in range(100): print(y1[i],y2[i])
阅读全文
0 0
- Tensorflow 在损失函数中加入正则项(Normalization)
- 79、tensorflow计算一个五层神经网络的正则化损失系数、防止网络过拟合、正则化的思想就是在损失函数中加入刻画模型复杂程度的指标
- tensorflow学习笔记(三十八):损失函数加上正则项
- tensorflow中损失函数总结
- TensorFlow之损失函数、学习率、正则
- tensorflow中正则化防止过拟合以及Batch Normalization
- Tensorflow 损失函数定义
- TensorFlow损失函数专题
- TensorFlow自定义损失函数
- 深度学习笔记——TensorFlow学习笔记(二)激活函数、损失函数、优化算法和正则项
- [tensorflow损失函数系列]sigmoid_cross_entropy_with_logits
- [tensorflow损失函数系列]weighted_cross_entropy_with_logits
- [tensorflow损失函数系列]softmax_cross_entropy_with_logits
- [tensorflow损失函数系列]sparse_softmax_cross_entropy_with_logits
- Tensorflow实现经典损失函数
- TensorFlow四种损失函数
- 10、TensorFLow 中的损失函数
- tensorflow在图像中加入标注框
- final关键字在Java中的具体用法总结
- 机器学习中特征选择概述
- Codevs1515 跳
- MAC下想用ubuntu apt-get怎么办呢?
- tar 命令简单使用
- Tensorflow 在损失函数中加入正则项(Normalization)
- 两个有序数组的中位数
- 第3章 启程——Windows编程基础. 学习笔记
- Java实现操作excel表格(基于poi.jar)
- 洛谷Oj-数的划分-递推/搜索+剪枝
- 152. Maximum Product Subarray
- 数据库链接
- Windows核心编程第一章笔记
- 处理Excel的类库主要有两种JXL 和POI和介绍.