python实现梯度下降法

# coding:utf-8
import numpy as np 
import matplotlib.pyplot as plt
x = np.arange(-5/2,5/2,0.01)
y = -x**3+x**2+np.e+x**4

dy = lambda x:-3*x**2+2*x+4*x**3
dyy = lambda x:-6*x+2+12*x**2

a = 0.001
def sgd_func(n):
    x0=-10
    ady = []
    for i in range(n):
        # x0 = x0 -a*dy(x0)
        x0 = x0 - dy(x0)/dyy(x0)
        ady.append(x0)
    return x0,ady

x0,ady = sgd_func(10)

y0 = x0**2+x0+np.e
plt.subplot(1,2,1)
plt.plot(ady)
plt.subplot(1,2,2)
plt.plot(x,y)
plt.text(x0,y0,'* min_value')
plt.show()

 

转载于:https://www.cnblogs.com/wuzaipei/p/10085626.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值