WELCOME TO PYTORCH TUTORIALS
要学习如何使用PyTorch,请从入门教程开始。60分钟的闪电战是最常见的起点,并提供了如何使用PyTorch从基础到构建深度神经网络的广泛视角。
注意事项:
- 如果您希望通过IPython / Jupyter交互式地学习教程,那么每个教程都有一个用于Jupyter笔记本和Python源代码的下载链接。PyTorch示例中还提供了其他高质量的示例,包括图像分类、无监督学习、增强学习、机器翻译和许多其他应用程序。
- 您可以在PyTorch文档或通过内联帮助找到PyTorch API和层的参考文档。
- 如果您希望教程部分得到改进,请在这里打开一个github问题和您的反馈。
- 查看我们的PyTorch备考单以获得更多有用的信息。
- 最后,这里是到PyTorch发行说明的链接
本教程的目标:
- 高层次地理解PyTorch张量库和神经网络
- 训练一个小的神经网络对图像进行分类
本教程假设您对numpy有基本的了解
确保安装了torch和torchvision包。
WHAT IS PYTORCH?
这是一个基于python的科学计算程序包,针对两类用户:
- 代替NumPy使用gpu的功能。
- 深度学习研究平台,提供最大的灵活性和速度。
Getting Started
Tensors
Tensors类似于NumPy s ndarrays,此外,张量还可以用于GPU加速计算。
from __future__ import print_function
import torch
构造一个5x3矩阵,未初始化:
x = torch.empty(5, 3)
print(x)
Out:
tensor([[-1.1015e-07, 4.5807e-41, -3.5803e-06],
[ 4.5807e-41, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00]])
构造一个随机初始化的矩阵:
x = torch.rand(5, 3)
print(x)
Out:
tensor([[0.9727, 0.8805, 0.5857],
[0.1284, 0.4006, 0.1293],
[0.5041, 0.9665, 0.4347],
[0.0748, 0.1356, 0.5269],
[0.2129, 0.0561, 0.2891]])
构造一个填充0和long类型矩阵:
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
Out:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
直接从数据构造tensor:
x = torch.tensor([5.5, 3])
print(x)
Out:
tensor([5.5000, 3.0000])
或者在现有tensor的基础上创建一个tensor。这些方法将重用输入tensor的属性,例如dtype,除非用户提供新的值
x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes
print(x)
x = torch.randn_like(x, dtype=torch.float) # override dtype!
print(x) # result has the same size
Out:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.3323, -0.8246, 1.5832],
[ 0.1779, -0.0341, -0.3968],
[-0.4179, 1.3846, 1.0981],
[-1.5426, -0.4654, -0.6574],
[-0.9417, -0.0177, -1.1593]])
获得它的大小:
print(x.size())
Out:
torch.Size([5, 3])
NOTE
torch.Size实际上是一个元组,所以它支持所有的元组操作。
Operations
操作有多种语法。在下面的示例中,我们将研究加法操作。
加法:语法1
y = torch.rand(5, 3)
print(x + y)
Out:
tensor([[ 0.4512, -0.4297, 2.0196],
[ 1.0648, 0.4395, 0.4832],
[ 0.2238, 1.5926, 1.7444],
[-1.1602, -0.1060, 0.1966],
[-0.4518, 0.6022, -0.3636]])
加法: 语法 2
print(torch.add(x, y))
Out:
tensor([[ 0.4512, -0.4297, 2.0196],
[ 1.0648, 0.4395, 0.4832],
[ 0.2238, 1.5926, 1.7444],
[-1.1602, -0.1060, 0.1966],
[-0.4518, 0.6022, -0.3636]])
加法:提供一个输出tensor作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
Out:
tensor([[ 0.4512, -0.4297, 2.0196],
[ 1.0648, 0.4395, 0.4832],
[ 0.2238, 1.5926, 1.7444],
[-1.1602, -0.1060, 0.1966],
[-0.4518, 0.6022, -0.3636]])
加法:就地
# adds x to y
y.add_(x)
print(y)
Out:
tensor([[ 0.4512, -0.4297, 2.0196],
[ 1.0648, 0.4395, 0.4832],
[ 0.2238, 1.5926, 1.7444],
[-1.1602, -0.1060, 0.1966],
[-0.4518, 0.6022, -0.3636]])
NOTE
任何改变tensor的操作都是用_后固定的。例如:x.copy_(y)、x.t_()将更改x。
您可以使用标准的类似于numpy的索引,其中包含所有附加功能!
print(x[:, 1])
Out:
tensor([-0.8246, -0.0341, 1.3846, -0.4654, -0.0177])
调整大小:如果你想调整tensor的大小/形状,你可以使用torch.view:
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
Out:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果您有一个单元素tensor,那么使用.item()来获得作为Python数字的值
x = torch.randn(1)
print(x)
print(x.item())
Out:
tensor([0.9806])
0.9805544018745422
NumPy Bridge
将Torch Tensor转换为NumPy array,反之亦然,这是一件轻而易举的事。
Torch Tensor和NumPy array将共享它们的底层内存位置,更改一个将更改另一个。
Converting a Torch Tensor to a NumPy Array
a = torch.ones(5)
print(a)
Out:
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
Out:
[1. 1. 1. 1. 1.]
查看numpy数组的值是如何变化的。
a.add_(1)
print(a)
print(b)
Out:
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
Converting NumPy Array to Torch Tensor
查看如何改变np数组自动改变Torch Tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
Out:
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
CPU上除char张量外的所有张量都支持转换为NumPy和back。
CUDA Tensors
Tensors可以使用.to方法移动到任何设备上。
# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA device object
y = torch.ones_like(x, device=device) # directly create a tensor on GPU
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
Out:
tensor([1.9806], device='cuda:0')
tensor([1.9806], dtype=torch.float64)