Skip to main content

Small utilities to simplify trainining of PyTorch models.

Project description

PyPI version License: MIT GitHub Repo stars PyPI - Python Version

Trainer Tools

A lightweight, hook-based training loop for PyTorch. trainer-tools abstracts away the boilerplate of training loops while remaining fully customizable via a powerful flexible hook system.

Features

  • Hook System: Customize every step of the training lifecycle (before/after batch, step, epoch, fit).
  • Built-in Integrations: Comes with hooks for wandb or trackio, Progress Bar, and Checkpointing.
  • Optimization: Easy Automatic Mixed Precision (AMP), Gradient Accumulation, and Gradient Clipping.
  • Metrics: robust metric tracking and logging to JSONL or external trackers.
  • Memory Profiling: Built-in tools to debug CUDA memory leaks.

Installation

pip install trainer-tools

Quick Start

Here is a minimal example of training a simple model:

import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
from trainer_tools.trainer import Trainer
from trainer_tools.hooks import MetricsHook, Accuracy, Loss, ProgressBarHook

# 1. Prepare Data
x = torch.randn(100, 10)
y = torch.randint(0, 2, (100,))
ds = TensorDataset(x, y)
dl = DataLoader(ds, batch_size=32)

# 2. Define Model
model = nn.Sequential(nn.Linear(10, 2))
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

# 3. Setup Hooks
metrics = MetricsHook(metrics=[Accuracy(), Loss()])
pbar = ProgressBarHook()

# 4. Train
trainer = Trainer(
    model=model,
    train_dl=dl,
    valid_dl=dl,
    optim=optimizer,
    loss_func=nn.CrossEntropyLoss(),
    epochs=5,
    hooks=[metrics, pbar],
    device="cuda" if torch.cuda.is_available() else "cpu"
)

trainer.fit()

The Hook System

trainer-tools relies on BaseHook. You can create custom behavior by subclassing it:

from trainer_tools.hooks import BaseHook

class MyCustomHook(BaseHook):
    def after_step(self, trainer):
        if trainer.step % 100 == 0:
            print(f"Current Loss: {trainer.loss}")

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

trainer_tools-0.2.0.tar.gz (114.5 kB view details)

Uploaded Source

Built Distribution

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

trainer_tools-0.2.0-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file trainer_tools-0.2.0.tar.gz.

File metadata

  • Download URL: trainer_tools-0.2.0.tar.gz
  • Upload date:
  • Size: 114.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trainer_tools-0.2.0.tar.gz
Algorithm Hash digest
SHA256 33d42b524e60b7541935702f25607093a34ed0ba9bc86255785e095bb10ccfe2
MD5 1712472643fa195b19992c9f7880e427
BLAKE2b-256 5c2ed086f977559183fb171b028c2c811de509fcfa9db6a906d020691a7fcd44

See more details on using hashes here.

Provenance

The following attestation bundles were made for trainer_tools-0.2.0.tar.gz:

Publisher: publish.yml on ssslakter/trainer-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trainer_tools-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: trainer_tools-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trainer_tools-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c747d3ffa56e9db33da3680590c292c8d41b85096159fa8d9c06a7acd6584e0f
MD5 fd4f13be770cb8787408a488677076c2
BLAKE2b-256 b4486ab27db4cfa01ed8bf200b2e8dfb83649d23f0fb4783144b5d1f67a36a1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for trainer_tools-0.2.0-py3-none-any.whl:

Publisher: publish.yml on ssslakter/trainer-tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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