8000 Einsum double contraction in particular order causes ValueError · Issue #5147 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Einsum double contraction in particular order causes ValueError #5147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexalemi opened this issue Oct 3, 2014 · 8 comments
Closed

Einsum double contraction in particular order causes ValueError #5147

alexalemi opened this issue Oct 3, 2014 · 8 comments

Comments

@alexalemi
Copy link

If I try to do

a = np.random.randn(3,3,3,3)
ans1 = np.einsum('iijj',a)

I get a ValueError:

>>> ValueError: dimensions in operand 0 for collapsing index 'j' don't match (0 != 3)

However, these alternatives work:

ans2 = np.einsum('ii', np.einsum('ijkk', a))
ans3 = np.einsum('ijij', np.einsum('ijkl->ikjl',a))
ans2 == ans3
>>> True

And these agree with the naive version up to the last decimal place

ans4 = 0.0
for i in xrange(3):
    for j in xrange(3):
        ans4 += a[i,i,j,j]
np.allclose(ans2, ans4) and np.allclose(ans3, ans4)
>>> True
@jaimefrio
Copy link
Member

Thanks for reporting, It look like the error should be somewhere in this function, in case you want to try your hand at it...

@tovrstra
Copy link
Contributor
tovrstra commented Oct 4, 2014

Hi same problem here (version 1.8.2). This fails for me

x = np.random.normal(0, 1, (5, 5, 5, 5))
y1 = np.einsum('aabb->ab', x)
for i in xrange(5):
    for j in xrange(5):
        assert y1[i,j] == x[i,i,j,j]
y2 = np.zeros((5, 5))
np.einsum('aabb->ab', x, out=y2)
assert (y1 == y2).all()

This does not reach the final assertion. Instead, I get the following error on the penultimate line:

ValueError: dimensions in operand 0 for collapsing index 'b' don't match (0 != 5)

@tovrstra
Copy link
Contributor
tovrstra commented Oct 4, 2014

Could it be that the error is rather on line 2240 instead of 2143? In the latter, there is explicitly a test that checks new_dims[i] != 0, while the error message shows that it is zero. So the error can't be raised there, right?

@jaimefrio
Copy link
Member

Yes, it does look like all you have to do is add to the condition in line 2237 something like && new_dims[i] !=0, to mimic what's done in line 2140. A PR fixing this and adding a simple regression test, would be very welcome.

tovrstra added a commit to tovrstra/numpy that referenced this issue Oct 4, 2014
tovrstra added a commit to tovrstra/numpy that referenced this issue Oct 4, 2014
tovrstra added a commit to tovrstra/numpy that referenced this issue Oct 4, 2014
juliantaylor added a commit that referenced this issue Oct 4, 2014
juliantaylor added a commit that referenced this issue Oct 4, 2014
@mfdgroot
Copy link

Could there be a regression of this bug?

a=np.arange(4).reshape(1,1,2,1,2)
np.einsum('bbcdc->d,a)

returns
ValueError: dimensions in operand 0 for collapsing index 'd' don't match (1 != 2).

Summing b and c separately works as do the following examples:

a=np.arange(4).reshape(1,1,2,2,1)
np.einsum('bbccd->d,a)

a=np.arange(8).reshape(1,1,2,2,2)
np.eisum('bbcdc->d,a)

@charris
Copy link
Member
charris commented Mar 23, 2018

There is a test for this bug.

@mfdgroot
Copy link

@charris
I'm sorry, I thought my problem looked like another instance of this issue.
Do you mean that I should open a separate issue?

@charris
Copy link
Member
charris commented Mar 24, 2018

@mfdgroot That would be good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0