Skip to main content

Machine learning bread and butter - a collection of basic deep learning tools

Project description

mlbnb

Machine learning bread and butter - a collection of tools for PyTorch-based machine learning experiments.

This library provides utilities to streamline common tasks such as experiment management, checkpointing, and data handling.

To jumpstart your ML experiment codebase, check out the associated scaffolding-v3 repo, which uses mlbnb.

Installation

This project uses uv for dependency management.

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Install dependencies
uv sync

Core Components

ExperimentPath

The ExperimentPath class helps manage the directory structure for your experiments. It can generate unique run names and save your experiment configuration.

Example:

from pathlib import Path
from omegaconf import OmegaConf
from mlbnb.paths import ExperimentPath

# Define your experiment configuration
cfg = OmegaConf.create({
    "learning_rate": 1e-3,
    "model": {
        "name": "resnet18",
        "pretrained": True
    },
    "dataset": "cifar10"
})

# Create a new experiment path
# This will create a directory like: /tmp/experiments/2025-06-26_11-02_witty_zebra
exp_path = ExperimentPath.from_config(cfg, root=Path("/tmp/experiments"))

print(f"Experiment directory: {exp_path}")

# The configuration is saved automatically
saved_cfg = exp_path.get_config()
assert saved_cfg == cfg

# You can also create paths for files within the experiment directory
model_dir = exp_path / "models"
model_dir.mkdir()
print(f"Model directory: {model_dir}")

CheckpointManager

The CheckpointManager simplifies saving and loading the state of your training loop, including the model, optimizer, and random number generators. It integrates with ExperimentPath to store checkpoints within your experiment's directory.

Example:

import torch
from torch import nn, optim
from mlbnb.checkpoint import CheckpointManager
from mlbnb.paths import ExperimentPath

# Assume exp_path is an existing ExperimentPath instance
exp_path = ExperimentPath(root="/tmp/experiments", name="my-first-experiment")

# Initialize components
model = nn.Linear(10, 2)
optimizer = optim.Adam(model.parameters())
generator = torch.Generator()

# Create a CheckpointManager
checkpoint_manager = CheckpointManager(exp_path)

# Save a checkpoint
checkpoint_manager.save_checkpoint(
    name="epoch_1",
    model=model,
    optimiser=optimizer,
    generator=generator
)
print(f"Saved checkpoints: {checkpoint_manager.list_checkpoints()}")

# Later, you can restore the state
new_model = nn.Linear(10, 2)
new_optimizer = optim.Adam(new_model.parameters())
new_generator = torch.Generator()

checkpoint_manager.reproduce(
    name="epoch_1",
    model=new_model,
    optimiser=new_optimizer,
    generator=new_generator
)

print("Restored state from checkpoint.")

Other Utilities

mlbnb also includes several other helpful modules:

  • WandbLogger & WandbProfiler: For logging metrics and profiling code with Weights & Biases.
  • StepIterator: An iterator that runs for a fixed number of steps, to turn training from "num epochs" to "num steps" cleanly.
  • LabelledArray: A NumPy array wrapper that allows indexing by named coordinates, similar to xarray, supporting memmaps.
  • checksum: A utility to compute a checksum for objects containing tensors to verify reproducibility.
  • EarlyStopper: Stop training when a metric stops improving.
  • CachedDataset: A PyTorch Dataset wrapper that caches items in memory.

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

mlbnb-0.1.11.tar.gz (166.8 kB view details)

Uploaded Source

Built Distribution

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

mlbnb-0.1.11-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file mlbnb-0.1.11.tar.gz.

File metadata

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

File hashes

Hashes for mlbnb-0.1.11.tar.gz
Algorithm Hash digest
SHA256 f1582bcd14524ee482d97c0dc1070bd6ef6921fe658d8077e275e422f6a75d0c
MD5 7801f052e1dba27d9b71906398628d06
BLAKE2b-256 3322a4422bf5612c226167acedf772962ca7eacb94ede8f4ee4f9ea33c6080c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlbnb-0.1.11.tar.gz:

Publisher: publish.yaml on jonas-scholz123/mlbnb

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

File details

Details for the file mlbnb-0.1.11-py3-none-any.whl.

File metadata

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

File hashes

Hashes for mlbnb-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 1120a1624f349ef50d7980c0949bfdef330c3dc69b88fb0b85eb607bc5e3ee2d
MD5 daf6b0a0898e9b521bfaeb812e9b7dfc
BLAKE2b-256 4d3cb360f959c73a4dc1e961ff2c48d05552bdbc63a4b9f518cf204bb7c51b2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlbnb-0.1.11-py3-none-any.whl:

Publisher: publish.yaml on jonas-scholz123/mlbnb

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