%matplotlib inline
什麼是PyTorch
這是一個基於Python的科學計算軟件包,針對兩組受衆:
-替代NumPy以使用GPU的功能
-提供最大靈活性和速度的深度學習研究平臺
入門
1. Tensors
Tensors與NumPy的ndarrays類似,此外,Tensors還可以在GPU上使用以加速計算。
from __future__ import print_function
import torch
注意
聲明瞭一個未初始化的矩陣,但在使用前不包含確定的已知值。創建未初始化的矩陣時,當時分配的內存中的任何值都將顯示爲初始值
- 構造一個未初始化的5x3矩陣:
x = torch.empty(5, 3)
print(x)
tensor([[8.9082e-39, 1.0194e-38, 9.1837e-39],
[8.4490e-39, 1.0102e-38, 1.0561e-38],
[9.9184e-39, 8.9082e-39, 1.0102e-38],
[9.9184e-39, 9.0000e-39, 1.0561e-38],
[1.0653e-38, 4.1327e-39, 8.9082e-39]])
- 構造一個隨機初始化的矩陣:
x = torch.rand(5, 3)
print(x)
tensor([[0.9027, 0.0830, 0.1839],
[0.2781, 0.0128, 0.1412],
[0.9696, 0.2547, 0.3788],
[0.3861, 0.4127, 0.3459],
[0.1689, 0.3855, 0.8316]])
- 構造一個填充零且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([[ 1.2456, -0.0769, -0.2649],
[ 1.4525, -1.4273, -1.3667],
[-0.6583, -1.2476, -0.3553],
[-0.9376, -1.0969, -1.0023],
[-0.8310, -0.2706, -2.3210]])
Get its size:
print(x.size())
torch.Size([5, 3])
注意
torch.Size 實際上是一個元組,因此它支持所有元組操作。
2. 運算方式
操作有多種語法。在下面的示例中,我們將看一下加法運算。
- 加法:語法1
y = torch.rand(5, 3)
print(x + y)
tensor([[ 1.7615, 0.4198, 0.6486],
[ 1.9849, -0.7055, -1.1323],
[ 0.1995, -0.5580, 0.3914],
[-0.7944, -0.2280, -0.9123],
[-0.1418, 0.2377, -2.1498]])
- 加法:語法2
print(torch.add(x, y))
tensor([[ 1.7615, 0.4198, 0.6486],
[ 1.9849, -0.7055, -1.1323],
[ 0.1995, -0.5580, 0.3914],
[-0.7944, -0.2280, -0.9123],
[-0.1418, 0.2377, -2.1498]])
- 加法:提供輸出張量作爲參數
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[ 1.7615, 0.4198, 0.6486],
[ 1.9849, -0.7055, -1.1323],
[ 0.1995, -0.5580, 0.3914],
[-0.7944, -0.2280, -0.9123],
[-0.1418, 0.2377, -2.1498]])
- 加法:就地
# adds x to y
y.add_(x)
print(y)
tensor([[ 1.7615, 0.4198, 0.6486],
[ 1.9849, -0.7055, -1.1323],
[ 0.1995, -0.5580, 0.3914],
[-0.7944, -0.2280, -0.9123],
[-0.1418, 0.2377, -2.1498]])
注意
任何使張量就地變化的操作都用固定_。例如:`x.copy_(y)`,`x.t_()`,將改變x。
print(x[:, 1])
tensor([-0.0769, -1.4273, -1.2476, -1.0969, -0.2706])
- 調整大小:如果要調整張量的大小/形狀,可以使用
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.0134])
1.0134013891220093
稍後閱讀:
100+張量的操作,包括移調,索引,切片,數學運算,線性代數,隨機數等等,被描述 在這裏.
3. NumPy Bridge
將Torch張量轉換爲NumPy數組,反之亦然,這很容易。
Torch Tensor和NumPy數組將共享其基礎內存位置(如果Torch Tensor在CPU上),並且更改一個將更改另一個。
將Tensor轉換爲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並返回。
4. 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([2.0134], device='cuda:0')
tensor([2.0134], dtype=torch.float64)