Pytorch是python的一个科学计算包,主要有两方面的好处:
(1)它可以替代NumPy,充分的利用GPU的算力;
(2)是一个深度学习的研究平台,提供最大的灵活性和速度。
下面开始学习记录:
Example:
import torch #导入pytorch
#Construct a 5X3 matrix, uninitialized:
x = torch.empty(5, 3)
print(x)
输出:
tensor([[ 3.2401e+18, 0.0000e+00, 1.3474e-08],
[ 4.5586e-41, 1.3476e-08, 4.5586e-41],
[ 1.3476e-08, 4.5586e-41, 1.3474e-08],
[ 4.5586e-41, 1.3475e-08, 4.5586e-41],
[ 1.3476e-08, 4.5586e-41, 1.3476e-08]])
Example 2:
import torch
Construct a tensor directly from data:
x = torch.tensor([5.5, 3])
print(x)
输出:
tensor([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]])
Example 3:
import tensor
Construct a tensor directly from data:
x = torch.tensor([5.5, 3])
print(x)
输出:
tensor([ 5.5000, 3.0000])
Example 4:
import torch
#Create a tensor based on an existing tensor. These methods will reuse properties of the input tensor,
#e.g. dtype, unless new values are provided by user.
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.2641, 0.0149, 0.7355],
[ 0.6106, -1.2480, 1.0592],
[ 2.6305, 0.5582, 0.3042],
[-1.4410, 2.4951, -0.0818],
[ 0.8605, 0.0001, -0.7220]])
Example 5:
import torch
#Gets its size
print(x.size())
输出
torch.Size([5, 3])
torch.Size
is in fact a tuple, so it supports all tuple operations.
对于操作存在许多语法,下面的例子主要展现一些额外的操作。
Addition: syntax 1
y = torch.rand(5, 3)
print(x + y)
Out:
tensor([[ 0.7355, 0.2798, 0.9392],
[ 1.0300, -0.6085, 1.7991],
[ 2.8120, 1.2438, 1.2999],
[-1.0534, 2.8053, 0.0163],
[ 1.4088, 0.9000, -0.1172]])
Addition: syntax 2
print(torch.add(x+y))
Out:
tensor([[ 0.7355, 0.2798, 0.9392],
[ 1.0300, -0.6085, 1.7991],
[ 2.8120, 1.2438, 1.2999],
[-1.0534, 2.8053, 0.0163],
[ 1.4088, 0.9000, -0.1172]])
Addition: syntax 3
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
Out:
tensor([[ 0.7355, 0.2798, 0.9392],
[ 1.0300, -0.6085, 1.7991],
[ 2.8120, 1.2438, 1.2999],
[-1.0534, 2.8053, 0.0163],
[ 1.4088, 0.9000, -0.1172]])
Addition:
# adds x to y
y.add_(x)
print(y)
Out:
tensor([[ 0.7355, 0.2798, 0.9392],
[ 1.0300, -0.6085, 1.7991],
[ 2.8120, 1.2438, 1.2999],
[-1.0534, 2.8053, 0.0163],
[ 1.4088, 0.9000, -0.1172]])
Any operation that mutates a tensor in-place is post-fixed with an _
. For example: x.copy_(y)
, x.t_()
, will change x
.
print(x[:, 1]) #和numpy的操作一样
Out:
tensor([ 0.0149, -1.2480, 0.5582, 2.4951, 0.0001])
如果想要resize/reshape 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()获得它的值:
x = torch.randn(1)
print(x)
print(x.item())
Out:
tensor([ 1.3159])
1.3159412145614624
torch Tensor和NumPy的array相互转换,它们公用一个内存地址,改变一个另外一个也会发生改变。
(1)Torch Tensor转换为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.]
改变a看b的变化:
a.add_(1)
print(a)
print(b)
Out:
tensor([ 2., 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)
Out:
[2. 2. 2. 2. 2.]
tensor([ 2., 2., 2., 2., 2.], dtype=torch.float64)
tensor可以使用.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([ 2.3159], device='cuda:0')
tensor([ 2.3159], dtype=torch.float64)