加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

lr正则项(稀疏性与泛化性)

发布时间:2020-12-14 00:44:21 所属栏目:百科 来源:网络整理
导读:1.参数衰减和稀疏性 参数衰减,带来的好处是:通常来讲数据都具有一定波动性,减弱每个参数,防止某些参数过大,可以减弱数据varience带来的影响,防止过拟合,提高泛化能力。 稀疏性,带来好处是:1)大幅减少计算;2)减少参数,防止过拟合,提高泛化能力 2.

1.参数衰减和稀疏性

参数衰减,带来的好处是:通常来讲数据都具有一定波动性,减弱每个参数,防止某些参数过大,可以减弱数据varience带来的影响,防止过拟合,提高泛化能力。

稀疏性,带来好处是:1)大幅减少计算;2)减少参数,防止过拟合,提高泛化能力

2.l1和l2两种正则化

1l可以带来参数衰减和稀疏性,l2只能带来参数衰减

下图解释:l1可以通过尖峰使得最小值在w=0处,而l2只能是最小值靠近w=0处。

代码:

#encoding=utf-8 ''' 训练集 x y 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 1 ''' import numpy as np
import matplotlib.pyplot as plt

#参数权值范围 w = np.arange(-4,4,0.01)

#log lost l = 4*np.log(map(lambda x:1/(1+np.exp(-x)),w)) 
    + 2*np.log(map(lambda x:1-1/(1+np.exp(-x)),w)) 
    + 4*np.log(0.5)
#regularization l1 r1 = np.abs(w)
#cost func with l1 L1 = (-l + 1*r1)/10
#regularization l1 r2 = np.square(w)
#cost func with l2 L2 = (-l + 1*r2)/10

#show diff of l1 and l2 plt.plot(w,-l/10)
plt.plot(w,1*r1/10)
plt.plot(w,L1)

plt.subplot(121)
plt.plot(w,-l/10,label='log lost')
plt.plot(w,1*r1/10,label='l1 regularization')
plt.plot(w,L1,label='log lost + l1')
plt.grid()
plt.xlabel('w')
plt.ylabel('cost')
plt.legend()
plt.subplot(122)
plt.plot(w,1*r2/10,L2,label='log lost + l2')
plt.grid()
plt.xlabel('w')
plt.ylabel('cost')
plt.legend()
plt.show()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读