Skip to main content

BoKeTE

Licence: MIT Python: 3.10+ PyTorch: 2.0+ Build System: Hatchling

A minimal PyTorch training and experimentation helper library for my personal use. Extracted from my computer vision final year project (2024/2025) at the University of Nottingham. Named after the Bad Bunny song BoKeTe, which refers to pothole in English. Designed as a modular, reusable helper package for general PyTorch deep learning workflows:

BoKeTE Music Video
"BoKeTe": Music video homage & namesake inspiration.

  • Training & Execution: Trainer loop with mixed-precision (AMP), auto cuDNN benchmarking, EarlyStopping, and model/optimizer checkpointing.
  • Experimentation & Reporting: Metric tracking, loss curve rendering (Matplotlib + Chart.js HTML), grid search parameter sweeps, and GFM Markdown report compilation (single-trial + multi-trial summaries).

[!NOTE] Personal Tooling & Disclaimer: bokete is an opinionated, personal PyTorch helper library created to streamline my own research workflows. While open-sourced under the MIT License, it is provided "as is" without warranty, guarantee of support, or promises of backward compatibility. If you choose to use it, you do so entirely at your own risk.

Table of Contents


1. Public API Summary

All core primitives are exported at the root package level (from bokete import ...):

Function / Class Module Description
set_seed(seed, deterministic=False) bokete.training Seeds Python, NumPy, and PyTorch (CPU/CUDA) for reproducibility.
determine_device() bokete.training Detects best available hardware device (cuda, mps, or cpu).
Trainer(...) bokete.training Main training loop wrapper with AMP mixed-precision, auto-cuDNN benchmarking & gradient clipping.
EarlyStopping(...) bokete.training Callback signaling early stop when validation loss stalls.
Checkpoint(...) bokete.training Callback saving best.pt and last.pt model weights + optimizer state.
training_report(metrics) bokete.metrics Computes final losses, mean losses, and best epoch summary dict.
plot_loss_curves(...) bokete.plotting Renders Matplotlib loss graph & interactive Chart.js HTML plot.
experiment_report(...) bokete.reporting Generates a structured GFM Markdown trial report string with dynamic overview metadata.
multi_trial_report(...) bokete.reporting Generates a combined GFM Markdown summary report string across multiple experiment trials.
run_experiments(...) bokete.experiments Executes grid search parameter sweeps across configuration paths.

(back to top)


2. Installation

bokete is packaged using the hatchling build backend and is fully compatible with uv workspaces.

Workspace / Local Installation (uv)

When used inside a project workspace, uv sync automatically installs bokete in editable mode:

uv sync

Or standalone via uv pip:

uv pip install -e ./bokete

Dependency Requirements

  • Python: >= 3.10
  • PyTorch: >= 2.0
  • NumPy, Matplotlib, tqdm

(back to top)


3. Core Modules & API Reference

3.1 Training & Checkpointing (bokete.training)

Provides the core training loop wrapper (Trainer), early stopping logic (EarlyStopping), model checkpointing (Checkpoint), and hardware device detection (determine_device).

from bokete import Trainer, EarlyStopping, Checkpoint, determine_device, set_seed

# Reproducibility & Hardware setup
set_seed(42)
device = determine_device()

# Setup callbacks
early_stop = EarlyStopping(patience=10, min_delta=1e-4)
checkpoint = Checkpoint(directory="checkpoints")

# Initialise and execute training loop
trainer = Trainer(model, criterion, optimizer, device=device, amp=True)
metrics = trainer.fit(
    train_loader, 
    val_loader, 
    epochs=50, 
    early_stopping=early_stop,
    checkpoint=checkpoint
)

3.2 Metric Tracking & Summaries (bokete.metrics)

Aggregates running loss metrics per epoch and generates structured summary statistics (final losses, mean losses, best epoch).

from bokete import TrainingMetrics, training_report

# Compute summary stats from training history dictionary
summary = training_report(eval_metrics)
# Returns: {'final_train_loss': 0.12, 'final_val_loss': 0.18, 'mean_train_loss': ..., 'best_epoch': 34}

3.3 Loss Curve Plotting (bokete.plotting)

Renders publication-ready training and validation loss curves using Matplotlib with annotated best-epoch markers, alongside a companion interactive Chart.js HTML file.

from bokete import plot_loss_curves

plot_loss_curves(
    train_loss=eval_metrics['train_loss'],
    val_loss=eval_metrics['val_loss'],
    path="results/graph.png",
    best_epoch=summary['best_epoch']
)

