I just had a bug which was based on np.sum
and an equivalent (or at least I thought so...) np.einsum
command not giving the same result. Here is an example:
import numpy.random
array = np.random.randint(-10000, 10000, size=(4, 100, 200, 600), dtype=np.int16)
sum1 = np.sum(array, axis=(0,1,2))
sum2 = np.einsum('aijt->t', array)
print(np.allclose(sum1, sum2))
plt.figure()
plt.plot(sum1)
plt.plot(sum2)
plt.show()
After some searching, this is due to overflow of the integer data type.
My question:
- Why is
np.einsum
not giving the same result asnp.sum
here? I feel the np.sum behaviour is a lot more desirable leading to less errors. - Why does
np.einsum
not throw an overflow error or at least a warning? This is super scary in terms of getting hidden bugs when using it. Should I be checking for those by hand every time I use the command? - Would this considered be a bug in numpy?