当前位置:网站首页>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
边栏推荐
- Stackoverflow 2022 database annual survey
- align-items 与 align-content 的区别
- Stm32f1 and stm32cubeide programming example - matrix keyboard driver
- 4年用户数破亿,孙哥带领波场再创新高
- The press conference of Tencent cloud Database & CSDN engineer's ability lightweight certification is coming
- 数启扬帆,智聚人才 | 腾讯云数据库 & CSDN 工程师能力轻量认证发布会重磅来袭!...
- Centos7:切换mysql用户并登录mysql
- Realization of a springboard machine
- Hisilicon 35xx realizes gt911 touch screen function "suggestions collection"
- Recognize the startup function and find the user entry
猜你喜欢

基于SSM实现水果蔬菜商城管理系统

(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)

Unit test ci/cd
![[today in history] June 28: musk was born; Microsoft launches office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launches office 365; The inventor of Chua's circuit was born

Mysq 8.0 launched histogram, which greatly improved the performance!

Fh511+tp4333 form an outdoor mobile power lighting camping lamp scheme.

Mobile web training day-1

How does Quanzhi v853 chip switch sensors on Tina v85x platform?

New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta

Implementation of fruit and vegetable mall management system based on SSM
随机推荐
哈希竞猜游戏系统开发技术成熟案例及源码
pytorch主要模块
无心剑英译朱熹《观书有感二首·其一》
How to handle the safest account opening when downloading the mobile app of Huatai Securities
Vscode shortcut key
STM32F1与STM32CubeIDE编程实例-矩阵键盘驱动
Matlab plotyy coordinate axis setting, [reprint] example of MATLAB plotyy drawing double ordinate graph [easy to understand]
##测试bug常用“Redmine”
pytorch模型调参、训练相关内容
Centos7 - installing mysql5.7
China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
Vscode如何设置自动保存代码
Pytorch main modules
一文抄 10 篇!韩国发表的顶级会议论文被曝抄袭,第一作者是“原罪”?
Stackoverflow 2022 database annual survey
Deep understanding of Bayes theorem
Elastic box auto wrap demo
新品体验:阿里云新一代本地SSD实例i4开放公测
Writing skills of resume
Vs2012 VC creates a new blank window application