Pytorch
基於Python的科學計算包
- 作爲Numpy的替代品,可以使用GPU的強大計算能力
- 提供強大的靈活性和高速的深度學習研究平臺
Tensors(張量)
Tensors與Numpy的ndarray類似,但是Tensors可以使用GPU計算
# 創建一個5x5矩陣,未初始化
import torch
x = torch.empty(5,5)
x
tensor([[9.9184e-39, 9.0000e-39, 1.0561e-38, 1.0653e-38, 4.1327e-39],
[8.9082e-39, 9.8265e-39, 9.4592e-39, 1.0561e-38, 9.7347e-39],
[9.1837e-39, 8.4490e-39, 9.5511e-39, 1.0469e-38, 8.7245e-39],
[8.9082e-39, 1.0653e-38, 8.4490e-39, 1.1112e-38, 9.2755e-39],
[9.5511e-39, 9.1837e-39, 1.0561e-38, 9.1837e-39, 9.6429e-39]])
# 創建隨機初始化矩陣
x = torch.randn(2, 3)
x
tensor([[ 0.1443, 0.4900, 0.0978],
[-0.6755, 0.1361, -0.1632]])
# 創建0填充矩陣,類型long
x = torch.zeros(2, 3, dtype=torch.long)
x
tensor([[0, 0, 0],
[0, 0, 0]])
# 創建tensor並使用現有數據
x = torch.tensor([5.5, 3, 2])
x
tensor([5.5000, 3.0000, 2.0000])
# 根據現有的張量創建張量,可設置新的值進行覆蓋
x = x.new_ones(3, 2, dtype=torch.double)
print(x)
y = torch.rand_like(x, dtype=torch.float)
print(y)
tensor([[1., 1.],
[1., 1.],
[1., 1.]], dtype=torch.float64)
tensor([[0.5680, 0.2010],
[0.9147, 0.4793],
[0.3011, 0.6881]])
# 獲取size
x.size()
torch.Size([3, 2])
注:使用size()方法與Numpy的shape屬性相同,張量也支持reshape屬性,“torch.size返回的是touple類型,所以支持touple操作”
# 加法
y = torch.rand(3,2, dtype=torch.double)
print(x + y)
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
torch.add(x, y)
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
res = torch.empty(3, 2, dtype=torch.double)
torch.add(x, y, out=res)
res
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
y.add_(x)
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
注:任何以_結尾的操作都會用結果替換變量
x[:, -1]
tensor([1., 1., 1.], dtype=torch.float64)
# view:可以改變張量的維度和大小
x = torch.randn(2, 3)
y = x.view(6)
z = x.view(-1, 6)
print(x.size(), y.size(), z.size())
torch.Size([2, 3]) torch.Size([6]) torch.Size([1, 6])
# .item()
x = torch.randn(1)
print(x)
print(x.item())
tensor([-1.3655])
-1.365473747253418
Numpy轉換
a = torch.ones(5)
a
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
b
array([1., 1., 1., 1., 1.], dtype=float32)
a.add_(1)
a
tensor([2., 2., 2., 2., 2.])
b
array([2., 2., 2., 2., 2.], dtype=float32)
# from_numpy
import numpy as np
a = np.ones(4)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
[2. 2. 2. 2.]
tensor([2., 2., 2., 2.], dtype=torch.float64)
cuda張量
- torch.cuda.is_avaliable() 判斷設備是否有cuda可用,torch.device將張量移動到設備中
if torch.cuda.is_available():
device = torch.device('cuda') # CUDA設備
y = torch.ones_like(x, device=device) # 從GPU創建張量
x = x.to(device) # 將張量移動到cuda中
z = x + y
print(z)
print(z.to('cpu'), torch.double) # .to 可對變量做更改
tensor([-0.3655], device='cuda:0')
tensor([-0.3655]) torch.float64