梯度下降代码还原+可视化

话不多说,直接上代码

import numpy as np
import matplotlib.pylab as plt

def numerical_gradient(f, x):
    h = 1e-4
    grad = np.zeros_like(x) #生成和x形状相同的数组
    for idx in range (x.size):
        tem_val = x[idx]
        x[idx] = tem_val + h
        fxh1 = f(x)
        
        x[idx] = tem_val - h
        fxh2 = f(x)

        grad[idx] = (fxh1 - fxh2) / (2 * h)
        x[idx] = tem_val
    return grad

def gradient_descent(f, init_x, lr, step_num):
    x = init_x
    x1 = np.empty((step_num, init_x.size))
    for i in range (step_num):
        x1[i] = x.copy()
        grad = numerical_gradient(f, x)
        x -= lr * grad

    return  x, x1

def function_2(x):
    return x[0]**2 + x[1]**2

if __name__ == '__main__':
    init_x = np.array([-3.0, 4.0])
    result, result_1= gradient_descent(function_2, init_x=init_x, lr=0.1, step_num=100)
    print(result)
    print(result_1)
    plt.plot(result_1[:,0], result_1[:,1], 'o')
    plt.xlim(-3.5, 3.5)
    plt.ylim(-4.5, 4.5)
    plt.xlabel('x0')
    plt.ylabel('x1')
    plt.show()

以下是可视化结果:

如图,可以清楚地看到梯度下降的过程。

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值