You’ve missed the return statement within your loss function.
def custom_loss_function(gap):
  loss = torch.sum([1./g for g in gap])

Try your code again but with,
def custom_loss_function(gap):
  return torch.sum([1./g for g in gap])