pytorch-梯度下降

梯度下降

y = x 2 ∗ s i n ( x ) y ′ = 2 x s i n x + x 2 c o s x x 1 = x − Δ y ′ ( x ) 其中 Δ 表示学习率, y ′ ( x ) 代表 y 在 x 点处关于 x 的梯度。 y = x^2 * sin(x) \\ y' = 2xsinx+x^2cosx \\ x_1 = x - \Delta y'(x) \\ 其中 \Delta 表示学习率, y'(x)代表y在x点处关于x的梯度。 y=x2∗sin(x)y′=2xsinx+x2cosxx1=x−Δy′(x)其中Δ表示学习率,y′(x)代表y在x点处关于x的梯度。

注意:

在单变量中,梯度就等于导数。

在多变量中,梯度方向表示函数值增加最快的方向。

1、学习率过大会使算法难以收敛,且波动很大

2、学习率过低可能导致算法收敛过慢

具体代码:

csharp 复制代码
import numpy as np

# y = wx+b
# loss = (wx+b-y)^2

# 计算损失
def compute_loss(w,b,points):
    totalError = 0
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        totalError += (y - (w * x + b)) ** 2
    return totalError / float(len(points))

# 梯度下降
# w' = w - lr * dloss/dw
# dl/dw = 2(wx+b-y)x
# dl/db = 2(wx+b-y)
def step_gradient(b_current, w_current, points, learningRate):
    b_gradient = 0
    w_gradient = 0
    N = float(len(points))
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        b_gradient += (2/N) * (((w_current * x + b_current )) - y) * x
        w_gradient += (2/N) * (w_current * x + b_current - y)
    new_b = b_current - (learningRate * b_gradient)
    new_w = w_current - (learningRate * w_gradient)
    return [new_b, new_w]
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
    b = starting_b
    w = starting_w
    for i in range(num_iterations):
        b , w = step_gradient(b, w, np.array(points), learning_rate)
    return [b, w]

def generate_data(num_points):
    np.random.seed(42)
    x = 2 * np.random.rand(num_points,1)
    y = 4 + 3 * x + np.random.randn(num_points, 1)
    points = np.hstack((x,y))
    return points

def run():
    # points = np.genfromtxt("data.csv", delimiter=',')
    points = generate_data(100)
    learning_rate = 0.0001
    initial_b = 0
    initial_w = 0
    num_iterations = 100
    print("Starting gradient descent at b = {0}, m = {1}, error = {2}".format(initial_b,initial_w,compute_loss(initial_b,initial_w,points)))
    print("Runing....")
    [b , w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iterations)
    print("After {0} iterations b = {1}, m = {2}, loss = {3}".format(num_iterations, b, w, compute_loss(b,w,points)))

if __name__ == '__main__':
    run()
相关推荐
奈斯。zs16 分钟前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody205016 分钟前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow
学步_技术18 分钟前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式
DisonTangor1 小时前
阿里通义千问开源Qwen2.5系列模型:Qwen2-VL-72B媲美GPT-4
人工智能·计算机视觉
豆浩宇1 小时前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
Narutolxy1 小时前
Python 单元测试:深入理解与实战应用20240919
python·单元测试·log4j
LLSU131 小时前
聚星文社AI软件小说推文软件
人工智能
JackieZhengChina1 小时前
吴泳铭:AI最大的想象力不在手机屏幕,而是改变物理世界
人工智能·智能手机
ShuQiHere1 小时前
【ShuQiHere】 探索数据挖掘的世界:从概念到应用
人工智能·数据挖掘
嵌入式杂谈1 小时前
OpenCV计算机视觉:探索图片处理的多种操作
人工智能·opencv·计算机视觉