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()
相关推荐
程序边界3 分钟前
全球人工智能技术大会(GAITC 2025):技术前沿与产业融合的深度交响
人工智能
OpenCSG10 分钟前
电子行业AI赋能软件开发经典案例——某金融软件公司
人工智能·算法·金融·开源
新加坡内哥谈技术16 分钟前
极客时间:在 Google Colab 上尝试 Prefix Tuning
人工智能
今天又学了啥22 分钟前
李飞飞World Labs开源革命性Web端3D渲染器Forge!3D高斯溅射技术首次实现全平台流畅运行
人工智能
极智视界32 分钟前
分类场景数据集大全「包含数据标注+训练脚本」 (持续原地更新)
人工智能·yolo·数据集·分类算法·数据标注·classification·分类数据集
深科文库34 分钟前
构建 MCP 服务器:第 4 部分 — 创建工具
python·chatgpt·prompt·aigc·agi·ai-native
witton38 分钟前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
翻滚的小@强1 小时前
自动驾驶科普(百度Apollo)学习笔记
人工智能·自动驾驶·百度apollo
从零开始学习人工智能1 小时前
从游戏到自动驾驶:互联网时代强化学习如何让机器学会自主决策?
人工智能·游戏·自动驾驶
幼稚园的山代王1 小时前
Prompt Enginering(提示工程)先进技术
java·人工智能·ai·chatgpt·langchain·prompt