Complete worked example
python
from bootstrap_lab import torch_setup
torch = torch_setup()
from torch.utils.data import TensorDataset, DataLoader
def batches(values, batch_size):
if type(batch_size) is not int or batch_size < 1:
raise ValueError('positive batch size required')
dataset = TensorDataset(torch.tensor(values, dtype=torch.float32))
loader = DataLoader(dataset, batch_size=batch_size, shuffle=False,
num_workers=0, drop_last=False)
return [batch[0].tolist() for batch in loader]- Reasoning
- The helper wraps a one-dimensional value tensor in TensorDataset and uses a non-shuffled DataLoader. Each returned batch is selected from the loader’s tuple structure.