torch_brain.batching#
Utilities for collating samples of variable length into a batch.
The core entry point is collate(), a drop-in extension of PyTorch’s
default_collate that handles tensors of differing
lengths. Instead of configuring it directly, you wrap objects with a collation
recipe:
pad()(and its variants) to pad a dimension to the batch maximum.chain()to concatenate along the first dimension.Optionally,
track_mask()ortrack_batch()to emit a padding mask or per-element batch index.
When used with PyTorch’s default DataLoader,
collate() applies the wrapped recipes when batching. Below is
an example of how these are used in practice.
import torch
from torch_brain.datasets import Dataset
from torch_brain.batching import collate, chain, pad, track_mask
class MyDataset(Dataset):
...
def __getitem__(self, index):
# logic to gather data
...
return {
"spike_times": chain(spike_times),
"spike_seqlen": len(spike_times),
"query_times": pad(query_times),
"query_mask": track_mask(query_times),
}
ds = MyDataset(...)
loader = torch.utils.data.DataLoader(
dataset=ds,
sampler=...,
collate_fn=collate, # <- specify collate from torch_brain.batching
num_workers=4,
...
)
Collate Function#
Extension of PyTorch's |
Collation Recipes#
Wrap objects with these functions to collate them in different ways.
Wrap an object to specify that it (or any of its members) should be stacked along the first dimension when batching. |
|
Wrap an object to specify that it (or any of its members) should be padded to the maximum length in the batch. |
|
Wrap an object to specify that it (or any of its members) should be padded to the maximum length in the batch. |
|
Wrap an object to specify that it (or any of its members) should be padded to the maximum length in the batch along its first two dimensions. |
|
Wrap an object to specify that it (or any of its members) should be padded to the maximum length in the batch along its first two dimensions. |
Mask & Index Tracking#
Use these alongside a collation wrapper to track padding masks or batch indices for a particular array or tensor.
Wrap an array or tensor to track the batch_index. |
|
Wrap an array or tensor to specify that its padding mask should be tracked. |
|
Wrap an array or tensor to specify that its padding mask should be tracked. |
|
Wrap an array or tensor to specify that its padding mask should be tracked. |
|
Wrap an array or tensor to specify that its padding mask should be tracked. |