How to calculate integral of function

ericlormul: <p>I’m wondering how to implement integral function in pytorch. I’ll use an example from <a href="https://openaccess.thecvf.com/content_CVPR_2019/papers/Theagarajan_ShieldNets_Defending_Against_Adversarial_Attacks_Using_Probabilistic_Adversarial_Robustness_CVPR_2019_paper.pdf" rel="noopener nofollow ugc">the paper</a>, but it’s a general question.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/c/0cf0688cdd6f54c2693bdc7dcd782a94927f446a.png" data-download-href="https://discuss.pytorch.org/uploads/default/0cf0688cdd6f54c2693bdc7dcd782a94927f446a" title="Screen Shot 2021-03-30 at 10.22.06 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0cf0688cdd6f54c2693bdc7dcd782a94927f446a_2_689x173.png" alt="Screen Shot 2021-03-30 at 10.22.06 PM" data-base62-sha1="1QsNJUnbEpABwLXip1AoVZzKpnk" width="689" height="173" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0cf0688cdd6f54c2693bdc7dcd782a94927f446a_2_689x173.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0cf0688cdd6f54c2693bdc7dcd782a94927f446a_2_1033x259.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/c/0cf0688cdd6f54c2693bdc7dcd782a94927f446a.png 2x" data-dominant-color="F5F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2021-03-30 at 10.22.06 PM</span><span class="informations">1206×304 15.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Background info:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6bca4a5e4a851e9ef98d7835301e62a3e0d78963.png" data-download-href="https://discuss.pytorch.org/uploads/default/6bca4a5e4a851e9ef98d7835301e62a3e0d78963" title="Screen Shot 2021-03-31 at 12.04.18 AM"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6bca4a5e4a851e9ef98d7835301e62a3e0d78963.png" alt="Screen Shot 2021-03-31 at 12.04.18 AM" data-base62-sha1="fnyuFLXrBrQloiSOzLdgWXmnLN1" width="690" height="251" data-dominant-color="F0F0F0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2021-03-31 at 12.04.18 AM</span><span class="informations">746×272 18 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>The loss function requires to integrate over the whole perturbation space for every input image. Each perturbation delta is a vector.</p>
<p>My questions:</p>
<ol>
<li>
<p>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?</p>
</li>
<li>
<p>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?</p>
</li>
</ol>
<p>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!</p>

omarfoq: <p>Hi,</p>
<p>As you guessed, you can only approximate the integral as a sum, not that the integral you are dealing with is also an expectation, so all your formula can be seen as the expectation over X,Y and \delta. In my opinion, you can create a loader over your original dataset, and that yields in addition to X and Y the perturbed X’. Then you can do the rest of the training as usual.</p>
<p>One other thing that seems strange in you case, is that you model $\pi_{\omega}$ seems to be non-deterministic. However I think in practice I think this will be a neural network, so the second expectation on X’ can be ignored, and you can just  compute the loss using the output of   $\pi_{\omega}$.</p>
<p>To summarize, you need to:</p>
<ul>
<li>
<p>Each sample consists of X_{i}, y_{i} and a random $\delta$ generated on the fly, when reading the sample.</p>
</li>
<li>
<p>When you get a batch of X_{1:B}, y_{1:B} and \delta_{1:B}, you compute X’ = \pi_{\omega}(X+\delta).</p>
</li>
<li>
<p>You use X’ as your new input to the classification model you are training, i.e., the model labeled with $\theta$, and you compute the first part of the loss as for any classification problem, and you add  the regularization term.</p>
</li>
</ul>

ericlormul: <p>Thank you so much, <a class="mention" href="/u/omarfoq">@omarfoq</a>! It solves my question of implementation and I guess that’s how it is approximated in practice.  Got a couple follow-up of math notations in this kind of equations.</p>
<ol>
<li>“your formula can be seen as the expectation over X,Y and \delta” . I understand this as following: it’s an integral of a 3 random variables (X, Y, delta) function. Here the sense of expectation comes from integral operation. My question is what the two E notations(E_{(X,Y)}~D and E_X^{\prime}~pi_{\omega}) are literally used for in the equation. If the integral operation gives the meaning of expectation, what’s the meaning of having the first E in the beginning of the equation, if we omit the first E, what goes wrong? Similar question for the second E. If the second E is not ignored, does it mean there should be another integral over X^{\prime} on [L + Reg]? Thanks.</li>
</ol>

omarfoq: <p>Hello,</p>
<p>Expectation is indeed a Lebesgue integral. When we write $\mathbb{E}<em>{(X,Y) \sim D} f(X,Y)$ it can be understood as $\int</em>{X, Y \in \mathcal{X} \times \mathcal{Y}} f(x,y) p_{D}(x, y) dx dy$ where $\mathcal{X}$ ad $\mathcal{Y}$ are the input and output sets and $p_{D}(x,y)$ is the density of $(x, y)$. So the first expectation in your equation, means that you integrate over all possible samples weighted with a density function of the distribution $D$. Note that in practice, we don’t know this density function, so the expectation term is approximated by an empirical average, this is why we take the average over all elements in the dataset.</p>
<p>Some parts of what I said here an not 100% accurate, I need far more space and notation to write the exact definitions, but I hope my answer was clear.</p>

ericlormul: <p>Great explanation! Let me rephrase this approximation strategy:</p>
<ol>
<li>To approximate the first E, we iterate over  all (X, Y) in dataset</li>
<li>To approximate the integral while ignoring the second E, we give each (X, Y) a random delta on the fly. This means each example will be paired with multiple deltas during the whole training process with multiple epochs. Overall, it approximates the integration over the space of upper cased Delta for each (X, Y).</li>
</ol>
<p>Then, we do it in batches. At each batch, we use the average L_{imp} to back-propagate. I hope I didn’t misunderstand your explanation. Thanks.</p>

omarfoq: <p>Yes that’s exactly the reasoning I have <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>