g33sean:

So i want to sum over input x first before I sum over the difference between the pixels between x and y.


In your code snippet you would:

calculate the difference between x and y: (x-y)

square the difference: ()**2

apply the sum in dim=[1,2,3] so that your result should have the shape [N]

sum over the batch dimension.

You would get he same result, if you just apply the sum over all dimensions directly:
((x-y)**2).sum()

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.