micrograd_hk
A tiny autograd engine built on top of NumPy for fast, matrix-driven backpropagation.
This package implements a minimal Value computation graph (inspired by micrograd), but instead of scalar-by-scalar autograd, everything is vectorized over full NumPy matrices — so training is significantly faster.
It ships with a small MLP / DenseLayer API and a Trainer class supporting several optimizers: SGD, Momentum, RMSProp, Adam, and a custom variant, Adam-M, developed and tuned as part of this project.
✨ Features
- Matrix-first
Valueautograd engine (@,+,-,**,sum,tanh) with full backward-pass support - Simple
DenseLayerandMLPbuilding blocks - Built-in
Trainerwith automatic checkpoint saving/loading - Five optimizers out of the box, including a custom modified Adam
- Optional
tqdmprogress bar during training
📦 Installation
pip install micrograd_hk
To also get the training progress bar:
pip install micrograd_hk[progress]
🚀 Quick Start
from micrograd_hk import Value, DenseLayer, MLP, Trainer
# Build a small MLP: 3 input features -> [8, 8, 1]
m = MLP(shape=[1, 3], nouts=[8, 8, 1])
# Wrap your data and model in a Trainer
loss_evaluator = Trainer(m, xs_matrix, ys_matrix)
# Train using matrix calculations
final_loss, y_pred = loss_evaluator.train(
lr=0.01,
iterations=3000,
optimizer="adam_m",
iter_stp=None,
save_checkpoint=True,
)
For lower-level control, MLP and DenseLayer can be used and composed directly.
🧠 Optimizers
Trainer supports the following, selected via the optimizer argument of .train():
| Optimizer | Key |
|---|---|
| SGD | "sgd" |
| Momentum | "momentum" |
| RMSProp | "rmsprop" |
| Adam | "adam" |
| Adam-M | "adam_m" |
Adam (standard)
$$ m_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t \qquad\quad v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2 $$
$$ \hat{m}_t = \frac{m_t}{1 - \beta_1^{t}} \qquad\quad \hat{v}_t = \frac{v_t}{1 - \beta_2^{t}} $$
$$ \theta_t = \theta_{t-1} - \frac{\eta , \hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon} $$
Adam-M (modified — this project)
Adam-M keeps the same moment estimates as Adam, but changes two things: the bias-correction denominators are shifted constants instead of powers of the raw betas, and the step counter t periodically resets every iter_stp steps.
$$ m_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t \qquad\quad v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2 $$
$$ \hat{m}_t = \frac{m_t}{1.5 - \beta_1^{t}} \qquad\quad \hat{v}_t = \frac{v_t}{2.5 - \beta_2^{t}} $$
$$ \theta_t = \theta_{t-1} - \frac{\eta , \hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon} $$
$$ \text{if } iter_stp \text{ is set and } t > iter_stp: \quad t \leftarrow 1 $$
Reference implementation:
def adam_m(self, iter_stp, beta1=0.9, beta2=0.999, eps=1e-8):
"""Adam-M Variant with periodic counter resets."""
self.t += 1
if iter_stp is not None and self.t > iter_stp:
self.t = 1 # Reset counter back to 1
for p in self.mlp.parameters():
p.momentum = p.momentum * beta1 + (1.0 - beta1) * p.grad
p.velocity = (beta2 * p.velocity) + (1.0 - beta2) * (p.grad ** 2)
momentum_corrected = p.momentum / (1.5 - beta1 ** self.t)
velocity_corrected = p.velocity / (2.5 - beta2 ** self.t)
p.data -= (self.lr * momentum_corrected) / (np.sqrt(velocity_corrected) + eps)
The periodic reset keeps the bias-correction terms from decaying all the way to 1 over long training runs, which in testing gave Adam-M steadier convergence on longer runs compared to standard Adam.
📊 Benchmarks
Adam vs. Adam-M, trained on the same model and data (test.py, using matplotlib):
Generated by the optimizer comparison script — both optimizers are trained on identical, deep-copied model initializations so the comparison is apples-to-apples.
🧩 Saving & Loading Models
Training checkpoints are saved automatically when save_checkpoint=True:
final_loss, y_pred = loss_evaluator.train(
lr=0.01,
iterations=3000,
optimizer="adam_m",
save_checkpoint=True,
save_filename="trained_mlp.pkl",
)
To reload a trained model into a fresh architecture:
from micrograd_hk import MLP, Trainer
blueprint = MLP(shape=[1, 3], nouts=[8, 8, 1])
model = Trainer.load_model("trained_mlp.pkl", blueprint)
📄 License
MIT © Hariom Lohat
Metadata
Release files for micrograd-hk 0.1.1
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 |
|---|---|---|---|---|
| micrograd_hk-0.1.1.tar.gz | 73.2 kB | Without provenance | Details for micrograd_hk-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| micrograd_hk-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for micrograd_hk-0.1.1-py3-none-any.whl |
Total release size: 84.8 kB
Release files / micrograd_hk-0.1.1.tar.gz
| Download URL | micrograd_hk-0.1.1.tar.gz |
|---|---|
| Size | 73.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
109cc604c10818b77ded45771ae1feef3c1ab2e80c78fd3ee357b1255abf67f4
|
|
BLAKE2b-256 checksum How to use checksums |
09d2e3e715519bca7776516b2f275afa3d32a74b21ccf7e348539c34fbcbff0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.14
|
Release files / micrograd_hk-0.1.1-py3-none-any.whl
| Download URL | micrograd_hk-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a4a229c57779c18fe6f8dfee41b80582af40f536e0a015af3b846443c1867b8b
|
|
BLAKE2b-256 checksum How to use checksums |
45c7a320471ed391e29bf0fa0975b2e9f876bcbfa71224c4dc9a5d0a1e4fbaef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.14
|
Security for micrograd-hk 0.1.1
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.