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()