Navigate back to the homepage

Coding a neural network from scratch

Sudaraka Jayathilaka
June 6th, 2024 · 2 min read

Learning how AI works on a deeper level has always been in my bucket list. I found this amazing video by Andrej Karpathy which walks you through the process of creating an autograd engine first and then move on to create a very basic Neural Network. So here is my experience following the video.

Autograd Engine

nn image source

A Neural Network is basically a graph. once we give an input, the neural network returns an output. Each of these neurons have certain parameters which impacts the output directly. We train neural networks to output a desired value by tweaking these parameters in neurons. But how can we derive the parameter values that could cause the neural network to output the desired value?.

Turns out, old friend calculus comes to help here. We can approximate the impact of a certain parameter on the output of the Neural Network, by calculating the gradient of the parameter with respect to the output.

Autograd engine helps us to build a Neural Network (modeled as a graph), and easily do backpropagation to calculate the gradients of each parameter. Hence the name Autograd Engine.

Tiny Grad

Tiny grad is the engine I build. This is a very basic Auto Grad engine which

  • Supports implementing Simple Neural networks
  • Only works with scalaer inputs
  • Supports backpropagating through the neural network and calculating gradient values for each parameter

I did a tiny experiment with tinygrad, where I created a simple neural netowrk and trained it to predict a given input. I was simply playing with a set of numbers, but it was quite fun. You can find my jupyter notebook below,

Micrograd

The tinygrad engine is mostly inspired by Andrej Karpathi’s Micrograd, Autograd Engine.

article banner source

More articles from Sudaraka Jayathilaka

Handling EI_EXPOSE_REP & EI_EXPOSE_REP2

SpotBugs is a great tool for static code analysis. Recently I got two similar warnings in one of the codebases I work on and I had to fix it…

April 29th, 2024 · 3 min read

GatsbyJS for Dummies

Recently I wanted to revamp my personal website. While in the quest for the Ideal template, I came found this super clean-looking template…

April 10th, 2024 · 4 min read
© 2021–2024 Sudaraka Jayathilaka
Link to $mailto:sudarakayasindu@gmail.comLink to $https://github.com/sudaraka94Link to $https://www.linkedin.com/in/sudarakajayathilaka/Link to $https://x.com/sudaraka94Link to $https://medium.com/@sudarakayasindu