Hey all, say you have some model F(input), how would you do a nested summation in Pytorch such that you would sum over the input such that it wold sum(sum(F(input)-target) where the first sum is summing over the inputs and second sum is summing over the difference between F(input)-target
Could you post a small example using random tensors, which shows the desired output, please?
Thank you for the response, say your model outputs x and your target values are y. So x and y will be
So i want to sum over input x first before I sum over the difference between the pixels between x and y. I want the output to be a scalar, i think it would be something like this
In your code snippet you would:
- calculate the difference between
- square the difference:
- apply the sum in
dim=[1,2,3]so that your result should have the shape
- sum over the batch dimension.
You would get he same result, if you just apply the sum over all dimensions directly:
If you want to apply the sum on
x before, I assume you would like to use broadcasting afterwards to calculate the difference?
If so, this code might work:
((x.sum([1, 2, 3], keepdim=True) - y)**2).sum()
but please double check the result, as I’m still unsure if I understand the use case correctly.