Anyway to get mean and std together just like tf.nn.moments()?

It’s known that torch.mean() and torch.std() can return mean and standard deviation respectively, and also the computation of standard deviation depends on its mean value.

In some large scale tensor, the cost of calculating mean and std is high, so my question is, if I want both mean and std, is there anyway to get them together avoiding repeatedly calculate mean value again just like tf.nn.moments()?

Thanks

1 Like

You could write your own using tensor primitives:
https://www.mathsisfun.com/data/standard-deviation-formulas.html

Or perhaps delve into the code and make a modified version that returns std and mean in a single pass?

1 Like

It’s an old topic, but it was my first Google search result when searching for “torch moments”. There is such a function now:

torch.std_mean returns the tuple (std, mean) containing the standard deviation and mean.

Similarly, there is also torch.var_mean.