A mathematical equation of a loss function in pytorch

Najeh_Nafti: <p>For an image, how can I calculate the below function in pytorch:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c3b87ae0e0e0f60fb6699f5d437113c51802376.png" alt="ttt" data-base62-sha1="d9Vt7AMfvZ7iO41ebUTDLy2U20e" width="218" height="82"></p>

AlphaBetaGamma96: <p>Can you explain what each variable means? It just looks like summing over a matrix and multiplying by a value P_r</p>

Najeh_Nafti: <p>x is a tensor (image), where I would like to calculate the sum of each pixel (i,j) on it and the calculate the average(mean) of the obtained sum.</p>

AlphaBetaGamma96: <p>So, <code>x</code> is of shape <code>[N,N]</code> or <code>[C,N,N]</code> where <code>N</code> is the width/height, and <code>C</code> is the number of channels?</p>
<p>If it’s just a matrix,</p>
<pre><code class="lang-auto">x = torch.randn(100,100) #100,100 image
answer = x.mean() #add all values then average
</code></pre>

Najeh_Nafti: <p>x is an image that can be x = torch.tensor([[[1,2],[2,3]],<br>
[[4,5],[6,7]],<br>
[[8,9],[10,11]]])</p>
<p>So how about the sum of different pixels (i,j)? because I tried torch.sum(x,(2,1)).mean() but it doesn’t work in my code</p>

AlphaBetaGamma96: <p>The equation above is a sum over <code>i</code> and <code>j</code> meaning to sum over <strong>all</strong> indices, not a subset.</p>
<p>So, try <code>x.mean(dim=(-2,-1))</code> which means over the last two dims of the Tensor.</p>

Najeh_Nafti: <p>x.mean(dim=(-2,-1)), doesn’t work !</p>
<p>is the below equation correct, and represent the image equation ?<br>
x = torch.sum(x)/len(x)</p>

AlphaBetaGamma96: <p>Can you share the error message? That should work for a Tensor of 2 dims or more.</p>
<p>Can you also share the shape of an example Tensor via <code>.shape</code> and not by printing it?</p>
<p>So, I’ve just read the equation again, and is the <code>E_x</code> meant to be a statistical expectation? Because it looks like it should be written like this,<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/b/b/bbd28ac45b955ef735954d2b88ea3aafff183723.png" alt="image" data-base62-sha1="qNyn16iz8YVF8UW89tsChZNfFhF" width="563" height="359"></p>
<p>Is this how the equation should be written?</p>

Najeh_Nafti: <p>the error message is:</p>
<p>IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 2)<br>
The shape of the image is: torch.Size([8, 1, 128, 128])</p>
<p>The equation should be like that:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c3b87ae0e0e0f60fb6699f5d437113c51802376.png" alt="ttt" data-base62-sha1="d9Vt7AMfvZ7iO41ebUTDLy2U20e" width="218" height="82"></p>
<p>E_x : is the average critic score on real images (GAN model)</p>

AlphaBetaGamma96: <p>The reason why I re-typed the equation in LaTeX was to clarify the exact meaning as wrapping <code>x</code> in []'s makes no sense (to me at least), but it makes complete sense if the []'s wrap the entire sum and you can then state the equation is the expectation of the sum of all pixels.</p>
<p>If you have a reference for the equation please do share.</p>
<p>The PyTorch code below will compute the expectation of the sum of all pixels,</p>
<pre><code class="lang-auto">x = torch.randn(8,1,128,128) #fake data
sum_over_image = x.sum(dim=(-2,-1)) #summing over i and j

#then take the expectation over all datas in batch (returns scalar as there's only 1 channel).
expectation = torch.mean(sum_over_image, dim=0) #returns tensor([13.3208]) 
</code></pre>

Najeh_Nafti: <p>The reference of the equation of the loss function is taken from this paper: <a href="https://openaccess.thecvf.com/content_CVPR_2020/html/Schonfeld_A_U-Net_Based_Discriminator_for_Generative_Adversarial_Networks_CVPR_2020_paper.html" rel="noopener nofollow ugc">https://openaccess.thecvf.com/content_CVPR_2020/html/Schonfeld_A_U-Net_Based_Discriminator_for_Generative_Adversarial_Networks_CVPR_2020_paper.html</a></p>
<p>The function is in sub-section 3.1, that represents the discriminator loss function and in my case is represented as below:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c961887491fc534ccc9ed86f501745a73b5c1e8.png" alt="gggg" data-base62-sha1="dd3vnn745mZPD6uCxSTC9L0nIiQ" width="555" height="88"></p>

AlphaBetaGamma96: <p>Assuming that <code>D(x')</code> and <code>D(x)</code> are the same shape as the example above then what I wrote above should be correct. I’m not an expert on GANs but it looks like it’s as simple as just summing over the pixels and then taking its expectation over the data</p>
<pre><code class="lang-auto">x = torch.randn(8,1,128,128) #fake data
sum_over_image = x.sum(dim=(-2,-1)) #summing over i and j

#then take the expectation over all datas in batch (returns scalar as there's only 1 channel).
expectation = torch.mean(sum_over_image, dim=0) #returns tensor([13.3208]) 
</code></pre>