# Mean for empty tensor

I found that when I x.mean() for an empty tensor x, I got nan. Is it intended or not?
I think it would confuse people.

Hi,

I think this behavior is intentional as it makes more sense than anything to me, at least.
This is the prefered implementation in numpy too.

Personally, I think mean of empty tensor can be defined in this way: `nothing/len(nothing)` or clearer version `[nan] / len([nan])` which only can produce two kind of output, 1. crash in runtime! 2. nan as the output
(I did not read the source code so I might be wrong about how it works exactly)

Also, if you look more intuitively, if we do not want to raise an exception, the best scenario is `nan` as there are many numerical operation that might produce `nan` as an unexpected bahvior.

Can you explain what is your idea and prefered bahvior? And why it would confuse people?

Bests

Well, I think `mean() of an empty tensor` should be` an empty tensor`.

How can we consider `empty` when we for sure know that `len(an empty array)=0` ?
Actually, this happens:
`torch.sum(a) / len(a)`, and obviously `len(a)=0`, so if `torch.sum(a)` becomes anything rather than `0`, then division by zero exception will occur.
Also, `torch.sum(torch.tensor([np.nan])) = tensor[nan]` so `[nan] / 0 = nan`.

Although there might be some other reasons due to stability which I do not know about.

The main question that I have too is that why the sum of empty array is zero? Although in the case of mean, it won’t make a difference as `nan/nan = nan = zero/nan `.
After googling, I found that in numpy, fortran intrinsics follow this behavior:

So as torch tries to implement similar behavior to numpy, same idea is happening here.

I hope someone can explain more accurately.