当前位置:网站首页>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
边栏推荐
- 【2021年新书推荐】Enterprise Application Development with C# 9 and .NET 5
- Cancel remote dependency and use local dependency
- JNI中使用open打开文件是返回-1问题
- [2021 book recommendation] red hat rhcsa 8 cert Guide: ex200
- Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
- Chapter 1 numpy Foundation
- 电脑关机程序
- Binder mechanism principle
- 5种方法获取Torch网络模型参数量计算量等信息
- Write a wechat double open gadget to your girlfriend
猜你喜欢
【点云系列】Relationship-based Point Cloud Completion
Easyui combobox 判断输入项是否存在于下拉列表中
[point cloud series] a rotation invariant framework for deep point cloud analysis
Bottomsheetdialogfragment conflicts with listview recyclerview Scrollview sliding
Pymysql connection database
[point cloud series] sg-gan: advantageous self attention GCN for point cloud topological parts generation
【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
Visual studio 2019 installation and use
Android interview Online Economic encyclopedia [constantly updating...]
随机推荐
第5 章 机器学习基础
Pymysql connection database
【动态规划】不同路径2
GEE配置本地开发环境
PyTorch 模型剪枝实例教程三、多参数与全局剪枝
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
ThreadLocal,看我就够了!
WebRTC ICE candidate里面的raddr和rport表示什么?
MySQL notes 5_ Operation data
第1章 NumPy基础
【点云系列】Relationship-based Point Cloud Completion
【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
face_recognition人脸检测
torch_ Geometric learning 1, messagepassing
Pytorch best practices and coding style guide
torch. mm() torch. sparse. mm() torch. bmm() torch. Mul () torch The difference between matmul()
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
第4章 Pytorch数据处理工具箱
Keras如何保存、加载Keras模型
[3D shape reconstruction series] implicit functions in feature space for 3D shape reconstruction and completion