A PyTorch Batch Sampler that buckets by input length and cuts to min size in batch
Project description
This package provides 2 utilities:
cut2min_bucket.DatasetWrapperto eliminate padding and cut to min size in batchcut2min_bucket.BucketBatchSamplera batch sampler that buckets by input length.
In addition, we provide a Distributed Data Parallel version of the batch sampler: cut2min_bucket.DistributedBucketBatchSampler.
A detailed motivation for this package can be found on my blog.
Simple example:
import cut2min_bucket
import torch
import numpy as np
X = []
for _ in range(10000):
X.append(torch.tensor(np.random.randn(torch.randint(size=(), low=2, high=1000),)))
seqlens = torch.tensor([len(x) for x in X])
X = torch.nn.utils.rnn.pad_sequence(X, batch_first=True)
y = (torch.rand(10000)>0.5).int()
dataset = torch.utils.data.TensorDataset(X, y)
dataset = cut2min_bucket.DatasetWrapper(
dataset, seqlens,
index_or_key=0
)
batch_sampler = cut2min_bucket.BucketBatchSampler(
dataset,
seqlens,
batch_size=8,
n_partitions=5
)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_sampler=batch_sampler,
collate_fn=dataset.collate_fn,
)
next(iter(dataloader))
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
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 cut2min_bucket-0.1.2.tar.gz.
File metadata
- Download URL: cut2min_bucket-0.1.2.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b66f92d8e70c6877d219c90e8644793102560823a8c7b603f4f2658332d03248
|
|
| MD5 |
e1c6200fd932e69b34641abf807854cb
|
|
| BLAKE2b-256 |
7b3ba542a027502c7685fbcd6c89fd9d83149a4d3334d2b65d3fd2779a18ce05
|
File details
Details for the file cut2min_bucket-0.1.2-py3-none-any.whl.
File metadata
- Download URL: cut2min_bucket-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2cdf72fcd06feea42762859e7113cd9706e45a89d0fcd87d76214df6cc2fc6b
|
|
| MD5 |
0581580bf49247320a540daa5ef3e1b2
|
|
| BLAKE2b-256 |
d491c610b463c290bb5650d8684ffc9afc2de543016a883ac434def1ba3ec779
|