Skip to main content

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

pytorch_training_template-0.1.4.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytorch_training_template-0.1.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pytorch_training_template-0.1.4.tar.gz.

File metadata

File hashes

Hashes for pytorch_training_template-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5b745b50906ea6f74e4287a0d4cb3e92f97df990b0f7affffdbeb550c497c040
MD5 5df80c3d4392e8161bf9fb147107d6ec
BLAKE2b-256 35d2055b02dc6d3d8c6dfe03a712b985d69d4578a247340bcb5be02daa6f6543

See more details on using hashes here.

File details

Details for the file pytorch_training_template-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_training_template-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b794273f471dad5c66cae24e24c03239aa11b142dd58c42e8955fb11ee232130
MD5 6fc294c79548560ad2ce165b5983e6a4
BLAKE2b-256 de35d4fd9f3050d393d76047aea7fd2f48154433eb7de51d68cc431429c0960b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page