Yet another simple & effective PyTorch trainer
Project description
Deep-Trainer
Lightweight training utilities for PyTorch projects.
deep-trainer provides a minimal yet flexible training loop abstraction
for PyTorch projects, including:
- Training & evaluation loops
- Automatic Mixed Precision (AMP) support
- Checkpointing (best / last / all)
- Metric handling system with aggregation
- TensorBoard logging (or custom loggers)
- Easy subclassing for custom training behavior
⚠️ Project Status
This project was originally developed as a personal baseline training framework.
- The codebase is functional but relatively old
- APIs may evolve in future versions
- Some refactoring and cleanup are planned
- Backward compatibility is not guaranteed for future major updates
If you use this project in production or research, please consider pinning a version.
Contributions and improvements are welcome.
🚀 Installation
Install with pip
pip install deep-trainer
Install from source
git clone https://github.com/raphaelreme/deep-trainer.git
cd deep-trainer
pip install .
🏁 Getting Started
Below is a minimal training example for a classification task.
import torch
from deep_trainer import PytorchTrainer
# ======================
# Dataset
# ======================
trainset = ...
valset = ...
testset = ...
train_loader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
val_loader = torch.utils.data.DataLoader(valset, batch_size=256)
test_loader = torch.utils.data.DataLoader(testset, batch_size=256)
# ======================
# Model
# ======================
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = ...
model.to(device)
# ======================
# Optimizer & Scheduler
# ======================
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
# Step every batch (scheduler is stepped per training step)
scheduler = torch.optim.lr_scheduler.StepLR(
optimizer,
step_size=len(train_loader) * 50, # decay every 50 epochs
gamma=0.1,
)
# ======================
# Loss
# ======================
criterion = torch.nn.CrossEntropyLoss()
# ======================
# Training
# ======================
trainer = PytorchTrainer(
model,
optimizer,
scheduler=scheduler,
save_mode="small", # keep best + last checkpoint
device=device,
use_amp=True, # optional mixed precision
)
trainer.train(
epochs=150,
train_loader=train_loader,
criterion=criterion,
val_loader=val_loader,
)
# ======================
# Testing (Best model)
# ======================
trainer.load("experiments/checkpoints/best.ckpt") # Reload best checkpoint
test_metrics = trainer.evaluate(test_loader)
print(test_metrics)
Features Overview
✔ Simple Trainer Abstraction
The PytorchTrainer handles:
- Forward / backward passes
- Optimizer and scheduler stepping
- Mixed precision scaling
- Metric tracking
- Validation & best checkpoint selection
- Logging
You probably will need to override the following method:
process_train_batchtrain_stepbackwardeval_step
to customize behavior (multi-loss, gradient accumulation, multiple optimizers, self-supervised learning, etc.).
✔ Flexible Metric System
The metric system supports:
- Per-batch metrics
- Aggregated metrics
- Validation metric selection
- Custom metrics via subclassing
✔ Logging
By default, logs are written to TensorBoard.
tensorboard --logdir experiments/logs/
You can also use:
DictLogger(in-memory logging)MultiLogger(combine multiple loggers)- Or implement your own logger by subclassing
TrainLogger
Example
An example training script is available in:
example/example.py
It demonstrates training a PreActResNet18 on CIFAR-10.
To use it:
# Show available hyperparameters
python example.py -h
# Launch training
python example.py
# Monitor training
tensorboard --logdir experiments/logs/
With default parameters, it reaches approximately 94--95% validation accuracy on CIFAR-10.
Design Philosophy
deep-trainer aims to be:
- Minimal (no heavy abstractions)
- Transparent (easy to read & debug)
- Hackable (easy to override core behavior)
- Suitable for research baselines
It is not intended to replace full-featured training frameworks like:
- PyTorch Lightning
- HuggingFace Trainer
- Accelerate
Instead, it provides a lightweight middle ground between raw PyTorch loops and larger ecosystems.
Contributing
Contributions are welcome!
If you'd like to:
- Improve documentation
- Refactor old components
- Add new metrics
- Improve testing
- Modernize APIs
Feel free to open an issue or submit a pull request.
📜 License
MIT License. See LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file deep_trainer-0.2.0.tar.gz.
File metadata
- Download URL: deep_trainer-0.2.0.tar.gz
- Upload date:
- Size: 19.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c80296559305bfd26ae062c5c7c0e988ea4e56e26dbe4bae3762c6720839c69
|
|
| MD5 |
e7d7dbaae670166cddc9e6e243ef1d64
|
|
| BLAKE2b-256 |
6e6e2ebef68a6b92579eb75db880c04aa19a4d6674ef59f7e6d6cb1742c22c6c
|
File details
Details for the file deep_trainer-0.2.0-py3-none-any.whl.
File metadata
- Download URL: deep_trainer-0.2.0-py3-none-any.whl
- Upload date:
- Size: 22.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b758532ebc801eced7ad5c749f8fa64425c2a11160460fcf25bed6792302b1de
|
|
| MD5 |
0fe308b13160ed982d05646849cf82d0
|
|
| BLAKE2b-256 |
2e6d8239e30caae72d48e68951fe1b61236b71a8ae65667763ee783b085fb9cf
|