什么是Pytorch?
Pytorch是一个基于Python的科学计算软件包,针对两组受众:
- 替代NumPy以使用GPU的功能
- 提供最大灵活性和速度的深度学习研究平台
Getting Started
Tensors
Tensors(张量)类似于NumPy的ndarrays,此外,Tensors还可以在GPU上使用以加速计算。
from __future__ import print_function import torch
注意
声明了一个未初始化的矩阵,但在使用前不包含确定的已知值。在创建未初始化的矩阵时,当时分配的内存中的任何值都将显示为初始值。
构造一个未初始化的5x3矩阵:
x = torch.empty(5, 3) print(x)
输出:
tensor([[ 9.8348e+18, 4.5845e-41, -3.5873e-11], [ 3.0950e-41, 1.2141e-40, 3.8102e-38], [ 5.3741e-30, 4.8419e+30, 7.7765e+31], [ 4.7987e+30, 4.9796e-37, 2.1325e-41], [ 2.4230e+21, 1.6045e-37, 1.9106e-28]])
构造一个随机初始化的矩阵:
x = torch.rand(5, 3) print(x)
输出:
tensor([[0.8379, 0.5171, 0.5525], [0.5173, 0.8930, 0.0898], [0.9211, 0.8738, 0.9919], [0.9876, 0.0921, 0.1055], [0.7662, 0.4146, 0.9539]])
构造一个填充零且dtype 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])
或者基于现有张量创建张量。这些方法将重用输入张量的属性,例如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([[-0.2447, 0.9554, -1.1188], [-0.7718, 0.5454, -0.5616], [ 0.7332, -0.9550, 0.2147], [ 0.6549, -0.9197, 1.4548], [-0.5826, 0.9719, 0.1522]])
输出它的大小:
print(x.size())
输出:
torch.Size([5, 3])
注意
torch.Size
实际上是一个tuple,因此它支持所有tuple操作。
Operations
操作有多种语法。在下面的示例中,我们将看一下加法运算。
加法:语法1
y = torch.rand(5, 3) print(x + y)
输出:
tensor([[ 0.5103, 1.4621, -0.3477], [ 0.1262, 1.1215, -0.2381], [ 1.0119, -0.0862, 0.4582], [ 0.6862, -0.6686, 2.2950], [ 0.1223, 1.5743, 0.9786]])
加法:语法2
print(torch.add(x, y))
输出:
tensor([[ 0.5103, 1.4621, -0.3477], [ 0.1262, 1.1215, -0.2381], [ 1.0119, -0.0862, 0.4582], [ 0.6862, -0.6686, 2.2950], [ 0.1223, 1.5743, 0.9786]])
加法:提供输出张量作为参数
result = torch.empty(5, 3) torch.add(x, y, out=result) print(result)
输出:
tensor([[ 0.5103, 1.4621, -0.3477], [ 0.1262, 1.1215, -0.2381], [ 1.0119, -0.0862, 0.4582], [ 0.6862, -0.6686, 2.2950], [ 0.1223, 1.5743, 0.9786]])
加法:in-place
# adds x to y y.add_(x) print(y)
输出:
tensor([[ 0.5103, 1.4621, -0.3477], [ 0.1262, 1.1215, -0.2381], [ 1.0119, -0.0862, 0.4582], [ 0.6862, -0.6686, 2.2950], [ 0.1223, 1.5743, 0.9786]])
注意
任何使张量就地发生变化的操作都用固定_
。例如:x.copy_(y)
,x.t_()
,将改变x
。
numpy中所有index的用法在pytorch中都可以使用
print(x[:, 1])
输出:
tensor([ 0.9554, 0.5454, -0.9550, -0.9197, 0.9719])
调整大小:如果要调整张量的大小/形状,可以使用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())
输出:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果具有一个元素张量,请使用.item()
将该值作为Python数字获取:
x = torch.randn(1) print(x) print(x.item())
输出:
tensor([-1.9486]) -1.9485716819763184
100+张量的操作,包括移调,索引,切片,数学运算,线性代数,随机数等等,在这里描述。
NumPy Bridge
将Torch张量转换为NumPy数组,反之亦然,这很容易。
Torch Tensor和NumPy数组将共享其基础内存位置(如果Torch Tensor在CPU上),并且更改一个将更改另一个。
将Torch张量转换为NumPy数组
a = torch.ones(5) print(a)
输出:
tensor([1., 1., 1., 1., 1.])
b = a.numpy() print(b)
输出:
[1. 1. 1. 1. 1.]
看numpy数组的值如何变化。
a.add_(1) print(a) print(b)
输出:
tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.]
将NumPy数组转换为Torch张量
看更改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)
输出:
[2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
除了CharTensor之外,CPU上的所有张量都支持转换为NumPy并返回。
CUDA 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!
输出:
tensor([-0.9486], device='cuda:0') tensor([-0.9486], dtype=torch.float64)
接下来,给大家介绍一下租用GPU做实验的方法,我们是在智星云租用的GPU,使用体验很好。具体大家可以参考:智星云官网: http://www.ai-galaxy.cn/,淘宝店:https://shop36573300.taobao.com/公众号: 智星AI
参考文献: