code: import torch 
  
  def calculate_topk_accuracy(y_pred, y, k = 4):
      with torch.no_grad():
          batch_size = y.shape[0]
          _, top_pred = y_pred.topk(k, 1)
          top_pred = top_pred.t()
          correct = top_pred.eq(y.view(1, -1).expand_as(top_pred))
          correct_1 = correct[:1].reshape(-1).float().sum(0, keepdim = True)
          correct_k = correct[:k].reshape(-1).float().sum(0, keepdim = True)
          acc_1 = correct_1 / batch_size
          acc_k = correct_k / batch_size
      return acc_1, acc_k
  
  
  def evaluate(model, iterator, criterion, device):
      
      epoch_loss = 0
      epoch_acc_1 = 0
      epoch_acc_5 = 0
      
      model.eval()
      
      with torch.no_grad():
          
          for (x, y) in iterator:
  
              x = x.to(device)
              y = y.to(device)
  
              y_pred, _ = model(x)
  
              loss = criterion(y_pred, y)
  
              acc_1, acc_5 = calculate_topk_accuracy(y_pred, y)
  
              epoch_loss += loss.item()
              epoch_acc_1 += acc_1.item()
              epoch_acc_5 += acc_5.item()
          
      epoch_loss /= len(iterator)
      epoch_acc_1 /= len(iterator)
      epoch_acc_5 /= len(iterator)
          
      return epoch_loss, epoch_acc_1, epoch_acc_5
  
  #Normalization 
  def normalize_image(image):
      image_min = image.min()
      image_max = image.max()
      image.clamp_(min = image_min, max = image_max)
      image.add_(-image_min).div_(image_max - image_min + 1e-5)
      return image    
  
apis: torch.no_grad