`keepdims` changes output value

Viewed 51
import numpy as np
np.random.seed(2)
x = np.random.randn(1000000).astype('float32')
print(float(np.linalg.norm(x, keepdims=1)**2))
print(float(np.linalg.norm(x, keepdims=0)**2))
998428.125
998428.1084311157

Reproduced in Colab. Also, Colab outputs different values than my CPU:

998425.0625
998425.059075091

Removing **2, they match. Also reproduced with sum, haven't tried other methods.

Why this behavior? I can understand device dependence but keepdims seems buggy.

1 Answers

This is because after keepdims=0, your NumPy turns into a single dim float32, which after **2, becomes a float64. The other still has two axes and for some reason NumPy does not do this.

>>> print(
>>>    np.linalg.norm(x, keepdims=1).dtype, 
>>>    np.linalg.norm(x, keepdims=0).dtype
>>> )
# Returns

float32 float32

>>> print(
>>>   (np.linalg.norm(x, keepdims=1)**2).dtype, 
>>>   (np.linalg.norm(x, keepdims=0) ** 2).dtype
>>> )
# Returns
float32 float64

The version of numpy I'm using is 1.20.3.

I could not find any documentation of why this happens in the NumPy documentation. I think opening a GitHub issue in NumPy's repository, might be a good idea.

Related