code: import numpy as np
  import torch
  
  def batch(x, y, batch_size=1, shuffle=True):
      assert len(x) == len(
          y), "Input and target data must contain same number of elements"
      if isinstance(x, np.ndarray):
          x = torch.from_numpy(x).float()
      if isinstance(y, np.ndarray):
          y = torch.from_numpy(y).float()
  
      n = len(x)
  
      if shuffle:
          rand_perm = torch.randperm(n)
          x = x[rand_perm]
          y = y[rand_perm]
  
      batches = []
      for i in range(n // batch_size):
          x_b = x[i * batch_size: (i + 1) * batch_size]
          y_b = y[i * batch_size: (i + 1) * batch_size]
  
          batches.append((x_b, y_b))
      return batches
  
  
apis: torch.from_numpy torch.randperm