当前位置:网站首页>Pytorch Foundation
Pytorch Foundation
2022-06-28 13:23:00 【Gu_ NN】
Catalog
pytorch yes Facebook Developed deep learning library . Details visible Official website .
install
For the convenience of management , Usually in Anaconda or miniconda Create a new virtual environment installation in pytorch.
New virtual environment (MacOS)
Method 1 : direct Anaconda Navigator Install in
stay Navigator>Environments Create a new environment in 
Method 2 : Create... In the terminal
Open the terminal and input the following command :
# View the installed environment
conda env list
# install pytorch A virtual environment
conda create -n pytorch python==3.7
Other commands related to virtual environment operation :
# Delete the specified virtual environment
conda remove -n pytorch --all
# Activate pytorch A virtual environment
conda activate pytorch
# Exit virtual environment
conda deactivate
install pytorch
mac It can be installed directly pytorch, The installation order can be found on the official website .
Check whether the installation is successful
import torch
torch.cuda.is_available()
The return value is False Then for CPU edition .
Basic concepts
Tensor
tensor (Tensor) It's a vector 、 Matrix direction n Expansion of dimension , yes pytorch Data storage in 、 The main tool of transformation .
establish Tensor
| function | give an example | explain |
|---|---|---|
| tensor(data) | x = torch.tensor([1,2]) | Generate a 1 D tensor [1,2] |
| empty(data) | x = torch.empty(3,2) | Generate a 3 That's ok 2 Column space tensor |
| ones(sizes) | x = torch.ones(3,2) | x = [ 1 1 1 1 1 1 ] x=\begin{bmatrix}1 & 1 \\1 & 1\\1 & 1\end{bmatrix} x=⎣⎡111111⎦⎤ |
| zeros(sizes) | x = torch.zeros(3,2) | x = [ 0 0 0 0 0 0 ] x=\begin{bmatrix}0 & 0 \\0 & 0 \\0 & 0\end{bmatrix} x=⎣⎡000000⎦⎤ |
| eye(sizes) | x = torch.eye(3,2) | x = [ 1 0 0 1 0 0 ] x=\begin{bmatrix}1 & 0 \\0 & 1 \\0 & 0\end{bmatrix} x=⎣⎡100010⎦⎤ |
| rand(sizes) | x = torch.rand(3,2) | Random generation obeys [0,1) Evenly distributed 3 That's ok 2 Column tensor |
| randn(sizes) | x = torch.randn(3,2) | Random generation obeys N(0,1) Normal distribution 3 That's ok 2 Column tensor |
| randperm(m) | x = torch.randperm(3) | Random 1 dimension [0,6) Integers are arranged randomly |
operation
| operation | explain | function |
|---|---|---|
| Add | x+y | z = x + y |
| z = torch.add(x,y) | ||
| w = torch.empty(0) torch.add(x,y,out =w) | ||
| y.add_(x) | ||
| Subtraction | x-y | z = x - y |
| z = torch.sub(x,y) | ||
| Multiplication | Multiply the corresponding position elements | z = x * y |
| z = torch.mul(x,y) | ||
| division | The corresponding position element is divided by | z = x / y |
| z = torch.div(x,y) | ||
| Matrix cross multiplication | Cross riding | z = x @ y |
| z = torch.mm(x,y) | ||
| General multiplication | x,y Are all 1 dimension : Point multiplication x,y Are all 2 dimension : Differential multiplication x,y There is 1 A for 1 Another dimension is greater than 1 position : Trigger broadcast mechanism | z = torch.matmul(x,y) |
notes : Broadcast mechanism For when two shapes 不 Identical Tensor When operating by element , First copy the elements appropriately so that the two Tensor After the shape is the same, operate according to the element .
Other operating
| function | give an example | explain |
|---|---|---|
| view(shape) | x.view(2,3) | Turn the original 3 That's ok 2 The tensor of a column x Change it to 2 That's ok 3 Column |
| item() | x[0,0].item() | return x The first 1 That's ok 1 Column element values , Not Tensor Format |
autograd
autograd yes pytorch Chinese vs Tensor A package that performs automatic derivation . When Tensor.requires_grad=True when , Will automatically track the Tensor All operations . The default is False.
Basic application process
# Generate a tracking tensor
x = torch.ones(2, 2, requires_grad=True)
y = x**2
out = y.sum()
# Back propagation
out.backward()
# Output derivative
print(x.grad)
# New operation
out2 = x.sum()
out2.backward()
print(x.grad) # Will accumulate out.backward() result
# Gradient clear
out3 = x.sum()
x.grad.data.zero_()
out3.backward()
print(x.grad)
Block tracing
- use with torch.no_grad()
print(x.requires_grad)
print((x ** 2).requires_grad)
with torch.no_grad():
print((x ** 2).requires_grad)
- use Tensor.data Carry out operations
x = torch.ones(1,requires_grad=True)
print(x.data) # Or a tensor
print(x.data.requires_grad) # return False
y = 2 * x
x.data *= 100 # Only changed the value , It won't be recorded on the calculation chart , So it doesn't affect gradient propagation
y.backward()
print(x.grad)
Parallel computing
It is said that 1.12 Version of pytorch Will support mac GPU edition .
Network partitioning
Divide the model into several parts , Different parts are put in different places GPU
Layer-wise partitioning
Continue to divide the same part of the model into different tasks , In different GPU Calculate at the same time
Data parallelism( Main stream )
Split data , In different GPU Run different data on the same model
Reference resources
datawhale: Explain profound theories in simple language pytorch
边栏推荐
- 电脑无线网络不显示网络列表应该如何解决
- CodeBlocks MinGW installation configuration problem
- Tencent tangdaosheng: facing the new world of digital and real integration, developers are the most important "architects"
- Data analysis - promoter evolution analysis
- matlab plotyy 坐标轴设置,[转载]Matlab plotyy画双纵坐标图实例[通俗易懂]
- 2.01 backpack problem
- pytorch基础
- 股票网上开户及开户流程怎样?手机开户是安全么?
- Class structure in C language - dot
- scratch旅行相册 电子学会图形化编程scratch等级考试一级真题和答案解析2022年6月
猜你喜欢

Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform

