A modular PyTorch training template for deep learning tasks
Project description
PyTorch Training Template
A flexible and modular PyTorch training template for deep learning tasks, designed to streamline model training, evaluation, and visualization. This template supports mixed precision training, checkpointing, early stopping, and customizable metrics, making it suitable for a variety of machine learning projects.
Features
- Modular Data Handling: Move data (tensors, lists, or dictionaries) to GPU/CPU with a single move_to_device function.
- Checkpointing: Save and load model, optimizer, and scheduler states for resuming training.
- Mixed Precision Training: Support for Automatic Mixed Precision (AMP) to optimize training on GPUs.
- Flexible Training Loop: Customizable loss functions, metrics, and improvement criteria.
- Early Stopping: Stop training if the model stops improving after a specified number of epochs.
- Visualization: Plot training and validation loss/metric curves using Matplotlib.
- Example Included: A simple Convolutional Neural Network (CNN) trained on the MNIST dataset.
API Reference
Device Management
-
move_to_device(batch, device)
Move data to the specified device (CPU/GPU). -
get_autocast(use_amp, device)
Returns a context manager for mixed precision training. -
get_grad_scaler(use_amp)
Returns a gradient scaler for mixed precision training.
Checkpointing
-
save_checkpoint(model, optimizer, scheduler, epoch, best_score, path)
Save a model checkpoint. -
load_checkpoint(model, optimizer, scheduler, path, device)
Load a model checkpoint.
Training
-
train(model, optimizer, n_epochs, train_loader, val_loader, ...)
Train the model with customizable options. -
train_epoch(model, optimizer, loader, loss_fn, device, ...)
Train the model for one epoch. -
evaluate(model, loader, loss_fn, device, metric_fn)
Evaluate the model on a dataset.
Metrics (basic ones)
-
accuracy(predictions, target)
Compute classification accuracy. -
binary_accuracy(predictions, target)
Compute binary classification accuracy. -
mse(predictions, target)
Compute mean squared error.
Visualization
plot_reses(train_loss, train_metric, val_loss, val_metric, metric_name)
Plot training and validation curves.
Project details
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 pytorch_training_template-0.1.4.tar.gz.
File metadata
- Download URL: pytorch_training_template-0.1.4.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b745b50906ea6f74e4287a0d4cb3e92f97df990b0f7affffdbeb550c497c040
|
|
| MD5 |
5df80c3d4392e8161bf9fb147107d6ec
|
|
| BLAKE2b-256 |
35d2055b02dc6d3d8c6dfe03a712b985d69d4578a247340bcb5be02daa6f6543
|
File details
Details for the file pytorch_training_template-0.1.4-py3-none-any.whl.
File metadata
- Download URL: pytorch_training_template-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b794273f471dad5c66cae24e24c03239aa11b142dd58c42e8955fb11ee232130
|
|
| MD5 |
6fc294c79548560ad2ce165b5983e6a4
|
|
| BLAKE2b-256 |
de35d4fd9f3050d393d76047aea7fd2f48154433eb7de51d68cc431429c0960b
|