Hi Seyhmus!



 niceonede:

I am not able to calculate this loss function inside “torch” framework using for example “torch.quad”, because such code does not exist in Pytorch. I decided to define a custom loss function using “quad” but this returns a float number.


Pytorch’s autograd cannot compute gradients for computations that
are performed outside of the pytorch framework and you will not be
able to backpropagate through them – unless you give them some
help.
You will need to package your quad()  computation as a Function
and provide it with a backward() function that computes its gradient.
See Extending torch.autograd 5 for a start.


   def integrand(y,a,b,m,s,a2,b2,m2,s2): 

   return abs(((1/2)*b*math.exp(1)**((-1)*(s**(-1))**b*abs((-1)*m+y)**b)*s**(-1)*math.erfc((-1)*2**(-1/2)*a*s**(-1)*((-1)*m+y))*math.gamma(b**(-1))**(-1))-((1/2)*b2*math.exp(1)**((-1)*(s2**(-1))**b2*abs((-1)*m2+y)**b2)*s2**(-1)*math.erfc((-1)*2**(-1/2)*a2*s2**(-1)*((-1)*m2+y))*math.gamma(b2**(-1))**(-1)));



I haven’t looked carefully at integrand() and the underlying math,
but it would seem that one possibility could be the following:
You can switch the order of differentiation and integration (provided
things converge appropriately), so you could analytically differentiate
integrand(), code up that formula, and then integrate it numerically
(using your quad() or something similar) and use that as the main
ingredient in the backward() function of your custom autograd
Function.
Best.
K. Frank