当前位置:网站首页>1.1 pytorch and neural network
1.1 pytorch and neural network
2022-04-23 07:21:00 【sunshinecxm_ BJTU】
The first 1 Chapter PyTorch And neural networks
1.1 PyTorch introduction
1.1.2 PyTorch tensor
1.1.3 PyTorch Automatic derivation mechanism
1.1.4 Calculation chart
Automatic gradient calculation seems magical , But it's not magic .
The principle behind it is worthy of in-depth understanding , This knowledge will help us build larger networks .
Take a look at this very simple network . It's not even a neural network , It's just a series of calculations .

In the diagram above , We see the input x Used to calculate y,y Then used to calculate the output z.
hypothesis y and z The calculation process of is as follows :

If we want to know the output z z z How to follow x x x change , We need to know the gradient d y / d x dy/dx dy/dx. Let's calculate step by step .

The first line is the chain rule of calculus (chain rule), Very important to us .
We just figured out , z z z along with x x x The change of can be expressed as 4 x 4x 4x. If x = 3.5 x = 3.5 x=3.5, be d z / d x = 4 × 3.5 = 14 dz/dx = 4 × 3.5 = 14 dz/dx=4×3.5=14.
When y y y With x x x The formal definition of , and z z z With y y y The formal definition of ,PyTorch Then connect these tensors into a picture , To show how these tensors are connected . This picture is called the calculation chart (computation graph).
In our case , The calculation diagram may look like the following :

We can see y y y How is from x x x Calculated , z z z How is from y y y Calculated . Besides ,PyTorch Several reverse arrows have also been added , Express y y y How to follow x x x change , z z z How to follow y y y change . These are gradients , It is used to update the neural network in the training process . The process of calculus consists of PyTorch complete , There is no need for us to calculate .
To work out z z z How to follow x x x change , We merged from z z z Through y y y go back to x x x All gradients in the path of . This is the chain rule of calculus .

PyTorch There is only one forward connection graph . We need to pass backward() function , send PyTorch Calculate the reverse gradient .

gradient dz/dx In tensor x Is stored as x.grad.

It is worth noting that , tensor x x x The internal gradient value is the same as z It's about . This is because we ask PyTorch Use z.backward () from z z z Reverse calculation . therefore , x . g r a d x.grad x.grad yes d z / d x dz/dx dz/dx, instead of d y / d x dy/dx dy/dx.
Most effective neural networks contain multiple nodes , Each node has multiple links connected to the node , And the link from this node . Let's take a simple example , The node in the example has multiple incoming links .

so , Input a a a and b b b At the same time x x x and y y y Have an impact on , And output z z z By x x x and y y y Calculated .
The relationship between these nodes is as follows .

We calculate the gradient in the same way .

next , Add this information to the calculation diagram .

Now? , We can easily pass z To a Calculate the gradient of the path dz/da. actually , from z To a There are two paths , One goes through x, The other one goes through y, We just need to add the expressions of the two paths . This is reasonable , Because from a To z Both paths are affected z Value , This is also the same as what we calculated with the chain rule of calculus dz/da The result is the same .
d z / d a = d z / d x + d x / d a + d z / d y + d y / d a dz/da = dz/dx+dx/da +dz/dy+dy/da dz/da=dz/dx+dx/da+dz/dy+dy/da

The first path goes through x x x, Expressed as 2 × 2 2 × 2 2×2; The second path goes through y y y, Expressed as 3 × 10 a 3×10a 3×10a. therefore , z z z along with a a a The rate of change is 4 + 30 a 4 + 30a 4+30a.
If a a a yes 2, be d z / d a dz/da dz/da yes 4 + 30 × 2 = 64.
Let's test it with PyTorch Whether this value can also be obtained . First , We define PyTorch The relationships needed to build the calculation diagram .

next , We trigger the gradient calculation and query the tensor a a a The value of the inside .


Effective neural networks are usually much larger than this small network . however PyTorch The way to construct the calculation diagram and the process of calculating the gradient backward along the path are the same .
1.1.5 Learning points
- Colab The service allows us to run on Google's servers Python Code .Colab Use Python The notebook , We only need one Web Browser ready to use .
- PyTorch It's a leading Python Machine learning architecture . It is associated with numpy similar , Allows us to use an array of numbers . meanwhile , It also provides a rich set of tools and functions , Make machine learning easier .
- stay PyTorch in , The basic unit of data is tensor (tensor). Tensors can be multidimensional arrays 、 Simple two-dimensional matrix 、 One dimensional list , It can also be a single value .
- PyTorch Its main feature is that it can automatically calculate the gradient of the function (gradient). The calculation of gradient is the key to training neural network . So ,PyTorch You need to build a calculation diagram (computationgraph), The graph contains multiple tensors and the relationship between them . In the code , This process is automatically completed when we define another tensor with one tensor .
版权声明
本文为[sunshinecxm_ BJTU]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610529584.html
边栏推荐
猜你喜欢

MySQL数据库安装与配置详解
![[2021 book recommendation] kubernetes in production best practices](/img/78/2b5bf03adad5da9a109ea5d4e56b18.png)
[2021 book recommendation] kubernetes in production best practices

【2021年新书推荐】Learn WinUI 3.0

Ffmpeg common commands
![[2021 book recommendation] effortless app development with Oracle visual builder](/img/db/d8802b42d5374e4117db638a0b82b0.png)
[2021 book recommendation] effortless app development with Oracle visual builder

ArcGIS license server administrator cannot start the workaround

【点云系列】Learning Representations and Generative Models for 3D pointclouds

【点云系列】PnP-3D: A Plug-and-Play for 3D Point Clouds
树莓派:双色LED灯实验

Chapter 1 numpy Foundation
随机推荐
[point cloud series] a rotation invariant framework for deep point cloud analysis
WebView displays a blank due to a certificate problem
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
常用UI控件简写名
读书小记——Activity
【 planification dynamique】 différentes voies 2
【点云系列】Fully-Convolutional geometric features
What did you do during the internship
【動態規劃】不同路徑2
ArcGIS license server administrator cannot start the workaround
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
MySQL notes 4_ Primary key auto_increment
Gee configuring local development environment
【点云系列】SO-Net:Self-Organizing Network for Point Cloud Analysis
torch. mm() torch. sparse. mm() torch. bmm() torch. Mul () torch The difference between matmul()
C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
MySQL的安装与配置——详细教程
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
[Point Cloud Series] SG - Gan: Adversarial Self - attachment GCN for Point Cloud Topological parts Generation