The goal of this project is to create a mini auto gradient engine. We will do this in two steps:
- Create the
Value
data structure that keeps track of the computational graph when you use the default math operator+
,*
,**
, etc. - Implement the backpropagation algorithm using the computational graph.
When you are done, you can create a computational graph and calculates its gradients using the following API:
x_1 = Value(3)
x_2 = Value(4)
x_3 = x_1 * x_2
x_4 = Value(5)
x_5 = x_4 + x_3
x_6 = Value(2)
l = x_5 * x_6
l.backwards()
A Visualization of the above computational graph and its gradients.
You do not need to remember much from calculus classes. As long as you can remember the following derivative rules you should be fine.
Name | Function | Derivative |
---|---|---|
Definition | - | |
(Unrelated) constant rule | ||
Sum rule |
|
|
Multiplication rule |
|
|
Constant multiplication rule | ||
Power rule | ||
Chain rule | - | |
tanh | tanh(x) |
First install the project using:
poetry run install
You might need to tell poetry where to find a python 3.9 interpreter using:
poetry env use /path/to/python
In this exercise we are going to implement a data structure that keeps track of the computational graph.
This data structure will be implemented in the Value
class in src/mini_auto_grad/engine.py
.
To implement this functionality you have to do the following:
- Implement the arithmetic logic by using the
__add__
,____radd__
,__pow__
,__mul__
, etc such that the returned value has the correctdata
attribute. - Ensure that the new
Value
instance that is returned by the arithmetic method keeps track of the children that created it. E.g.x = a + b
thena
andb
will be children ofx
.
You can verify that you have implemented everything correctly using:
poetry run pytest -m ex1
In this exercise we are going to implement the back propagation algorithm in the backwards
method of the Value
class.
This algorithm does the following:
- Call the
backwards
method on the loss leaf node. - Set the gradient of this loss leaf node to
1
since$\frac{\partial L}{\partial L} = 1$ . - Find the revered topological order of the computation graph using dfs.
- Propagate the gradient backwards by calling the
_backwards(grad)
function.
To implement this algorithm you need to do the following:
- Make sure that arithmetic functions (
__add__
,____radd__
,__pow__
,__mul__
, etc) pass the correct_backwards
function to their parent node. - Finish the
find_reversed_topological_order
function insrc/mini_auto_grad/engine.py
.
You can verify if you have implemented everything correctly using:
poetry run pytest -m ex2
We have a working auto grad engine, lets see if we can use it to build a small MLP that can solve the XOR problem.
We have set up this MLP in learning process in tests/test_nn.py
.
To implement this functionality you have to do the following in src/test_nn.py
:
- Finish the implementation of
Neuron
. This should be a function$f(x) = b + \sum_{i=0}^N x_i * w_i$ whereby$b$ and$w_i$ are instances ofValue
. - Finish the implementation of
Layer
. This function that takes$n$ inputs and uses$m$ Neuron
functions to map it to$m$ output features.
You can verify if you have implemented everything correctly using:
poetry run pytest -m ex3