Use a pyhton function as a loss function in pytorch

How can i use a python function (that uses numpy and scipy for computing results) as a loss function?
can i send output of my neural network and labels to a python function and then use result for backward?
there is a big computational complexity before python function that i cannot write cost function using pytorch tensors

You could define a new autograd.Function (tips here: ). This would involve implementing forward() and backward() calls for your loss function.

Autograd only works with functions that are performed on Variables. If you send the output of your network to scipy/numpy functions, then autograd won’t be able to record the operations, which is why you’ll need to define your own backward().