Skip to content

FazalHussain/NeuralNetworkBasics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🧠 NeuralNetworkBasics

Python NumPy License Status

Building neural networks from first principles β€” no magic, no black boxes.

A hands-on implementation of computation graphs, manual derivative calculations, and backpropagation using pure calculus and the chain rule.


✨ What This Project Is About

Most deep learning tutorials hand you PyTorch or TensorFlow and say "trust the .backward() call."

This project does the opposite.

Every gradient is computed by hand, every node in the computation graph is explicitly differentiated, and every chain rule application is written out in code. The goal is to build genuine intuition for how automatic differentiation actually works under the hood β€” before letting any framework do it for you.

"Everything became much clearer when I started ignoring full-page dense derivations and just started writing code." β€” Andrej Karpathy


🌿 Branch: feature/manual_derivative_calculation_for_each_node

This branch is the mathematical core of the project. It implements:

  • A Value class that wraps scalars and tracks operations to form a computation graph
  • Manual derivative formulas for each operation node using calculus rules
  • Local gradients for direct parent–child relationships
  • Chain rule propagation to carry gradients backwards through indirect dependencies
  • Numerical gradient verification β€” comparing analytical derivatives against finite differences to catch errors
  • The foundation for automatic differentiation (autograd) built from scratch

πŸ—ΊοΈ Concepts Covered

1. The Value Class & Computation Graph

Each scalar in the network is a Value node. When you perform operations on Value objects, they silently record the computation graph β€” who produced whom, and which operation was used.

a = Value(2.0, label='a')
b = Value(-3.0, label='b')
c = Value(10.0, label='c')
e = a*b; e.label = 'e'
d = e + c; d.label = 'd'
f = Value(-2.0, label='f')
L = d * f; L.label = 'L'

This builds a directed acyclic graph (DAG) of operations β€” exactly what modern autograd engines use internally.


2. Manual Derivative Calculation

For each node, the local gradient (how much the node's output changes with respect to each of its inputs) is derived analytically using calculus rules:

Operation Local Gradient
z = a + b βˆ‚z/βˆ‚a = 1, βˆ‚z/βˆ‚b = 1
z = a * b βˆ‚z/βˆ‚a = b, βˆ‚z/βˆ‚b = a
z = a ** n βˆ‚z/βˆ‚a = n Β· aⁿ⁻¹
z = tanh(a) βˆ‚z/βˆ‚a = 1 βˆ’ tanhΒ²(a)
z = exp(a) βˆ‚z/βˆ‚a = exp(a)
z = a / b βˆ‚z/βˆ‚a = 1/b, βˆ‚z/βˆ‚b = βˆ’a/bΒ²

No approximations. No finite differences. Pure calculus.


3. Chain Rule Propagation

The chain rule is what makes backpropagation possible. For any intermediate node e in the graph:

dL/da = (dL/de) Β· (de/da)

In code, this means every node multiplies the gradient flowing in from its children by its own local gradient, then passes the result upstream to its parents.

This accumulation (using +=) is critical β€” a single node can feed multiple downstream paths, so gradients from all paths must be summed.


4. Numerical Gradient Verification

To validate that our analytical derivatives are correct, we compare them against numerical derivatives computed via finite differences:

df/dx β‰ˆ (f(x + h) - f(x)) / h     where h β†’ 0

If the analytical and numerical gradients match (within floating point tolerance), the implementation is correct. This is a standard technique used in industry to unit-test gradient implementations.


πŸ—οΈ Project Structure

NeuralNetworkBasics/
β”‚
β”œβ”€β”€ init.ipynb    # All concepts: Value class, computation graph, manual backprop & gradient verification
└── README.md

Everything lives in a single self-contained notebook β€” no scattered scripts, no dependencies to wire up. Open it and run top to bottom.


πŸš€ Getting Started

Prerequisites

pip install numpy matplotlib jupyter

Clone & Run

git clone https://github.com/FazalHussain/NeuralNetworkBasics.git
cd NeuralNetworkBasics
git checkout feature/manual_derivative_calculation_for_each_node

jupyter notebook init.ipynb

πŸ“ The Math Behind It

Chain Rule (Multi-Variable)

For a node Z that depends on y, which depends on x:

Derivative

Derivative Calculation

πŸ“– References: Chain Rule β€” Wikipedia Β· Derivative β€” Wikipedia


πŸŽ“ Learning Path

This project follows the philosophy of learning by building. The recommended progression:

1. Understand scalar derivatives         
2. Build the Value class                 
3. Construct a small expression graph   
4. Derive local gradients manually      
5. Apply chain rule backwards
6. Verify with numerical gradients      
7. Generalize β†’ autograd engine         β†’  (next milestone)

πŸ™ Acknowledgements

This project is deeply inspired by Andrej Karpathy's legendary lecture series on building neural networks from scratch:

πŸ“Ί The spelled-out intro to neural networks and backpropagation: building micrograd

Karpathy's micrograd project demonstrates that a complete autograd engine β€” capable of training real neural networks β€” can be implemented in under 100 lines of Python. This repository walks through the same ideas with additional annotation, manual derivation steps, and verification tooling.


πŸ—ΊοΈ Roadmap

  • Value class with computation graph tracking
  • Manual derivative calculation for each operation node
  • Chain rule propagation (manual backpropagation)
  • Numerical gradient verification
  • Generalized backward() pass (topological sort)
  • Neuron, Layer, and MLP classes built on top of Value
  • Training a small classifier end-to-end
  • Visualization of computation graphs

πŸ‘¨β€πŸ’» Author

Syed Fazal Hussain Naqvi Senior Android Engineer & Technical Lead Β· AI Engineering Enthusiast

GitHub


"To understand backpropagation, you must first understand differentiation. To understand differentiation, you must write it out yourself."

⭐ Star this repo if it helped you understand the math behind neural networks.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors