You are detaching the loss from the computation graph by creating a new tensor:
loss = torch.from_numpy(loss)

If looks like you are passing PyTorch tensors to the function, so I think you don’t need this operation.
PS: You can add code snippets by wrapping them in three backticks ``` 
Also, Variables are deprecated, so you can now directly use tensors.