PyNorch/norch/utils/data/dataloader.py

29 lines
830 B
Python
Raw Permalink Normal View History

2024-05-18 17:59:14 -03:00
import numpy as np
from .batch import Batch
class DataLoader:
2024-05-25 09:48:32 -03:00
2024-05-27 15:08:27 -03:00
def __init__(self, dataset, batch_size, sampler=None):
2024-05-18 17:59:14 -03:00
self.dataset = dataset
self.batch_size = batch_size
2024-05-25 09:48:32 -03:00
self.sampler = sampler
2024-05-18 17:59:14 -03:00
def __iter__(self):
2024-05-25 09:48:32 -03:00
if self.sampler is not None:
2024-05-27 15:08:27 -03:00
indices = list(iter(self.sampler))
2024-05-25 09:48:32 -03:00
else:
2024-05-27 15:08:27 -03:00
indices = list(range(len(self.dataset)))
for i in range(0, len(indices), self.batch_size):
batch_indices = indices[i:i + self.batch_size]
batch_data = self.dataset[batch_indices]
yield Batch(batch_data, len(batch_data))
2024-05-18 17:59:14 -03:00
def __len__(self):
2024-05-25 09:48:32 -03:00
if self.sampler is not None:
return len(self.sampler) // self.batch_size
2024-05-25 10:53:09 -03:00
2024-05-18 17:59:14 -03:00
return len(self.dataset) // self.batch_size