小白入门PyTorch | 第一篇:什么是PyTorch?
什么是PyTorch?
這是一個基于Python的科學計算包,主要分入如下2部分:
- 使用GPU的功能代替numpy
- 一個深刻的學習研究平臺,提供最大的靈活性和速度
開始學習
Tensors (張量)
Tensors類似于numpy的ndarrays,另外還可以在GPU上使用Tensors來加速計算。
from __future__ import print_function import torch構造一個5x3矩陣,不初始化。
x = torch.empty(5, 3) print(x) tensor([[1.6932e+22, 7.7144e+31, 6.7109e+22],[1.6486e+22, 4.3605e+27, 2.8929e+12],[7.5338e+28, 1.8037e+28, 3.4740e-12],[1.7743e+28, 6.8239e+16, 1.8832e+34],[1.6078e+19, 4.4721e+21, 5.0789e-11]])構造一個隨機初始化的矩陣:
x = torch.rand(5, 3) print(x) tensor([[0.2712, 0.3545, 0.5300],[0.0976, 0.0149, 0.8799],[0.7187, 0.7343, 0.4521],[0.4418, 0.0132, 0.2708],[0.9201, 0.0794, 0.4476]])構造一個矩陣全為 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。
x = x.new_ones(5, 3, dtype=torch.double) print(x)x = torch.randn_like(x, dtype=torch.float) print(x) tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], dtype=torch.float64) tensor([[-1.2157, -0.6880, 0.3270],[-0.3162, -0.2479, 0.8731],[-0.3330, -0.3823, 0.5237],[-1.3132, -0.1246, 0.6706],[ 1.1174, -1.0695, 0.7972]])獲取它的維度信息:
print(x.size()) torch.Size([5, 3])注意
``torch.Size`` 是一個元組,所以它支持左右的元組操作。
操作
在接下來的例子中,我們將會看到加法操作。
加法: 方式 1
y = torch.rand(5, 3) print(x + y) tensor([[-0.4000, 0.0549, 1.2980],[ 0.0748, 0.5602, 1.2120],[ 0.1771, -0.1623, 1.4076],[-0.4690, 0.6656, 0.8570],[ 1.5434, -0.8243, 1.4676]])加法: 方式2
print(torch.add(x, y)) tensor([[-0.4000, 0.0549, 1.2980],[ 0.0748, 0.5602, 1.2120],[ 0.1771, -0.1623, 1.4076],[-0.4690, 0.6656, 0.8570],[ 1.5434, -0.8243, 1.4676]])加法: 提供一個輸出 tensor 作為參數
result = torch.empty(5, 3) torch.add(x, y, out=result) print(result) tensor([[-0.4000, 0.0549, 1.2980],[ 0.0748, 0.5602, 1.2120],[ 0.1771, -0.1623, 1.4076],[-0.4690, 0.6656, 0.8570],[ 1.5434, -0.8243, 1.4676]])加法: in-place
# adds x to y y.add_(x) print(y) tensor([[-0.4000, 0.0549, 1.2980],[ 0.0748, 0.5602, 1.2120],[ 0.1771, -0.1623, 1.4076],[-0.4690, 0.6656, 0.8570],[ 1.5434, -0.8243, 1.4676]])注意
任何使張量會發生變化的操作都有一個前綴 '_'。例如: ``x.copy_(y)``, ``x.t_()``, 將會改變 ``x``.
你可以使用標準的 NumPy 類似的索引操作
print(x[:, 1]) tensor([-0.6880, -0.2479, -0.3823, -0.1246, -1.0695])改變大小:如果你想改變一個 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()) torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])如果你有一個元素 tensor ,可以使用 .item() 來獲得這個 tensor 的值 。
x = torch.randn(1) print(x) print(x.item()) tensor([-0.4592]) -0.4592222571372986Numpy轉換
將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張量
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 上的所有 Tensors 與 NumPy 都可以相互轉化
CUDA Tensors
可以通過 .to 方法將 Tensors 轉移到任何設備
# 在GPU可用時運行 # 我們將使用 torch.device 對象將 tensors 移入、移出GPU if torch.cuda.is_available():device = torch.device("cuda") # 創建一共 CUDA設備對象y = torch.ones_like(x, device=device) # 在GPU上直接創建一個 tensorx = x.to(device) # 等價于:x = x.to("cuda")z = x + yprint(z)print(z.to("cpu", torch.double)) # .to() 將同時改變數據類型 tensor([0.5408], device='cuda:0') tensor([0.5408], dtype=torch.float64) 👇🏻掃一掃下方二維碼,獲取7900+本電子書👇🏻總結
以上是生活随笔為你收集整理的小白入门PyTorch | 第一篇:什么是PyTorch?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习100例 | 第28天:水果的识
- 下一篇: 深度学习100例 | 第53天:用YOL