Skip to main content

TinyMLP

TinyMLP is a lightweight, pure Python scalar automatic differentiation (autograd) engine and neural network library built for education, experimentation, and deep learning fundamentals.


Key Features

  • Scalar Autograd Engine: Tracks computational graphs dynamically and computes gradients using reverse-mode automatic differentiation.
  • Neural Network Components: Clean Neuron, Layer, and MLP abstractions similar to PyTorch.
  • Activation Functions: ReLU, Sigmoid, and Tanh.
  • Loss Functions: MSE (Mean Squared Error) and CrossEntropy (Binary Cross Entropy).
  • Optimizer: SGD (Stochastic Gradient Descent).
  • Zero External Dependencies: Standard library Python with optional pytest for testing.

Directory Structure

TinyMLP/
│
├── README.md
├── LICENSE
├── pyproject.toml
├── .gitignore
│
├── src/
│   └── tinymlp/
│       ├── __init__.py
│       ├── core/
│       │   ├── __init__.py
│       │   ├── value.py
│       │   └── engine.py
│       ├── nn/
│       │   ├── __init__.py
│       │   ├── neuron.py
│       │   ├── layer.py
│       │   └── mlp.py
│       ├── activations/
│       │   ├── __init__.py
│       │   ├── relu.py
│       │   ├── sigmoid.py
│       │   └── tanh.py
│       ├── losses/
│       │   ├── __init__.py
│       │   ├── mse.py
│       │   └── cross_entropy.py
│       └── optim/
│           ├── __init__.py
│           └── sgd.py
│
├── tests/
│   ├── __init__.py
│   ├── test_value.py
│   ├── test_neuron.py
│   ├── test_layer.py
│   ├── test_mlp.py
│   ├── test_activations.py
│   ├── test_losses.py
│   └── test_optimizer.py
│
├── examples/
│   ├── basic_mlp.py
│   ├── xor.py
│   └── regression.py
│
└── docs/
    └── README.md

Quickstart

1. Installation

Install in editable mode:

pip install -e .

2. Autograd Example

from tinymlp import Value

a = Value(2.0, label='a')
b = Value(-3.0, label='b')
c = Value(10.0, label='c')

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

L.backward()

print(f"L.data: {L.data}") # -8.0
print(f"a.grad: {a.grad}") # 6.0
print(f"b.grad: {b.grad}") # -4.0

3. Training an MLP on XOR

from tinymlp import MLP, SGD, mse_loss


# 2 inputs -> hidden layer of 4 -> 1 output
model = MLP(2, [4, 1], activations=['relu', 'sigmoid'])
optimizer = SGD(model.parameters(), lr=0.5)

X = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
y = [0.0, 1.0, 1.0, 0.0]

for epoch in range(100):
    y_pred = [model(x) for x in X]
    loss = mse_loss(y_pred, y)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if epoch % 20 == 0:
        print(f"Epoch {epoch} | Loss: {loss.data:.4f}")

Running Tests

Run all unit tests with pytest:

pytest tests/

License

MIT License

Contributor's: Hasher Amin
Muhammad Aadil C.(Founder & Solo Builder @genvimart)

Metadata

Release files for tinymlp 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tinymlp 0.1.0
File Size Uploaded
tinymlp-0.1.0.tar.gz 12.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tinymlp 0.1.0
File Interpreter ABI Platform
tinymlp-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / tinymlp-0.1.0.tar.gz

Download URL tinymlp-0.1.0.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
297125fdc114a88fb52d9ca1ad7741a554a4da18f28eb538a104ad2f694c0b87
BLAKE2b-256 checksum
How to use checksums
8d044a2469ccf59fdef49b9e64ec580a5e0ec6a16b15d387aa3dcc493cce63f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / tinymlp-0.1.0-py3-none-any.whl

Download URL tinymlp-0.1.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8303ffa493c41a969a581e15065baa52223b482c1aa7d1d02c3acf2cd16440ab
BLAKE2b-256 checksum
How to use checksums
37a35ee08202a53509244cbfd8807691e50b6c8e1675f07d9a03683f9689362b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page