ART: Adaptive Resampling-based Training for Imbalanced Classification in PyTorch
Project description
art-resampling
art-resampling is a PyTorch utility for Adaptive Resampling-Based Training for Imbalanced Datasets (ART).
This repository contains the reference implementation used in the paper: Adaptive Resampling for Imbalanced Datasets (ART)
Install
pip install art-resampling
Quick start
Run the example script:
python examples/tabular_multiclass.py
Library API
ARTDataset
A PyTorch Dataset that can act as:
- a normal dataset (
enable_art=False), or - an ART-resampled dataset (
enable_art=True).
from art_resampling import ARTDataset
train_dataset = ARTDataset(
X_train,
y_train,
enable_art=True,
c=0.3,
cls_weights=initial_class_weights,
seed=5529,
)
val_dataset = ARTDataset(X_val, y_val, enable_art=False)
Refresh helpers
Refresh using validation predictions
from art_resampling import art_refresh_dataset_from_predictions
train_loader, class_weights, per_class_f1 = art_refresh_dataset_from_predictions(
art_dataset=train_dataset,
y_true=val_true,
y_pred=val_pred,
n_classes=num_classes,
train_loader_kwargs={"batch_size": 256, "shuffle": True, "drop_last": True},
)
Refresh using precomputed per-class F1
from art_resampling import art_refresh_dataset_from_f1_scores
train_loader, class_weights, per_class_f1 = art_refresh_dataset_from_f1_scores(
art_dataset=train_dataset,
f1=per_class_f1,
train_loader_kwargs={"batch_size": 256, "shuffle": True, "drop_last": True},
)
Weight and metric utilities
import numpy as np
from art_resampling import per_class_f1, art_weights_from_f1
val_true = np.array([0, 0, 0, 1, 1, 2, 2, 2])
val_pred = np.array([0, 0, 1, 1, 2, 0, 2, 2])
f1 = per_class_f1(val_true, val_pred, n_classes=3)
weights = art_weights_from_f1(f1)
print("per_class_f1:", np.round(f1, 4))
print("class_weights:", np.round(weights, 4))
print("sum:", float(weights.sum()))
Smoke test
pytest -q
Notes
- This library focuses on the ART resampling logic only.
- The example uses a synthetic tabular dataset for a complete end-to-end demonstration.
Minimal PyTorch training loop with ART
You choose two key ART parameters:
c: fraction of each refreshed training set drawn uniformly from all samples (the "balanced" portion).bf: refresh frequency in epochs (refresh everybfepochs).
A typical training loop looks like:
from torch.utils.data import DataLoader
from art_resampling import ARTDataset, art_refresh_dataset_from_predictions
c = 0.3
bf = 4
initial_class_weights = [1 / num_classes] * num_classes
train_dataset = ARTDataset(X_train, y_train, enable_art=True, c=c, cls_weights=initial_class_weights, seed=seed)
val_dataset = ARTDataset(X_val, y_val, enable_art=False)
train_loader_kwargs = {"batch_size": 256, "shuffle": True, "drop_last": True}
val_loader_kwargs = {"batch_size": 512, "shuffle": False, "drop_last": False}
train_loader = DataLoader(train_dataset, **train_loader_kwargs)
val_loader = DataLoader(val_dataset, **val_loader_kwargs)
for epoch in range(1, epochs + 1):
train_one_epoch(model, train_loader, optimizer, loss_fn)
if epoch % bf == 0:
val_true, val_pred = predict_labels(model, val_loader)
train_loader, class_weights, per_class_f1 = art_refresh_dataset_from_predictions(
art_dataset=train_dataset,
y_true=val_true,
y_pred=val_pred,
n_classes=num_classes,
train_loader_kwargs=train_loader_kwargs,
)
Support
If you find a bug, please open a GitHub Issue with:
- a minimal code snippet to reproduce
- your Python / PyTorch / numpy versions
- the exact error message or unexpected behavior
For questions about how to integrate ART into a specific training loop, include a small sketch of your dataloader and evaluation step.
Project details
Release history Release notifications | RSS feed
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 art_resampling-0.1.1.post1.tar.gz.
File metadata
- Download URL: art_resampling-0.1.1.post1.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6603244625b83a8486e4772d66b37e2a3f89b7e7a216f9ed4ca0555492be2523
|
|
| MD5 |
2da1da2f1bd7b465180b957ed76fcd55
|
|
| BLAKE2b-256 |
d2b1c63a2edeb9b3e8b166de448efdd5c9afda70cfdfcf5a7c128e819e43d51d
|
File details
Details for the file art_resampling-0.1.1.post1-py3-none-any.whl.
File metadata
- Download URL: art_resampling-0.1.1.post1-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e6d0500da76305b4f466dea326ec956c92cc47a593d09525b578adc6eb457aa
|
|
| MD5 |
4f8834911c0c0698ecd76f6dd1a1b862
|
|
| BLAKE2b-256 |
72fe8fdbfebdea4bcaac468d28d599aa2237156946b34f1ceafdf36bc23563d3
|