数启扬帆,智聚人才 | 腾讯云数据库 & CSDN 工程师能力轻量认证发布会重磅来袭!...

Arduino-ESP32闪存文件插件程序搭建和上传

移动Web实训-flex布局测试题1

Fs7022 scheme series fs4059a dual two lithium battery series charging IC and protection IC
![Buuctf:[wustctf2020] plain](/img/0f/a7973d3f7593f2464e48609e27d7bd.png)
Buuctf:[wustctf2020] plain

Mobile web training day-2

pytorch模型

Setup and upload of arduino-esp32 flash file plug-in program

1015. picking flowers
随机推荐
Complete backpack beginner chapter "suggestions collection"
Vscode如何设置代码保存后自动格式化
895. 最长上升子序列
List集合转数组
哈希竞猜游戏系统开发技术成熟案例及源码
##测试bug常用“Redmine”
移动Web实训DAY-2
SHAREit實力出眾,登陸全球 IAP 實力榜 Top7
How to find opportunities in a bear market?
基于SSM实现水果蔬菜商城管理系统
FS7022方案系列FS4059A双节两节锂电池串联充电IC和保护IC
The press conference of Tencent cloud Database & CSDN engineer's ability lightweight certification is coming
SHAREit实力出众,登陆全球 IAP 实力榜 Top7
How to set auto format after saving code in vscade
求职简历的书写技巧
Shareit a une force exceptionnelle et se connecte au top 7 de la liste mondiale des forces IAP
我呕血收集融合了来自各路经典shell书籍的脚本教学,作为小白的你快点来吧
The $980000 SaaS project failed
flex布局中的align-content属性
The counter attack story of Fu Jie, a young secondary school student: I spent 20 years from the second undergraduate to the ICLR outstanding Thesis Award