Solved by ptrblck in post #4 
              
              
                As @bharat0to said, your loss is most likely a multi-dimensional tensor, which will thus throw this error. 
You could add some reduction or pass a gradient with the same shape as loss.