Skip to main content

A tiny autograd and neural-network toolkit with NumPy/CuPy support.

Project description

fygrad

fygrad is a tiny autograd and neural-network toolkit built on NumPy, with optional GPU support through CuPy. It is intentionally small so you can read the code and understand how backprop works.

Install

pip install fygrad

To use the GPU, install CuPy that matches your CUDA version (example):

pip install cupy-cuda12x

What is inside

  • Data: a thin wrapper over NumPy/CuPy arrays that keeps device info.
  • Node: a value in the computation graph with a gradient and a backward function.
  • functional: pure functions for ops (add, matmul, relu, conv, loss, ...).
  • module: layers and building blocks (Linear, RNN, LSTM, Conv, ...).
  • optim: optimizers (SGD, Adam).
  • data: simple dataset and dataloader helpers.

Quick start (autograd)

from fygrad import Node, functional as F

x = Node("x", [[1.0, 2.0], [3.0, 4.0]])
y = F.sum(x * 2)
y.backward()
print(x.grad)

Core concepts

Data

  • Holds the raw array in data and a device string.
  • Automatically reshapes scalars and 1D arrays into 2D.
from fygrad.data import Data

a = Data([1, 2, 3])
print(a.shape)

Node

  • Wraps a Data value and stores gradients in grad.
  • Supports operators like +, -, *, /, @, **.
  • Call backward() on a final scalar to compute gradients.
from fygrad import Node

x = Node("x", [[1.0, 2.0]])
w = Node("w", [[3.0], [4.0]])
y = x @ w
loss = y.sum()
loss.backward()
print(w.grad)

functional

This module provides stateless functions. Use them when you want explicit ops.

Common ops:

  • add, sub, mul, div, pow, matmul
  • exp, log, sqrt, tanh, relu, sigmoid, softmax
  • sum, mean, abs, transpose, getitem, flatten
  • embedding, conv, max_pool2d, avg_pool2d
  • losses: mse, cross_entropy, binary_cross_entropy
from fygrad import Node, functional as F

x = Node("x", [[-1.0, 2.0, 0.5]])
y = F.relu(x)

module

Module is the base class for layers. It tracks parameters and submodules.

Built-in layers:

  • Linear, RNN, LSTM
  • Embedding, PositionalEncoding, LayerNorm
  • ScaledDotProductAttention
  • Conv, MaxPool2d, AvgPool2d
  • activations: Sigmoid, Tanh, ReLU, Softmax
from fygrad.module import Linear
from fygrad import Node

layer = Linear(2, 1)
x = Node("x", [[1.0, 2.0]])
y = layer(x)

optim

Two optimizers are included: SGD and Adam.

from fygrad.module import Linear
from fygrad.optim import SGD
from fygrad import Node

model = Linear(2, 1)
opt = SGD(model.parameters(), lr=0.1)

x = Node("x", [[1.0, 2.0]])
target = Node("t", [[1.0]])
pred = model(x)
loss = (pred - target).sum()
loss.backward()
opt.step()
opt.zero_grad()

data

ArrayDataset and DataLoader are minimal helpers to batch data.

from fygrad.data import ArrayDataset, DataLoader

xs = [[1.0], [2.0], [3.0], [4.0]]
ys = [[2.0], [4.0], [6.0], [8.0]]

dataset = ArrayDataset(xs, ys)
loader = DataLoader(dataset, batch_size=2, shuffle=True)

for xb, yb in loader:
	print(xb, yb)

A tiny training loop

from fygrad import Node, functional as F
from fygrad.module import Linear
from fygrad.optim import SGD
from fygrad.data import ArrayDataset, DataLoader

dataset = ArrayDataset([[1.0], [2.0], [3.0], [4.0]], [[2.0], [4.0], [6.0], [8.0]])
loader = DataLoader(dataset, batch_size=2, shuffle=True)

model = Linear(1, 1)
opt = SGD(model.parameters(), lr=0.1)

for _ in range(100):
	for xb, yb in loader:
		x = Node("x", xb)
		y = Node("y", yb)
		pred = model(x)
		loss = F.mse(pred, y)
		loss.backward()
		opt.step()
		opt.zero_grad()

GPU usage

Use device="gpu" when constructing Node or when calling module methods, then move to GPU with to_gpu().

from fygrad import Node

x = Node("x", [[1.0, 2.0]], device="gpu")
print(x.device)

If CuPy is not available, device="gpu" raises a runtime error.

Saving and loading

Module.save() writes a JSON state, and load() restores it.

from fygrad.module import Linear

model = Linear(2, 1)
model.save("model.json")

model2 = Linear(2, 1)
model2.load("model.json")

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fygrad-0.1.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fygrad-0.1.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file fygrad-0.1.1.tar.gz.

File metadata

  • Download URL: fygrad-0.1.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fygrad-0.1.1.tar.gz
Algorithm Hash digest
SHA256 92cb355d17d71309716846411ba493e83e177655b6053e8d2ba075363e98f491
MD5 8624c4d8dbdb272bf4825174845ec21f
BLAKE2b-256 a33d922c23aa3a2a6417463c25644cb4e3c9c84c0b99d7bce37dbeaaaedc91b3

See more details on using hashes here.

File details

Details for the file fygrad-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fygrad-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fygrad-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c29c7bf0681700107f6eec18f17023048349df4445cf0179644d9cdb4baf82de
MD5 3ae9970cb595af1762016c7c6cbd796a
BLAKE2b-256 a735e3cae78502352108327cc358502e29dbf612f003a39d37fd8c5a4ab3d8ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page