Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-micrograd

A concise, readable Java implementation of Andrej Karpathy's micrograd ideas: scalar autograd + tiny neural nets (Neuron → Layer → MLP) built directly on a dynamic computation graph.

Inspired by Andrej Karpathy's micrograd - Vibe coded my way to understand NN fundamentals.

  • Core: Value nodes build a graph at runtime and backprop through it
  • NN building blocks: Neuron, Layer, MLP
  • Demos: exact micrograd replica and an enhanced classifier
  • Visualizations: computation graphs and decision boundaries

Project goal: Learn-by-building. Show the full path from scalars to a working MLP with clear code.

Quick Start

Prerequisites

  • JDK 21+

Build and Test

./gradlew build
./gradlew test

Run Demos

# Basic computation graph demo (Application)
./gradlew run

# Exact micrograd-style classifier (console output + optional boundary image)
java -cp build/classes/java/main com.vaibhavkhare.ml.autograd.demo.MicrogradReplicaDemo

# Enhanced classifier with extras (BCE, gradient clipping, circles dataset)
java -cp build/classes/java/main com.vaibhavkhare.ml.autograd.demo.AdvancedClassifierDemo

# Neuron/Value walkthrough with computation graph images
java -cp build/classes/java/main com.vaibhavkhare.ml.autograd.demo.NeuronValueExample

Outputs

  • Computation graphs (PNG) if Graphviz is available (e.g., neuron_forward_pass.png)
  • Decision boundary images from demos (see the Demos & Visualization guide)

Features

  • Autograd: add, sub, mul, div, pow, neg; tanh/relu/sigmoid/exp/log ops
  • Backprop: reverse topological traversal with gradient accumulation
  • Neural nets: dense Neuron, Layer, MLP with configurable activations
  • Visualization: computation graph rendering via Graphviz

Project Layout

src/main/java/com/vaibhavkhare/ml/autograd/
  ActivationType.java  GraphVisualizer.java  Layer.java  MLP.java
  Module.java          Neuron.java           Value.java
  demo/
    Application.java             # Graph basics + neuron demo
    MicrogradReplicaDemo.java    # Exact micrograd-style classifier
    AdvancedClassifierDemo.java  # Enhanced classifier (extras)
    NeuronValueExample.java      # Step-by-step Value/Neuron walkthrough + images

Visualization (Optional)

  • Images render even without native Graphviz via a JS fallback (you may see console warnings).
  • Optional: Install Graphviz for faster/native rendering and fewer warnings.
    • macOS: brew install graphviz

Attribution

My thoughts

About

Inspired by Andrej's Micrograd - learning the basics of NN by vibe coding

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages