Equiareal batch sampler
Standard practice in Deep Learning is to train models on batches of data, keeping the number of samples in a batch ("batch size") constant. However, what you really need is a constant memory footprint of a batch, to i.e. coordinate it with the memory of your GPU. If your samples have different sizes (common in text, time series data), constant batch size will lead to a highly variable memory footprint. This package provides a batch sampler that keeps constant "batch area": sum of lengths of samples. Batch area mostly corresponds to its memory footprint, although padding will increase it slightly.
Installation
pip install equibatch
Usage
from equibatch import EquiarealBatchSampler
data = [
'London',
'Birmingham',
'Glasgow',
'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch',
'Liverpool',
'Bristol',
'Manchester'
]
batch_sampler = EquiarealBatchSampler(
sampler = range(len(data)), # the order in which the dataset is traversed
len_checker = lambda ix: len(data[ix]), # definition of "footprint of a sample"
max_size = 10, # maximum number of samples in a batch
max_footprint = 60 # maximum cumulative footprint of a batch
)
for batch in batch_sampler:
sample = [data[ix] for ix in batch]
print(sample)
This will print
['London', 'Birmingham', 'Glasgow']
['Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch']
['Liverpool', 'Bristol', 'Manchester']
Pytorch support
If you have Pytorch installed, EquiarealBatchSampler will be a subclass of torch.utils.data.sampler.BatchSampler and you can use it in your torch.utils.data.DataLoader like this:
from torch.utils.data import DataLoader
dataloader = DataLoader(
dataset = data,
batch_sampler = batch_sampler
)
for input in dataloader:
output = model(input)
loss = loss_fn(output)
loss.backward()
optimizer.step()
Alternatives
If you are using torchtext, similar results can be achieved using batch_size_fn parameter in torchtext.data.Iterator
Metadata
Release files for equibatch 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| equibatch-0.1.3.tar.gz | 2.1 kB | Without provenance | Details for equibatch-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| equibatch-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for equibatch-0.1.3-py3-none-any.whl |
Total release size: 4.6 kB
Release files / equibatch-0.1.3.tar.gz
| Download URL | equibatch-0.1.3.tar.gz |
|---|---|
| Size | 2.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3d76c6b5b26801df3a6aa5595463845baab20e9073bfe5a38c76b255760ff690
|
|
BLAKE2b-256 checksum How to use checksums |
e71287547b93bcb803f051b1c654a86a9f551b877cc04fc4b300472419b1fa26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.12.5 Darwin/23.6.0
|
Release files / equibatch-0.1.3-py3-none-any.whl
| Download URL | equibatch-0.1.3-py3-none-any.whl |
|---|---|
| Size | 2.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bfcebeee6e93e39f2d3a1371c31f986c8b00d7b1ed77c260cae30ddb983cc2d7
|
|
BLAKE2b-256 checksum How to use checksums |
bfda6139bc0cd1e17a17b362433b59fb255db4b3173e0e9a52c168c1a6429d9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.12.5 Darwin/23.6.0
|
Security for equibatch 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.