With the recent update to Numpy (1.14), I found that it breaks my entire codebase. This is based on changing the default numpy einsum optimize argument from False to True.
As a result, the following basic operation now fails:
a = np.random.random([50, 2, 2])
b = np.random.random([50, 2])
np.einsum('bdc, ac -> ab', a, b, optimize=True)
with the following error trace:
ValueError Traceback (most recent call last)
<ipython-input-71-b0f9ce3c71a3> in <module>()
----> 1 np.einsum('bdc, ac -> ab', a, b, optimize=True)
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\einsumfunc.py in
einsum(*operands, **kwargs)
1118
1119 # Contract!
-> 1120 new_view = tensordot(*tmp_operands, axes=
(tuple(left_pos), tuple(right_pos)))
1121
1122 # Build a new view if needed
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py in
tensordot(a, b, axes)
1301 oldb = [bs[axis] for axis in notin]
1302
-> 1303 at = a.transpose(newaxes_a).reshape(newshape_a)
1304 bt = b.transpose(newaxes_b).reshape(newshape_b)
1305 res = dot(at, bt)
ValueError: axes don't match array
The operation I'm requesting from einsum seems very simple... so why does it fail? If I set "optimize=False", it works just fine.
I tried exploring with einsum_path but the resulting path info was the same with and without optimization.
I don't see what optimization has to do with this error.
For the first argument
b,d,c
are 50,2,2. For the seconda,c
are 50,2. The result should be 50,50. But what happened tod
?Oops:
So it's summing on
d
.Note the error - with optimization, it uses
tensordot
(transpose plusdot
), rather than the originaleinsum
nditer
approach.c_einsum
is the one that can handle that missingd
:Tried some timings:
Two step calculation with the default optimize:
The desired
c_einsum
is fasterIn fact
c_einsum
is faster even when thetensordot
version worksThis example might be too small to show of the advantages of
tensordot/blas
.Looks like this has been raised on github - both the failures, and the slower 'optimize' speed: https://github.com/numpy/numpy/issues/10343 "einsum broadcast regression (with optimize=True)"