小白入门PyTorch | 第一篇:什么是PyTorch?
什么是PyTorch?
這是一個(gè)基于Python的科學(xué)計(jì)算包,主要分入如下2部分:
- 使用GPU的功能代替numpy
- 一個(gè)深刻的學(xué)習(xí)研究平臺(tái),提供最大的靈活性和速度
開始學(xué)習(xí)
Tensors (張量)
Tensors類似于numpy的ndarrays,另外還可以在GPU上使用Tensors來加速計(jì)算。
from __future__ import print_function import torch構(gòu)造一個(gè)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]])構(gòu)造一個(gè)隨機(jī)初始化的矩陣:
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]])構(gòu)造一個(gè)矩陣全為 0,而且數(shù)據(jù)類型是 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]])構(gòu)造一個(gè)張量,直接使用數(shù)據(jù):
x = torch.tensor([5.5, 3]) print(x) tensor([5.5000, 3.0000])創(chuàng)建一個(gè) tensor 基于已經(jīng)存在的 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`` 是一個(gè)元組,所以它支持左右的元組操作。
操作
在接下來的例子中,我們將會(huì)看到加法操作。
加法: 方式 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]])加法: 提供一個(gè)輸出 tensor 作為參數(shù)
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]])注意
任何使張量會(huì)發(fā)生變化的操作都有一個(gè)前綴 '_'。例如: ``x.copy_(y)``, ``x.t_()``, 將會(huì)改變 ``x``.
你可以使用標(biāo)準(zhǔn)的 NumPy 類似的索引操作
print(x[:, 1]) tensor([-0.6880, -0.2479, -0.3823, -0.1246, -1.0695])改變大小:如果你想改變一個(gè) 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])如果你有一個(gè)元素 tensor ,可以使用 .item() 來獲得這個(gè) tensor 的值 。
x = torch.randn(1) print(x) print(x.item()) tensor([-0.4592]) -0.4592222571372986Numpy轉(zhuǎn)換
將Tensor轉(zhuǎn)換為numpy數(shù)組
a = torch.ones(5) print(a) tensor([1., 1., 1., 1., 1.]) b = a.numpy() print(b) [1. 1. 1. 1. 1.]看看numpy數(shù)組的值如何變化。
a.add_(1) print(a) print(b) tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.]將 numpy 數(shù)組轉(zhuǎn)換為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 都可以相互轉(zhuǎn)化
CUDA Tensors
可以通過 .to 方法將 Tensors 轉(zhuǎn)移到任何設(shè)備
# 在GPU可用時(shí)運(yùn)行 # 我們將使用 torch.device 對(duì)象將 tensors 移入、移出GPU if torch.cuda.is_available():device = torch.device("cuda") # 創(chuàng)建一共 CUDA設(shè)備對(duì)象y = torch.ones_like(x, device=device) # 在GPU上直接創(chuàng)建一個(gè) tensorx = x.to(device) # 等價(jià)于:x = x.to("cuda")z = x + yprint(z)print(z.to("cpu", torch.double)) # .to() 將同時(shí)改變數(shù)據(jù)類型 tensor([0.5408], device='cuda:0') tensor([0.5408], dtype=torch.float64) 👇🏻掃一掃下方二維碼,獲取7900+本電子書👇🏻總結(jié)
以上是生活随笔為你收集整理的小白入门PyTorch | 第一篇:什么是PyTorch?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习100例 | 第28天:水果的识
- 下一篇: 深度学习100例 | 第53天:用YOL