# A mathematical equation of a loss function in pytorch

**URL:** <https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690>\
**Category:** kornia\
**Created:** [May 14, 2022, 8:26pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690 "2022-05-14T20:26:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Najeh\_Nafti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/najeh_nafti/32/36465_2.png) [@Najeh\_Nafti](https://discuss.pytorch.org/u/Najeh_Nafti)\
**Post date:** [May 14, 2022, 8:26pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/1 "2022-05-14T20:26:36Z")

</div>

For an image, how can I calculate the below function in pytorch:  
 ![ttt](https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c3b87ae0e0e0f60fb6699f5d437113c51802376.png)

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [May 14, 2022, 9:39pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/2 "2022-05-14T21:39:31Z")

</div>

Can you explain what each variable means? It just looks like summing over a matrix and multiplying by a value P\_r

---

<div class="post-metadata">

**Author:** ![Najeh\_Nafti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/najeh_nafti/32/36465_2.png) [@Najeh\_Nafti](https://discuss.pytorch.org/u/Najeh_Nafti)\
**Post date:** [May 14, 2022, 9:58pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/3 "2022-05-14T21:58:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [May 14, 2022, 10:35pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/4 "2022-05-14T22:35:44Z")

</div>

So, `x` is of shape `[N,N]` or `[C,N,N]` where `N` is the width/height, and `C` is the number of channels?

If it’s just a matrix,

```auto
x = torch.randn(100,100) #100,100 image
answer = x.mean() #add all values then average

```

---

<div class="post-metadata">

**Author:** ![Najeh\_Nafti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/najeh_nafti/32/36465_2.png) [@Najeh\_Nafti](https://discuss.pytorch.org/u/Najeh_Nafti)\
**Post date:** [May 14, 2022, 11:01pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/5 "2022-05-14T23:01:37Z")

</div>

x is an image that can be x = torch.tensor([[[1,2],[2,3]],  
[[4,5],[6,7]],  
[[8,9],[10,11]]])

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

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [May 14, 2022, 11:30pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/6 "2022-05-14T23:30:35Z")

</div>

The equation above is a sum over `i` and `j` meaning to sum over **all** indices, not a subset.

So, try `x.mean(dim=(-2,-1))` which means over the last two dims of the Tensor.

---

<div class="post-metadata">

**Author:** ![Najeh\_Nafti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/najeh_nafti/32/36465_2.png) [@Najeh\_Nafti](https://discuss.pytorch.org/u/Najeh_Nafti)\
**Post date:** [May 15, 2022, 12:55am UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/7 "2022-05-15T00:55:53Z")

</div>

x.mean(dim=(-2,-1)), doesn’t work !

is the below equation correct, and represent the image equation ?  
x = torch.sum(x)/len(x)

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [May 15, 2022, 11:09am UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/8 "2022-05-15T11:09:03Z")

</div>

Can you share the error message? That should work for a Tensor of 2 dims or more.

Can you also share the shape of an example Tensor via `.shape` and not by printing it?

So, I’ve just read the equation again, and is the `E_x` meant to be a statistical expectation? Because it looks like it should be written like this,  
 ![image](https://discuss.pytorch.org/uploads/default/original/3X/b/b/bbd28ac45b955ef735954d2b88ea3aafff183723.png)

Is this how the equation should be written?

---

<div class="post-metadata">

**Author:** ![Najeh\_Nafti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/najeh_nafti/32/36465_2.png) [@Najeh\_Nafti](https://discuss.pytorch.org/u/Najeh_Nafti)\
**Post date:** [May 18, 2022, 5:05pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/9 "2022-05-18T17:05:47Z")

</div>

the error message is:

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 2)  
The shape of the image is: torch.Size([8, 1, 128, 128])

The equation should be like that:  
 ![ttt](https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c3b87ae0e0e0f60fb6699f5d437113c51802376.png)

E\_x : is the average critic score on real images (GAN model)

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [May 18, 2022, 8:39pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/10 "2022-05-18T20:39:16Z")

</div>

The reason why I re-typed the equation in LaTeX was to clarify the exact meaning as wrapping `x` 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.

If you have a reference for the equation please do share.

The PyTorch code below will compute the expectation of the sum of all pixels,

```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]) 

```

---

<div class="post-metadata">

**Author:** ![Najeh\_Nafti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/najeh_nafti/32/36465_2.png) [@Najeh\_Nafti](https://discuss.pytorch.org/u/Najeh_Nafti)\
**Post date:** [May 18, 2022, 10:38pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/11 "2022-05-18T22:38:27Z")

</div>

The reference of the equation of the loss function is taken from this paper: [https://openaccess.thecvf.com/content\_CVPR\_2020/html/Schonfeld\_A\_U-Net\_Based\_Discriminator\_for\_Generative\_Adversarial\_Networks\_CVPR\_2020\_paper.html](https://openaccess.thecvf.com/content_CVPR_2020/html/Schonfeld_A_U-Net_Based_Discriminator_for_Generative_Adversarial_Networks_CVPR_2020_paper.html)

The function is in sub-section 3.1, that represents the discriminator loss function and in my case is represented as below:  
 ![gggg](https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c961887491fc534ccc9ed86f501745a73b5c1e8.png)

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [May 19, 2022, 8:01pm UTC](https://discuss.pytorch.org/t/a-mathematical-equation-of-a-loss-function-in-pytorch/151690/12 "2022-05-19T20:01:08Z")

</div>

Assuming that `D(x')` and `D(x)` 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

```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]) 

```
