A from-scratch neural network framework forged with NumPy.
TensorTrail is a compact educational deep learning framework built directly on NumPy. It includes a small reverse-mode autograd engine, neural network layers, losses, optimizers, data utilities, a trainer API, model serialization, gradient checking, graph export, and runnable examples.
It is not trying to replace PyTorch or TensorFlow. The point is to make the core machinery of a neural network framework visible, readable, and testable.
I built TensorTrail to understand deep learning infrastructure from the inside: how tensors remember computation history, how gradients flow backward through a dynamic graph, how layers expose parameters, and how training loops connect data, models, losses, optimizers, metrics, and checkpoints.
The project is intentionally small enough to inspect, but complete enough to train MLPs and tiny CNNs on offline synthetic datasets.
- NumPy-backed
Tensorobject - Reverse-mode automatic differentiation
- Dynamic computation graph with operation names
- Topological backward pass
- Broadcasting-aware gradients
- Gradient checker with centered finite differences
- Graphviz DOT computation graph export
- Layers:
Linear,Conv2D, pooling, normalization, dropout, activations Sequentialmodel composition- Losses: mean squared error, binary cross entropy, BCE with logits, multi-class cross entropy
- Optimizers: SGD, momentum SGD, Adam
- Global gradient clipping through optimizers and
Trainer - Epoch-level learning-rate schedules:
StepLRandExponentialLR - Offline datasets and mini-batch
DataLoader - Trainer with validation, metrics, early stopping, and checkpoint saving
- NumPy
.npzmodel save/load - Runnable examples and pytest coverage
pip install -e .For development tools:
pip install -e ".[dev]"For the optional plotting example:
pip install -e ".[plot]"If your shell exposes Python as python3, use:
python3 -m pip install -e .
python3 -m pip install -e ".[dev]"from tensortrail import Linear, ReLU, Sequential, Tensor
x = Tensor([[1.0, 2.0, 3.0]])
model = Sequential(
Linear(3, 8, seed=1),
ReLU(),
Linear(8, 2, seed=2),
)
logits = model(x)
print(logits)python examples/train_xor.pyThis trains a tiny MLP on the XOR truth table:
model = Sequential(
Linear(2, 8),
Tanh(),
Linear(8, 1),
Sigmoid(),
)For binary classification, use BinaryCrossEntropyLoss when a model already
outputs probabilities. Use BCEWithLogitsLoss when the model outputs raw
logits; it combines sigmoid and binary cross entropy in a numerically stable
loss.
python examples/train_binary_classifier.pypython examples/train_mlp_classifier.pyThis example generates an offline synthetic multi-class dataset and trains an MLP with BatchNorm and Dropout:
model = Sequential(
Linear(32, 24),
BatchNorm1D(24),
ReLU(),
Dropout(p=0.15),
Linear(24, 4),
)It uses DataLoader, Trainer(metrics=["accuracy"]), validation, early
stopping, and trainer.evaluate().
python examples/train_tiny_cnn.pyThe tiny CNN example generates offline 8x8 grayscale images with simple
class-specific patterns:
model = Sequential(
Conv2D(1, 4, kernel_size=3, padding=1),
ReLU(),
MaxPool2D(2),
Flatten(),
Linear(4 * 4 * 4, 3),
)Conv2D, MaxPool2D, and AveragePool2D are educational loop-based
implementations. They are correct and readable for small CPU examples, but not
optimized for production computer vision workloads.
TensorTrail records a dynamic computation graph as tensor operations run. Each
operation creates an output Tensor with references to its parents, an
operation name, and a small backward closure that knows the local derivative.
Calling backward() on a scalar output:
- Builds a topological ordering from the output back to all parents.
- Seeds the output gradient with
1. - Walks the graph in reverse topological order.
- Calls each node's backward closure.
- Accumulates gradient contributions into every tensor that requires them.
Broadcasting is handled by reducing gradients back to each operand's original shape, which is essential for bias terms and normalization parameters.
gradcheck validates smooth TensorTrail expressions by comparing analytical
backpropagation gradients against centered finite differences:
from tensortrail import Tensor, gradcheck
x = Tensor([1.0, -2.0, 3.0], requires_grad=True)
result = gradcheck(lambda value: (value * value).sum(), x)
print(result.passed)
print(result.message)
print(result.as_dict())For a deeper walkthrough, see docs/autograd_explained.md.
tensor.pyimplements theTensorobject and autograd engine.ops.pycontains softmax, log-softmax, one-hot encoding, and accuracy.modules.pydefines layers,Sequential, train/eval mode, parameters, and buffers.losses.pyimplements differentiable objective functions.optim.pyupdates trainable tensors with SGD or Adam, clips gradients by global norm, and provides simple learning-rate schedules.data.pyprovides offline datasets, splitting, and mini-batches.trainer.pyruns training, validation, metrics, gradient clipping, learning-rate schedules, early stopping, and checkpoints.gradcheck.pycompares analytical gradients with finite differences.serialization.pysaves and loads NumPy.npzmodel state.graph.pyexports computation graphs as Graphviz DOT.
More detail:
| Layer | Class | Notes |
|---|---|---|
| Fully connected | Linear(in, out) |
Xavier uniform init, optional bias |
| 2D convolution | Conv2D(in_channels, out_channels, kernel_size) |
NCHW input, stride, padding |
| Max pooling | MaxPool2D(kernel_size) |
Routes gradients to max positions |
| Average pooling | AveragePool2D(kernel_size) |
Distributes gradients evenly |
| Flatten | Flatten() |
(batch, ...) -> (batch, features) |
| Batch normalization | BatchNorm1D(features) |
Learnable scale/shift, running stats |
| Layer normalization | LayerNorm(features) |
Per-sample normalization over last dimension |
| Dropout | Dropout(p) |
Inverted dropout, inactive in eval mode |
| Activations | ReLU, Sigmoid, Tanh |
Differentiable activation modules |
| Container | Sequential(*layers) |
Chains modules in order |
- Tensor object
- Reverse-mode automatic differentiation
- Computational graph construction
- Topological backward pass
- Broadcasting-aware gradients
- Neural network
Modulesystem - Layers
- Loss functions
- Optimizers
- DataLoader
- Trainer API
- Gradient checker
- Model serialization
- Computation graph exporter
- Example training scripts
TensorTrail does not use PyTorch, TensorFlow, JAX, autograd, tinygrad, micrograd, scikit-learn models, or any existing ML/autograd framework. NumPy is the numerical backend.
pytestEquivalent command if pytest is not installed as a standalone executable:
python3 -m pytestThe tests cover tensor operations, autograd, broadcasting, modules, CNN layers, losses, optimizers, serialization, gradient checking, graph export, data utilities, metrics, and trainer behavior.
make install
make test
make xor
make examplesMain smoke checks:
python examples/train_xor.py
python examples/train_binary_classifier.py
python examples/train_mnist_like.py
python examples/train_mlp_classifier.py
python examples/train_regularized_mlp.py
python examples/train_tiny_cnn.py
python examples/visualize_autograd_graph.py
python examples/benchmark_ops.py
pytestOptional plotting example after pip install -e ".[plot]":
python examples/plot_training_history.pySee examples/README.md for a guide to each script.
- Add notebook walkthroughs for the autograd internals
- Add an im2col Conv2D implementation for faster educational comparison
TensorTrail is educational infrastructure, not production ML infrastructure. It does not target GPUs, distributed training, mixed precision, automatic batching, large datasets, deployment, or high performance.
The Conv2D and pooling layers use explicit Python/NumPy loops for clarity. That makes them easy to read and test, but much slower than optimized kernels in production frameworks.
- Built TensorTrail, a from-scratch neural network framework in Python and NumPy with reverse-mode automatic differentiation, dynamic computation graphs, and broadcasting-aware gradient accumulation.
- Implemented a topological backward pass, finite-difference gradient checker, and broadcasting-correct gradient reduction to validate autograd correctness across all operations.
- Developed a modular neural network API with
Tensor,Module,Linear,Conv2D, pooling,BatchNorm1D,LayerNorm,Dropout, activation layers,Sequential,MSELoss,BCEWithLogitsLoss,CrossEntropyLoss,SGD,Adam,DataLoader, andTrainer. - Added NumPy
.npzmodel serialization, Graphviz computation graph export, optional gradient clipping, learning-rate schedules, and aTrainerwith validation, early stopping, and checkpoint saving. - Validated the framework end-to-end with 137 pytest tests covering autograd, broadcasting, matmul, CNN layers, losses, optimizers, serialization, gradient checking, and trainer workflows.