Skip to content
RohanGudurPublic

About

numpy based autograd engine

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

numpygrad

A minimal NumPy-based autograd engine with a Tensor core, common activation functions, and simple losses. Inspired by micrograd by the goat Andrej Karpathy.

numpygrad preview

Install / Setup

  • Requires Python 3.9+
  • No external deps besides NumPy
pip install -r requirements.txt

Run examples from the project root:

python -m example.test1
python -m example.test2
python -m example.test3

Quickstart

from src import Tensor, Activations, Losses
import numpy as np

# Tensors from numpy arrays, lists/tuples, or scalars
x = Tensor([1.0, -2.0, 3.0])       # list
w = Tensor(np.array([0.5, 0.5, 0.5]))
b = Tensor(0.1)                    # scalar

# Simple forward pass
z = x * w + b

# Activation
y = Activations.relu(z)

# Loss (MSE)
target = Tensor([0.0, 0.0, 0.0])
loss = Losses.mse(y, target)

# Backprop
loss.backward()

print("y:", y.data)
print("loss:", loss.data)
print("x.grad:", x.grad)

Features

  • Tensor with reverse-mode autodiff supporting:
    • Addition, subtraction, multiplication, true division, power
    • Matrix multiply (@), exp, log, sum, mean, abs
  • Broadcast-aware gradient unbroadcasting
  • Activations: relu, leaky_relu, sigmoid, safe_softmax
  • Losses: mse

API Overview

  • Tensor(data, dtype=np.float64, requires_grad=True)

    • Accepts: NumPy arrays, Python lists/tuples, Python/NumPy scalars
    • .data: NumPy array backing the tensor
    • .grad: accumulated gradient (NumPy array)
    • .backward(grad=None): run reverse-mode autodiff
    • Utility ops: .sum(...), .mean(...), .exp(), .log(), .abs()
  • Activations

    • relu(x), leaky_relu(x, alpha=0.01), sigmoid(x), safe_softmax(x)
  • Losses

    • mse(x, y) → returns a scalar Tensor (average squared error)

Examples Included

  • example/test1.py: basic elementwise multiply + backward for gradient check
  • example/test2.py: ReLU + MSE + backward
  • example/test3.py: constructing tensors from lists and scalars

Notes

  • Gradients accumulate in-place; call tensor.zero_grad() if you need to reset between steps.
  • For non-scalar outputs, pass an initial gradient to .backward(grad=...).

About

numpy based autograd engine

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages