You are detaching output from the computation graph by assigning it to the max. index here:
 _, output = torch.max(pred, 1)

Could you explain your use case a bit?
Would it be possible  to reformulate your loss function, e.g. to return a single output value in your model and pass it the MSELoss?