常见梯度下降的算法
通常,在常见的机器学习与算法中,对于很多的监督式算法中,都需要我们对其原始模式建立其损失函数,然后在采用一些优化的算法来对建立的损失函数进行优化,目的是为了找到更优的参数,因此了解常见的随机梯度下降的算法,对于如何降低误差,有很大的帮助,在这里,将列出一些常见的梯度下降的算法:
1:批量梯度的下降的算法BGD
批量梯度下降的算法(Batch Grandient Descent)针对的是整个数据集,通过对所有的样本的计算,来求解梯度的方法
其一般的定义的损失函数为:
进一步便可以得到其批量梯度下降的迭代式为:
缺点:每次训练时要加载全部的样本的数据,训练的过程比较慢
优点:能够得到其全局最优解,易于并行实现
2:小批量梯度下降法MBGD
由于上面采用的算法,每次迭代时,总是需要要使用所有的样本,因此在样本数据量大的情况下,这种计算是要消耗大量的成本
因此在实际中,人们再想能否可以在每次迭代过程中利用部分样本来代替全部的样本,使其整个样本数据集能够被分为多个数据集块,因此便有产生了所谓了MBGD算法,其伪代码如下:
3:随机梯度下降法SGD
所谓的随机梯度下降法SGD,可以看做是一种MBGD的一种特殊的情况,即我们可以在SGD算法的中每次仅仅根据一个样本来对模型的参数进行调整,这样就实际上就是每个样本都能进行梯度算法,其实现过程如下图所示:
从上面可以看到,SGD算法是通过每个样本来迭代更新一次,对比上面的MBGD算法,可以知道迭代一次不可能达到最优,并且可以它也会伴随着一个问题,就是产生的脏数据比BGD较多,不能每次迭代时向着最优的方向迭代
优点:训练速度快;
缺点:准确度下降,并不是全局最优;不易于并行实现。
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 7 09:39:22 2018
@author: XGXYXS
"""
#=====================批量梯度下降算法(BGD)===========================#
# Ttraing the data
data1 = [(0.000000,95.364693) ,
(1.000000,97.217205) ,
(2.000000,75.195834),
(3.000000,60.105519) ,
(4.000000,49.342380),
(5.000000,37.400286),
(6.000000,51.057128),
(7.000000,25.500619),
(8.000000,5.259608),
(9.000000,0.639151),
(10.000000,-9.409936),
(11.000000, -4.383926),
(12.000000,-22.858197),
(13.000000,-37.758333),
(14.000000,-45.606221)]
data2 = [(2104.,400.),
(1600.,330.),
(2400.,369.),
(1416.,232.),
(3000.,540.)]
def create_hypothesis(theta1,theta0):
return lambda x:theta1*x +theta0
def linear_regression(data,learning_rate=0.001,variance = 0.00001):
"""Take a set of data points in the form:[(1,1),(2,2),...] and output (slope,y0)"""
# init the parameters to zero
theta0_guess =1.
theta1_guess =1.
theta0_last =100.
theta1_last =100.
m = len(data)
while (abs(theta1_guess-theta1_last)>variance or abs(theta0_guess-theta0_last)>variance):
theta1_last = theta1_guess
theta0_last = theta0_guess
hypothesis = create_hypothesis(theta1_guess,theta0_guess)
theta0_guess= theta0_guess-learning_rate*(1./m)*sum([hypothesis(point[0])-point[1] for point in data])
theta1_guess = theta0_guess-learning_rate*(1./m)*sum([hypothesis(point[0]-point[1])*point[0] for point in data])
return theta0_guess,theta1_guess
points =[(float(x),float(y)) for (x,y) in data1]
res = linear_regression(points)
print(res)
#=====================随机梯度下降算法SGD================================#
# train the data
x = [(1,0.,3) , (1,1.,3) ,(1,2.,3), (1,3.,2) , (1,4.,4)]
# y[i] is the output of y = theta0*x[0]+theta1*x[1]+theta2*x[2]
y=[95.364,97.217205,75.195834,60.105519,49.342380]
# set some params
loss =0.001
# learning_rate
alpha = 0.01
diff =[0,0]
# iteror count
max_itor =1000
error1=0
error0 = 0
cnt =0
m = len(x)
# init the parameters to zero
theta0=0
theta1=0
theta2= 0
while True:
cnt = cnt +1
for i in range(m):
diff[0] =y[i]-(theta0+theta1*x[i][1]+theta2*x[i][2])
theta0 = theta0+alpha*diff[0]*x[i][0]
theta1 =theta1+alpha*diff[0]*x[i][1]
theta2 =theta2+alpha*diff[0]*x[i][2]
error1 = 0
for lp in range(len(x)):
error1 +=(y[i]-(theta0+theta1*x[i][1]+theta2*x[i][2]))**2/2
if abs(error1-error0)<loss:
break
else:
error0 = error1
print (' theta0 : %f, theta1 : %f, theta2 : %f, error1 : %f'%(theta0,theta1,theta2,error1))
print ('Done: theta0 : %f, theta1 : %f, theta2 : %f'%(theta0,theta1,theta2))