WHAT IS PYTORCH?
import torch
import numpy as np
入门
张量(Tensors)
创建 5 × 3 5 \times 3 5×3未初始化矩阵
x = torch.empty(5, 3)
print(x)
tensor([[8.4490e-39, 1.1112e-38, 1.0194e-38],
[9.0919e-39, 8.4490e-39, 9.6429e-39],
[8.4490e-39, 9.6429e-39, 9.2755e-39],
[1.0286e-38, 9.0919e-39, 8.9082e-39],
[9.2755e-39, 8.4490e-39, 1.0194e-38]])
创建 5 × 3 5 \times 3 5×3随机初始化矩阵
x = torch.rand(5, 3)
print(x)
tensor([[0.2298, 0.3465, 0.0192],
[0.6144, 0.9847, 0.0743],
[0.9962, 0.8674, 0.8179],
[0.6407, 0.0486, 0.9379],
[0.3696, 0.8083, 0.7895]])
创建 5 × 3 5 \times 3 5×3长整型全零矩阵
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])
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.4860, -0.0354, -0.0974],
[-0.8735, -2.2988, -0.1597],
[-0.2047, 1.4410, 0.1788],
[ 2.4593, -0.1532, -1.1023],
[-0.6821, -0.7906, 0.4597]])
get size
print(x.size())
torch.Size([5, 3])
运算(operations)
加法(1)
y = torch.randn(5, 3)
print(x + y)
tensor([[ 0.8827, 0.0793, 0.1161],
[-0.6054, -2.1206, 0.4906],
[ 0.6030, -0.2212, -0.5874],
[ 0.9548, 0.4672, -2.1705],
[ 0.5627, -1.3957, -0.1847]])
加法(2)
print(torch.add(x, y))
tensor([[ 0.8827, 0.0793, 0.1161],
[-0.6054, -2.1206, 0.4906],
[ 0.6030, -0.2212, -0.5874],
[ 0.9548, 0.4672, -2.1705],
[ 0.5627, -1.3957, -0.1847]])
加法(指定输出张量)
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[ 0.8827, 0.0793, 0.1161],
[-0.6054, -2.1206, 0.4906],
[ 0.6030, -0.2212, -0.5874],
[ 0.9548, 0.4672, -2.1705],
[ 0.5627, -1.3957, -0.1847]])
加法(in-place)
y.add_(x)
print(y)
tensor([[ 0.8827, 0.0793, 0.1161],
[-0.6054, -2.1206, 0.4906],
[ 0.6030, -0.2212, -0.5874],
[ 0.9548, 0.4672, -2.1705],
[ 0.5627, -1.3957, -0.1847]])
运算加后缀“_”表示in-place
索引
print(x[:, 1])
tensor([-0.0354, -2.2988, 1.4410, -0.1532, -0.7906])
reshape
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
获取元素
x = torch.randn(1)
print(x)
print(x.item())
tensor([0.6939])
0.6939241290092468
Numpy
Torch张量–>Numpy数组
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
a.add_(1)
print(a)
print(b)
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
Numpy数组–>Torch张量
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)
除字符张量外,所有张量都支持与NumPy类型相互转换。
CUDA张量
# 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([1.6939], device='cuda:0')
tensor([1.6939], dtype=torch.float64)