梯度下降(Gradient descent,GD)

优化函数Optimizer Algorithm:

梯度下降(Gradient descent,GD)

img

其中 img表示假设预测函数的theta系数,神经网络中theta1为权重w,theta0为偏置b,img为学习率lr,J为关于theta的损失函数Loss。

img

实现代码(线性预测案例,y=a*x+b,通过最小化loss,优化a,b参数拟合预测曲线):

import matplotlib.pyplot as plt

#预测拟合函数
def h_x(theta0,theta1,x):
	return theta0+theta1*x

def SG(m,lr,theta0,theta1,X,Y):
	sum = 0
	for i in range(0,m):
		sum += (h_x(theta0,theta1,X[i])-Y[i])
	theta0 -= lr/m*sum

	sum = 0
	for i in range(0,m):
		sum += (h_x(theta0,theta1,X[i])-Y[i])*X[i]
	theta1 -= lr/m*sum
	# print("SG: theta0-{} theta1{}".format(theta0,theta1))
	return theta0,theta1

#损失函数
def loss(m,theta0,theta1,X,Y):
	result = 0.0
	for i in range(0,m):
		result += (h_x(theta0,theta1,X[i])-Y[i])**2

	return result/(2*m)

def main():
	#输入输出
	X = [1,2,3,4,5,6]
	Y = [13,14,20,21,25,30]

	m = len(X)

	theta0=0
	theta1=0
	# lr = 0.01
	lr = 0.3

	step = 0
	loss_list = []
	theta0_list = []
	theta1_list = []

	while 1:
		step+=1

		loss0 = 0.0
		loss1 = 0.0

		theta0,theta1 = SG(m,lr,theta0,theta1,X,Y)
		theta0_list.append(theta0)
		theta1_list.append(theta1)

		loss1 = loss(m,theta0,theta1,X,Y)
		loss_list.append(loss1)

		print("theta0:{} theta1:{} loss:{}".format(theta0,theta1,loss1))

		if step>20:
			break

	#输入数据X,Y二维曲线
	ax1 = plt.subplot(2,2,1) #row,col,index
	plt.plot(X,Y,'r') #红色线

	#梯度下降过程随下降次数的损失值二维曲线
	ax2 = plt.subplot(2,2,2) 
	plt.plot(list(range(0,step)),loss_list,'g')

	ax3 = plt.subplot(2,2,3)
	plt.plot(list(range(0,step)),theta0_list,c='blue',label="")

	ax4 = plt.subplot(2,2,4)
	plt.plot(theta1_list,loss_list,c='black',label="loss of theta1")
	plt.legend(loc='upper right')

	plt.show()

	print("Finally theta0:{} theta1:{} loss:{}".format(theta0,theta1,loss1))


if __name__ == '__main__':
	main()

lr = 0.01时输出结果:

在这里插入图片描述

lr = 0.3时输出结果:

在这里插入图片描述

Loss,关于theta0,theta1的梯度下降三维结果图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值