SynapGrad
A lightweight autograd engine with a deep learning library and a Torch-like API, built from scratch.
Installation
Python ≥ 3.9
pip install synapgrad
Technical Description
This project implements a completely functional engine for tracking operations between Tensors, by dynamically building a Directed Acyclic Graph (DAG), and an automatic gradient calculation and backpropagation algorithm (reverse-mode autodiff) over this DAG.
Built on top of the engine, the deep learning library implements the most common functions, layers, losses and optimizers in order to create AI models able to solve real problems. This library mimics PyTorch in a simplified way, with the same API style and function names.
Aim of the project
The objective of this project is to develop a lightweight deep learning library entirely from scratch, without relying on any existing frameworks (such as Keras, PyTorch, TensorFlow, scikit-learn, etc.), using only the numpy library.
At the moment, supporting GPU execution is not within the scope of the project
Autograd Engine
Automatic gradient calculation and backpropagation algorithm
Requirements
numpy>=1.23.0,<3.0.0 # Core
graphviz==0.20.1 # (Optional) Visualize DAG (https://graphviz.org/download/)
In the examples/visualize_graph.ipynb notebook there is an example of how to display the graph that synapgrad creates in the background as operations are chained:
import synapgrad
with synapgrad.retain_grads():
x1 = synapgrad.tensor([[-5.0, 3.0], [2.0, -4.0]], requires_grad=True)
x2 = synapgrad.tensor([[6.0, 0.4], [1.9, 2.0]], requires_grad=True)
x3 = synapgrad.tensor(3.0, requires_grad=True)
y = synapgrad.addmm(x3, x1, x2) # x3 + x1 @ x2
z = x2.sqrt() @ y
z.backward(synapgrad.ones(z.shape))
# graphviz is required to draw the graph
z.draw_graph()
Deep learning library
Built on top of the engine, synapgrad has a deep learning library that implements the following features:
Weight initialization: Xavier Glorot uniform, Xavier Glorot normal, He Kaiming uniform, He Kaiming normalActivations: ReLU, LeakyReLU, SELU, Tanh, Sigmoid, Softmax, LogSoftmaxLayers: Linear, Unfold, Fold, Flatten, DropoutConvolutions: MaxPool1d, MaxPool2d, AvgPool1d, AvgPool2d, Conv1d, Conv2dNormalizations: BatchNorm1d, BatchNorm2dOptimizers: SGD, Adam, AdamWLosses: MSELoss, NLLLoss, BCELoss, BCEWithLogitsLoss, CrossEntropyLoss
This project includes three Jupyter notebooks (located in examples/) that tackle three beginner-level AI problems:
- 1. Basic MLP for binary classification (sklearn 'make_moons' toy dataset)
- 2. MLP for handwritten digits classification (MNIST dataset)
- 3. CNN for handwritten digits classification (MNIST dataset)
Extra requirements
To use the SynapGrad training utilities, install the optional requirements by running:
pip install synapgrad[train]
Additionally to run the notebooks in /examples you will need to install:
torchvision>=0.13.1
# **** torch required only if you want to use torch engine instead of synapgrad's *****
torch>=1.12.1,<3.0.0 # Install following the instructions in https://pytorch.org/
Comparisons with PyTorch
To measure the efficiency of synapgrad, all three examples were compared to PyTorch 2.14. All training sessions were conducted on a Ryzen 7 9700X (8 cores). The times are those of trainer.fit, training plus the validation pass of every epoch.
| Notebook | torch | synapgrad | Model params | Dataset size | Batch size | Epochs |
|---|---|---|---|---|---|---|
| basic_mlp.ipynb | 1.4 s | 0.26 s | 337 | [150, 2] | 4 | 50 |
| mnist_mlp.ipynb | 4.5 s | 9.7 s | 178_110 | [48_000, 28, 28] | 64 | 10 |
| mnist_cnn.ipynb | 37.8 s | 4 min 55 s | 20_538 | [48_000, 1, 28, 28] | 128 | 5 |
Running tests
To run the unit tests you will have to install Pytest and PyTorch, which is used to check whether the gradients are calculated correctly
pytest>=8.0.0,<9.0.0
Run all the tests:
python -m pytest
To compare the speed of torch and synapgrad in each operation, run the command below:
python -m pytest ./tests/test_ops.py -s
Metadata
Release files for synapgrad 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| synapgrad-1.0.3.tar.gz | 50.9 kB | Without provenance | Details for synapgrad-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| synapgrad-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for synapgrad-1.0.3-py3-none-any.whl |
Total release size: 97.0 kB
Release files / synapgrad-1.0.3.tar.gz
| Download URL | synapgrad-1.0.3.tar.gz |
|---|---|
| Size | 50.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a7a88fec00e819aec9e64899f588bf50eeab475cc5ab2b0dac68a6ca7c291bf0
|
|
BLAKE2b-256 checksum How to use checksums |
64918bd0066319755bdf81dd3cf6e897cd78000457ffccf5b9c15e3c932274c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / synapgrad-1.0.3-py3-none-any.whl
| Download URL | synapgrad-1.0.3-py3-none-any.whl |
|---|---|
| Size | 46.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3bb85c42f09dfaf8891c5f46c3403232325d283fb91bb969b57b4fbe448e0a45
|
|
BLAKE2b-256 checksum How to use checksums |
c814e3a8db0f054d98737ad473f5116928abe4e24b91e104dc125633f099076b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for synapgrad 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.