Shortcuts

PyTorch:张量

Created On: Dec 03, 2020 | Last Updated: Dec 03, 2020 | Last Verified: Nov 05, 2024

一个三阶多项式,训练来预测 \(y=\sin(x)\),范围为 \(-\pi\)\(pi\),通过最小化平方欧几里得距离。

此实现使用 PyTorch 张量手动计算前向传播、损失和反向传播。

PyTorch 张量基本与 numpy 数组相同:它不知道深度学习、计算图或梯度,仅仅是一个通用的 n 维数组可用于任意数值计算。

numpy 数组与 PyTorch 张量最大的不同在于 PyTorch 张量既可以运行在 CPU 上,也可以运行在 GPU 上。要在 GPU 上运行操作,只需将张量转换为 cuda 数据类型。

99 788.6863403320312
199 536.3743896484375
299 366.064453125
399 250.9912567138672
499 173.16134643554688
599 120.4664535522461
699 84.75176239013672
799 60.51953887939453
899 44.06025695800781
999 32.86817169189453
1099 25.249109268188477
1199 20.056617736816406
1299 16.5137939453125
1399 14.093791007995605
1499 12.438772201538086
1599 11.305717468261719
1699 10.529045104980469
1799 9.996089935302734
1899 9.629949569702148
1999 9.378131866455078
Result: y = 0.02004171721637249 + 0.8429091572761536 x + -0.0034575294703245163 x^2 + -0.09136295318603516 x^3

import torch
import math


dtype = torch.float
device = torch.device("cpu")
# device = torch.device("cuda:0") # Uncomment this to run on GPU

# Create random input and output data
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
y = torch.sin(x)

# Randomly initialize weights
a = torch.randn((), device=device, dtype=dtype)
b = torch.randn((), device=device, dtype=dtype)
c = torch.randn((), device=device, dtype=dtype)
d = torch.randn((), device=device, dtype=dtype)

learning_rate = 1e-6
for t in range(2000):
    # Forward pass: compute predicted y
    y_pred = a + b * x + c * x ** 2 + d * x ** 3

    # Compute and print loss
    loss = (y_pred - y).pow(2).sum().item()
    if t % 100 == 99:
        print(t, loss)

    # Backprop to compute gradients of a, b, c, d with respect to loss
    grad_y_pred = 2.0 * (y_pred - y)
    grad_a = grad_y_pred.sum()
    grad_b = (grad_y_pred * x).sum()
    grad_c = (grad_y_pred * x ** 2).sum()
    grad_d = (grad_y_pred * x ** 3).sum()

    # Update weights using gradient descent
    a -= learning_rate * grad_a
    b -= learning_rate * grad_b
    c -= learning_rate * grad_c
    d -= learning_rate * grad_d


print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')

**脚本的总运行时间:**(0 分钟 0.144 秒)

画廊由 Sphinx-Gallery 生成

文档

访问 PyTorch 的详细开发者文档

查看文档

教程

获取针对初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并获得问题的解答

查看资源