I’m wondering how to implement integral function in pytorch. I’ll use an example from the paper, but it’s a general question.

Screen Shot 2021-03-30 at 10.22.06 PM1206×304 15.1 KB

Background info:

Screen Shot 2021-03-31 at 12.04.18 AM746×272 18 KB

The loss function requires to integrate over the whole perturbation space for every input image. Each perturbation delta is a vector.
My questions:


Since the space of perturbation is continuous with infinite examples, it seems we can only sample finite number of perturbation to approximate the integral, is it correct?


If n perturbations are sampled for approximating the integral.  How do we implement and approximate the above loss function? changing the integral to summation over the n perturbations sampled? How to implement it as batch processing?


Overall, my question is how to approximate integral over a continuous multi-dimensional space and how to implement the approximation in an elegant vectorization way? Any ideas and suggestions are welcomed. Thanks to the community!