3.4 Markdown Experiment Reporting (bokete.reporting)

Generates structured, self-contained GitHub-Flavoured Markdown (.md) reports containing trial overviews, key hyperparameter highlights, loss graph embeds, custom evaluation metrics, and multi-trial statistical summaries ($\text{Mean} \pm \text{Std}$, $\text{Min}$, $\text{Max}$).

from bokete import experiment_report, multi_trial_report

# 1. Single Trial Report
md_report = experiment_report(
    config=config_dict,
    metrics_summary=summary,
    train_loss=eval_metrics['train_loss'],
    val_loss=eval_metrics['val_loss'],
    graph_filename="graph.png",
    title="Trial 1 Report [mixed_r0.1_s0.1]",
    extra_metrics={"Convergence Speed": 0.2145}
)

with open("result.md", "w", encoding="utf-8") as f:
    f.write(md_report)

# 2. Multi-Trial Combined Summary Report
summary_md = multi_trial_report(
    config=config_dict,
    all_trial_metrics=[metrics_trial_1, metrics_trial_2, metrics_trial_3]
)

with open("summary-report.md", "w", encoding="utf-8") as f:
    f.write(summary_md)

3.5 Hyperparameter Sweeps (bokete.experiments)

Orchestrates parameter sweeps over a grid of configuration paths without bleeding state across runs.

from bokete import run_experiments

param_grid = {
    "training.lr": [0.001, 0.0001],
    "training.batch_size": [8, 16]
}

results = run_experiments(
    base_config=config,
    param_grid=param_grid,
    run_fn=train_and_eval_callback
)

(back to top)


4. Usage Example

Here is a complete, minimal example combining bokete helpers inside a standard PyTorch pipeline:

import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from bokete import (
    determine_device, 
    set_seed, 
    Trainer, 
    training_report, 
    plot_loss_curves, 
    experiment_report
)

# 1. Setup Environment
set_seed(42)
device = determine_device()

# 2. Model, Loss, Optimizer
model = MyNeuralNetwork().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

# 3. Train using Trainer wrapper
trainer = Trainer(model, criterion, optimizer, device=device)
history = trainer.fit(train_loader, val_loader, epochs=20)

# 4. Generate Reports & Visualisations
summary = training_report(history.as_dict())
plot_loss_curves(
    train_loss=history.train_loss,
    val_loss=history.val_loss,
    path="graph.png",
    best_epoch=summary['best_epoch']
)

md = experiment_report(
    config={"lr": 1e-3, "epochs": 20},
    metrics_summary=summary,
    train_loss=history.train_loss,
    val_loss=history.val_loss,
    graph_filename="graph.png",
    title="Minimal Training Run"
)

with open("result.md", "w", encoding="utf-8") as f:
    f.write(md)

(back to top)


5. Project Architecture

bokete/
├── pyproject.toml         # Hatchling build system configuration
├── README.md              # Package documentation
└── src/
    └── bokete/
        ├── __init__.py    # Public API exports
        ├── experiments.py # Grid sweep orchestration
        ├── metrics.py     # Loss aggregation & summary statistics
        ├── plotting.py    # Matplotlib loss curve & Chart.js HTML rendering
        ├── reporting.py   # Single-trial & multi-trial Markdown report generation
        └── training.py    # Core Trainer, EarlyStopping, Checkpoint, auto-benchmarking

(back to top)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bokete-0.1.0.tar.gz (700.8 kB view details)

Uploaded Source

Built Distribution

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

bokete-0.1.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file bokete-0.1.0.tar.gz.

File metadata

  • Download URL: bokete-0.1.0.tar.gz
  • Upload date:
  • Size: 700.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bokete-0.1.0.tar.gz
Algorithm Hash digest
SHA256 20b08246463f6f1cb8ee579fa6fc18df3bbfbf3968479b1295636163392927c1
MD5 8181496383db9d8edecded40650bab2e
BLAKE2b-256 bed8a3a7e8399f4644e69ceb24e570b841de1e9d0529a0573e7cf5d36dd76435

See more details on using hashes here.

File details

Details for the file bokete-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bokete-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bokete-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 889ef63383317a1e11e2b194a1811f93399233b6316a64586beef06746f54d59
MD5 c9bece769da31acc6e27060c00295447
BLAKE2b-256 3753d4aaac23db60b209df52abd7610ae40bb484f6c604123f8457b72f6a25ad

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.1

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page