什么是PyTorch?
2019年年初,ApacheCN組織志愿者翻譯了PyTorch1.0版本中文文檔(github地址),同時也獲得了PyTorch官方授權,我相信已經有許多人在中文文檔官網上看到了。不過目前校對還缺人手,希望大家踴躍參與。之前一段時間我們和PyTorch的有關負責人Bruce Lin一直在進行郵件交流。在之后適當的時候,我們會組織志愿者進行其他有關PyTorch的項目,歡迎大家加入我們,關注我們。更希望我們的一系列工作能夠對大家有所幫助。
譯者:bat67
校對者:FontTian
作者: Soumith Chintala
PyTorch是一個基于python的科學計算包,主要針對兩類人群:
- 作為NumPy的替代品,可以利用GPU的性能進行計算
- 作為一個高靈活性、速度快的深度學習平臺
入門
張量
Tensor(張量)類似于NumPy的ndarray,但還可以在GPU上使用來加速計算。
from __future__ import print_function import torch創建一個沒有初始化的5*3矩陣:
x = torch.empty(5, 3) print(x)輸出:
tensor([[2.2391e-19, 4.5869e-41, 1.4191e-17],[4.5869e-41, 0.0000e+00, 0.0000e+00],[0.0000e+00, 0.0000e+00, 0.0000e+00],[0.0000e+00, 0.0000e+00, 0.0000e+00],[0.0000e+00, 0.0000e+00, 0.0000e+00]])創建一個隨機初始化矩陣:
x = torch.rand(5, 3) print(x)輸出:
tensor([[0.5307, 0.9752, 0.5376],[0.2789, 0.7219, 0.1254],[0.6700, 0.6100, 0.3484],[0.0922, 0.0779, 0.2446],[0.2967, 0.9481, 0.1311]])構造一個填滿0且數據類型為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])或者根據已有的tensor建立新的tensor。除非用戶提供新的值,否則這些方法將重用輸入張量的屬性,例如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) # 重載 dtype! print(x) # 結果size一致輸出:
tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], dtype=torch.float64) tensor([[ 1.6040, -0.6769, 0.0555],[ 0.6273, 0.7683, -0.2838],[-0.7159, -0.5566, -0.2020],[ 0.6266, 0.3566, 1.4497],[-0.8092, -0.6741, 0.0406]])獲取張量的形狀:
print(x.size())輸出:
torch.Size([5, 3])注意:
torch.Size本質上還是tuple,所以支持tuple的一切操作。
運算
一種運算有多種語法。在下面的示例中,我們將研究加法運算。
加法:形式一
y = torch.rand(5, 3) print(x + y)輸出:
tensor([[ 2.5541, 0.0943, 0.9835],[ 1.4911, 1.3117, 0.5220],[-0.0078, -0.1161, 0.6687],[ 0.8176, 1.1179, 1.9194],[-0.3251, -0.2236, 0.7653]])加法:形式二
print(torch.add(x, y))輸出:
tensor([[ 2.5541, 0.0943, 0.9835],[ 1.4911, 1.3117, 0.5220],[-0.0078, -0.1161, 0.6687],[ 0.8176, 1.1179, 1.9194],[-0.3251, -0.2236, 0.7653]])加法:給定一個輸出張量作為參數
result = torch.empty(5, 3) torch.add(x, y, out=result) print(result)輸出:
tensor([[ 2.5541, 0.0943, 0.9835],[ 1.4911, 1.3117, 0.5220],[-0.0078, -0.1161, 0.6687],[ 0.8176, 1.1179, 1.9194],[-0.3251, -0.2236, 0.7653]])加法:原位/原地操作(in-place)
# adds x to y y.add_(x) print(y)輸出:
tensor([[ 2.5541, 0.0943, 0.9835],[ 1.4911, 1.3117, 0.5220],[-0.0078, -0.1161, 0.6687],[ 0.8176, 1.1179, 1.9194],[-0.3251, -0.2236, 0.7653]])注意:
任何一個in-place改變張量的操作后面都固定一個_。例如x.copy_(y)、x.t_()將更改x
也可以使用像標準的NumPy一樣的各種索引操作:
print(x[:, 1])輸出:
tensor([-0.6769, 0.7683, -0.5566, 0.3566, -0.6741])改變形狀:如果想改變形狀,可以使用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])如果是僅包含一個元素的tensor,可以使用.item()來得到對應的python數值
x = torch.randn(1) print(x) print(x.item())輸出:
tensor([0.0445]) 0.0445479191839695后續閱讀:
超過100種tensor的運算操作,包括轉置,索引,切片,數學運算,
線性代數,隨機數等,具體訪問這里
橋接 NumPy
將一個Torch張量轉換為一個NumPy數組是輕而易舉的事情,反之亦然。
Torch張量和NumPy數組將共享它們的底層內存位置,因此當一個改變時,另外也會改變。
將torch的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張量
看改變NumPy數組是如何自動改變Torch張量的:
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)CPU上的所有張量(CharTensor除外)都支持與Numpy的相互轉換。
CUDA上的張量
張量可以使用.to方法移動到任何設備(device)上:
# 當GPU可用時,我們可以運行以下代碼 # 我們將使用`torch.device`來將tensor移入和移出GPU if torch.cuda.is_available():device = torch.device("cuda") # a CUDA device objecty = torch.ones_like(x, device=device) # 直接在GPU上創建tensorx = x.to(device) # 或者使用`.to("cuda")`方法z = x + yprint(z)print(z.to("cpu", torch.double)) # `.to`也能在移動時改變dtype輸出:
tensor([1.0445], device='cuda:0') tensor([1.0445], dtype=torch.float64)總結
以上是生活随笔為你收集整理的什么是PyTorch?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虚拟机 centos 6.5 扩展根目录
- 下一篇: Autograd:自动求导