how do i make a python dataloader from scratch?

X = np.array([[1,2],[3,4],[5,6],[6,7]])

class DataLoader:
    def __init__(self, X, b_size):
        self.X = X
        self.b_size = b_size
    
    def __len__(self):
        return len(self.X)//self.b_size
    
    def __getitem__(self, index):        
        return self.X[index*self.b_size:index*self.b_size+self.b_size]

d = DataLoader(X, 2)
for i in range(len(d)):
  print (f"Iteration {i}: {d[i]}")

Output:

Iteration 0: [[1 2]
 [3 4]]
Iteration 1: [[5 6]
 [6 7]]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top