当前位置:网站首页>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
边栏推荐
- Mysql database literacy, do you really know what a database is
- 从pdb源码到frame帧对象
- scratch旅行相册 电子学会图形化编程scratch等级考试一级真题和答案解析2022年6月
- StackOverflow 2022数据库年度调查
- 新品体验:阿里云新一代本地SSD实例i4开放公测
- Setup and upload of arduino-esp32 flash file plug-in program
- 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
- After failing in the college entrance examination, he entered Harbin Institute of technology, but stayed in the university after graduation to be an "Explorer". Ding Xiao: scientific research is accum
- yii2编写swoole的websocket服务
- The $980000 SaaS project failed
猜你喜欢

中二青年付杰的逆袭故事:从二本生到 ICLR 杰出论文奖,我用了20年

Copy 10 for one article! The top conference papers published in South Korea were exposed to be plagiarized, and the first author was "original sin"?

yii2编写swoole的websocket服务

腾讯汤道生:面向数实融合新世界,开发者是最重要的“建筑师”

Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”

Mysq 8.0 launched histogram, which greatly improved the performance!

Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性

嵌入式开发:估算电池寿命的7个技巧

Flutter series part: detailed explanation of GridView layout commonly used in flutter

pytorch基础
随机推荐
Forecast and Analysis on market scale and development trend of China's operation and maintenance security products in 2022
ShareIt has outstanding strength and landed in the top 7 of the global IAP strength list
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
一文抄 10 篇!韩国发表的顶级会议论文被曝抄袭,第一作者是“原罪”?
移动Web实训DAY-2
codeblocks mingw安装配置问题
从pdb源码到frame帧对象
How does Quanzhi v853 chip switch sensors on Tina v85x platform?
BUUCTF:[WUSTCTF2020]朴实无华
中二青年付杰的逆袭故事:从二本生到 ICLR 杰出论文奖,我用了20年
股票网上开户及开户流程怎样?手机开户是安全么?
数启扬帆,智聚人才 | 腾讯云数据库 & CSDN 工程师能力轻量认证发布会重磅来袭!...
How vscade sets auto save code
SHAREit實力出眾,登陸全球 IAP 實力榜 Top7
Centos6.5 php+mysql MySQL library not found
JS class is not just a simple syntax sugar!
flutter 系列之:flutter 中常用的 GridView layout 详解
Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
专业英语历年题
Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"