目录:
一、PyTorch是什么
它是一个基于Python的科学计算包,有以下两个目标:
- 作为NumPy的替代者,充分发挥GPU的能力
- 一个深度学习研究平台,提供最大的灵活性和速度
二、入门
1、Tensor
Tensor
类似于 NumPy
中的 ndarray
,除此之外,tensor
能够运行在 GPU上以加速计算。
创建tensor
- 创建一个5x3的未初始化的矩阵:
x = torch.empty(5,3)
print(x)
输出:
tensor([[ 1.8918e-16, 4.5644e-41, -3.0675e-26],
[ 3.0773e-41, 0.0000e+00, 1.4013e-45],
[ 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)
输出
tensor([[0.5524, 0.8042, 0.8140],
[0.2944, 0.1345, 0.2921],
[0.3099, 0.3343, 0.7377],
[0.0685, 0.1461, 0.9683],
[0.8908, 0.4492, 0.9506]])
- 创建一个零矩阵,类型为
long
x = torch.zeros(5,3, dtype=torch.long)
print(x)
输出
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
- 从数据创建
x = torch.tensor([5.5,3])
print(x)
输出:
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
输出:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[-1.9325, 1.2030, -2.1360],
[-0.1392, 1.2079, 1.3194],
[-1.0496, 0.9343, 2.4428],
[-0.3567, -0.5195, -0.0276],
[ 1.8236, 0.1193, -0.5549]])
生成的 tensor
的大小:
print(x.size())
输出:
torch.Size([5, 3])
注意
torch.Size
是一个元组,支持所有的元组操作。
2、 运算
对于运算有许多的语法,接下来,以加法运算为例
- 语法1
y = torch.rand(5,3)
print(x + y)
输出:
tensor([[-1.8346, 1.6518, -1.2673],
[ 0.1698, 1.8538, 2.2153],
[-0.0803, 0.9863, 3.2740],
[-0.0906, -0.3752, 0.8960],
[ 1.8271, 0.8586, -0.0483]])
- 语法2
print(torch.add(x, y))
输出:
tensor([[-1.8346, 1.6518, -1.2673],
[ 0.1698, 1.8538, 2.2153],
[-0.0803, 0.9863, 3.2740],
[-0.0906, -0.3752, 0.8960],
[ 1.8271, 0.8586, -0.0483]])
- 提供输出
tensor
作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
输出:
tensor([[-1.8346, 1.6518, -1.2673],
[ 0.1698, 1.8538, 2.2153],
[-0.0803, 0.9863, 3.2740],
[-0.0906, -0.3752, 0.8960],
[ 1.8271, 0.8586, -0.0483]])
- 原地操作
y.add_(x) # adds x to y
print(y)
输出:
tensor([[-1.8346, 1.6518, -1.2673],
[ 0.1698, 1.8538, 2.2153],
[-0.0803, 0.9863, 3.2740],
[-0.0906, -0.3752, 0.8960],
[ 1.8271, 0.8586, -0.0483]])
注意
任何原地改变
tensor
的操作以下划线(_)作为后缀,例如:x.copy_(y),x.t_()
- 可以使用标准的
NumPy
索引操作
print(x[:, 1])
输出:
tensor([ 1.2030, 1.2079, 0.9343, -0.5195, 0.1193])
- 使用
torch.view
改变tensor
的大小和形状
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())
输出:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
- 对于只包含一个元素的
tensor
,使用.item()
将值作为Python
数字
x = torch.randn(1)
print(x)
print(x.item())
输出:
tensor([-0.7521])
-0.7521063685417175
三、Torch tensor & NumPy array
Torch tensor
和 Numpy array
之间的转换非常简单。
Torch tensor
和 Numpy array
将共享底层的内存位置(如果 torch tensor
在 CPU
上),改变其中的一个,另一个也会改变。
- 将
torch tensor
转换为NumPy array
a = torch.ones(5)
print(a)
输出:
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
输出:
[1. 1. 1. 1. 1.]
看一看 NumPy array
的值如何改变
a.add_(1)
print(a)
print(b)
输出:
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
- 将
NumPy array
转化为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)
输出:
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
除了 CharTensor
外,CPU
上的所有tensor
和 NumPy array
均可相互转化。
四、CUDA tensors
使用 .to
方法可以将 tensor
移动到任何设备上。
# 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!
输出:
tensor([0.2479], device='cuda:0')
tensor([0.2479], dtype=torch.float64)