机器学习入门 —— PyTorch | 原创,AI翻译
PyTorch
开始安装。此版本支持 Python 3.9
。
$ pip install torch torchvision
Collecting torch
Downloading torch-1.8.0-cp39-none-macosx_10_9_x86_64.whl (120.6 MB)
|████████████████████████████████| 120.6 MB 224 kB/s
Collecting torchvision
Downloading torchvision-0.9.0-cp39-cp39-macosx_10_9_x86_64.whl (13.1 MB)
|████████████████████████████████| 13.1 MB 549 kB/s
Requirement already satisfied: numpy in /usr/local/lib/python3.9/site-packages (from torch) (1.20.1)
Collecting typing-extensions
Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.9/site-packages (from torchvision) (8.0.1)
Installing collected packages: typing-extensions, torch, torchvision
Successfully installed torch-1.8.0 torchvision-0.9.0 typing-extensions-3.7.4.3
验证安装。
import torch
x = torch.rand(5, 3)
print(x)
出现错误。
Traceback (most recent call last):
File "torch.py", line 1, in <module>
import torch
File "torch.py", line 2, in <module>
x = torch.rand(5, 3)
AttributeError: partially initialized module 'torch' has no attribute 'rand' (most likely due to a circular import)
通过搜索错误信息,发现我们的文件也命名为 torch
,导致命名冲突。重命名后可正常运行。
tensor([[0.5520, 0.9446, 0.5543],
[0.6192, 0.0908, 0.8726],
[0.0223, 0.7685, 0.9814],
[0.4019, 0.5406, 0.3861],
[0.5485, 0.6040, 0.2387]])
查看一个示例。
# -*- coding: utf-8 -*-
import torch
import math
dtype = torch.float
device = torch.device("cpu")
# device = torch.device("cuda:0") # 取消注释以使用 GPU 运行
# 创建随机输入和输出数据
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
y = torch.sin(x)
# 随机初始化权重
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):
# 前向传播:计算预测的 y
y_pred = a + b * x + c * x ** 2 + d * x ** 3
# 计算并打印损失
loss = (y_pred - y).pow(2).sum().item()
if t % 100 == 99:
print(t, loss)
# 反向传播:计算 a, b, c, d 关于损失的梯度
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()
# 使用梯度下降更新权重
a -= learning_rate * grad_a
b -= learning_rate * grad_b
c -= learning_rate * grad_c
d -= learning_rate * grad_d
print(f'结果:y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')
运行代码。
99 1273.537353515625
199 849.24853515625
299 567.4786987304688
399 380.30291748046875
499 255.92752075195312
599 173.2559814453125
699 118.2861328125
799 81.72274780273438
899 57.39331817626953
999 41.198158264160156
1099 30.41307830810547
1199 23.227672576904297
1299 18.438262939453125
1399 15.244369506835938
1499 13.113286972045898
1599 11.690631866455078
1699 10.740333557128906
1799 10.105220794677734
1899 9.6804780960083
1999 9.39621353149414
结果:y = -0.011828352697193623 + 0.8360244631767273 x + 0.002040589228272438 x^2 + -0.09038365632295609 x^3
查看仅使用 numpy
库的代码示例。
# -*- coding: utf-8 -*-
import numpy as np
import math
# 创建随机输入和输出数据
x = np.linspace(-math.pi, math.pi, 2000)
y = np.sin(x)
# 随机初始化权重
a = np.random.randn()
b = np.random.randn()
c = np.random.randn()
d = np.random.randn()
learning_rate = 1e-6
for t in range(2000):
# 前向传播:计算预测的 y
# y = a + b x + c x^2 + d x^3
y_pred = a + b * x + c * x ** 2 + d * x ** 3
# 计算并打印损失
loss = np.square(y_pred - y).sum()
if t % 100 == 99:
print(t, loss)
# 反向传播:计算 a, b, c, d 关于损失的梯度
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()
# 更新权重
a -= learning_rate * grad_a
b -= learning_rate * grad_b
c -= learning_rate * grad_c
d -= learning_rate * grad_d
print(f'结果:y = {a} + {b} x + {c} x^2 + {d} x^3')
注意到这两种实现方式是不同的计算方式。
在这些示例中,首先生成一组 x 和 y 值。然后假设其为三次方程,再通过某些方法迭代计算系数。这些算法是什么?注意到它循环 2000
次,每次迭代都更精确。这里不展开细节。
最后
目前我们还不了解机器学习背后的计算原理,但这并不重要。有了以上知识,我们已经可以做很多事情。机器学习还可以用于处理文本、音频等。在探索几十个示例后,再学习理论也不迟。
练习
- 学生应按上述方式进行探索。