Also, another potential problem could be that you’re detaching the output of your model with
output1 = output.detach().numpy()

This will break the gradients within the model and probably explains why your model isn’t learning! The gradients are zero!
Also, remember to clear the gradient cache of your parameters (via optimizer.zero_grad()) otherwise your gradients will acculumate from all epochs! You’ll want to have something like this within your code!
for epoch in range(epochs):
  #lines to calculate loss
  optimizer.zero_grad() #clear current gradient values!
  loss = custom_loss_function(list_of_gaps) #calculate loss
  loss.backward() #calculate gradients
  optimizer.step() #update parameters