Chapter 4 question

Hey fastai community,
I just get confused by the this line of code in chapter 4:

def mnist_distance(a,b): return (a-b).abs().mean((-1,-2))

Why do we need to call mean function with -1, -2 position?
I just tried the same function but without -1,-2 and it gives me same result.

what did I miss here?
thank you all.

The (-1,-2) are the dimensions in which the mean will be calculated. -1 is the last dimension and -2 is the second to last dimension. Specifying the (-1,-2) dimensions will give you the same answer as just calling .mean() if your array is 2 dimensional, but if it is more than 2 dimensions then you will get a different answer.

If you have an 2 dimensional picture/array of shape x by y and you call .mean() on it or call .mean((-1,-2)) on it it will give you the same result, but if you ‘batch’ up your ‘pictures/arrays’ into an array so your new array is (# of images in batch) by x by y, then calling .mean() would give you the mean of all of the pictures (single number returned) but calling .mean((-1,-2)) will give you the mean of each picture in the batch so the return value would be the size of your batch. It’s essentially allowing the function to work on both single ‘images’ and batches of ‘images’.

Hopefully this screenshot illustrates what is happening…

1 Like

Thank you Mat for spending your time on my question, I really appreciate that.