

Source code analysis for Autograd
source link: https://www.tuicool.com/articles/hit/aQ7nye3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Autograd is a convenient tool to automatically differentiate native Python and Numpy code.
Let’s look at an example first:
import autograd.numpy as np from autograd import grad def f(x): return x * x + 1 grad_f = grad(f) print(grad_f(1.6))
The result is 3.2
f(x) = sqaure(x) + 1, its derivative is 2*x, so the result is correct.
Function grad() actually return a ‘function object’, which is ‘grad_f’. When we call grad_f(1.6), it will ‘trace’ f(x) by:

The ‘fun’ argument is our f(x) function.

In ‘trace()’, it acutually called f() without ‘x’ but a ArrayBox object. The ArrayBox object has two purposes:
1. Go through all the operations in f() along with ‘x’, so it chould get the real result of f(x)
2. Get all the corresponding gradients of operations in f()
ArrayBox class has already override all the basic arithmetic operations, such as add/sustract/multiply/divide/square. Therefore it can catch all the operations in f(x).

The gradients table is showed as below:

Otherwise, Autograd have other tricks to complete its work. Take function wrapper ‘@primitive’ as an example. This decorator make sure users could add new custom-defined-operation into Autograd.
The source code of Autograd is nice and neat. Its examples include fully-connected-network, CNN, even RNN. Let’s take a glimpse of the implement of Adam optimizer of Autograd to feel its concise code style:

Recommend
-
35
我们知道,深度学习最核心的其中一个步骤,就是求导:根据函数(linear + activation function)求weights相对于loss的导数(还是loss相对于weights的导数?)。然后根据得出的导数,相应的修改weights,让loss最小化。 各大深度学习框架T...
-
7
Open Source at Microsoft: an analysis of Microsoft Open Source StrategyI have been invited to the Open Source in Mobile Conference to give a spee...
-
21
Top 5 Open Source Source and Free Static Code Analysis Tools in 2020 Last Updated: 19-08-2020If you are a software developer or a code security analyst you often need to analyze your source code to de...
-
10
Welcome to another blog post about the journey of Octo, the open source tool for static site generation! Today I will be going over how I added static analysis tooling into the project and...
-
6
Walmart and eBay Electronic Brand Analysis Using SerpAPI ...
-
5
“PyTorch - Variables, functionals and Autograd.” Feb 9, 2018 Variables A Variable wraps a Tensor. It supports nearly all the API’s defined by a T...
-
5
Autograd解析|OneFlow学习笔记
-
5
深扒torch.autograd原理 精选 原创 当我们训练神经网络的时候,最常用的就是使用反向传播。在反向传播过程中,参数变化只和给定参...
-
4
本文深入探讨了 PyTorch 中 Autograd 的核心原理和功能。从基本概念、Tensor 与 Autograd 的交互,到计算图的构建和管理,再到反向传播和梯度计算的细节,最后涵盖了 Autograd 的高级特性。 关注TechLead,分享AI全...
-
10
Fastest Autograd in the West Dec 28, 2023 • Alex Rogozhnikov • Who needs fast autograd? Seemingly everyone these days! And once upon a time I needed an autograd that is act...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK