Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Invalid result from Reduction Iteration in given example #10410

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
oksidans opened this issue Jan 16, 2018 · 11 comments
Closed

Invalid result from Reduction Iteration in given example #10410

oksidans opened this issue Jan 16, 2018 · 11 comments

Comments

@oksidans
Copy link

oksidans commented Jan 16, 2018

The example for Reduction Iteration is producing the wrong result.

a = np.arange(24).reshape(2,3,4)
it = np.nditer([a, None], flags=['reduce_ok', 'external_loop'], op_flags=[['readonly'], ['readwrite', 'allocate']], op_axes=[None, [0,1,-1]])

it.operands[1][...] = 0
for x, y in it:
    y[...] += x

produces result:

>>> it.operands[1]
array([[ 3,  7, 11],
       [15, 19, 23]])

which is not accurate

>>> np.sum(a, axis=2)
array([[ 6, 22, 38],
       [54, 70, 86]])

I am using latest anaconda, Windows 7, 64bit, python 2.7, numpy 1.13.1, jupyter 1.0.0

@eric-wieser
Copy link
Member

eric-wieser commented Jan 16, 2018

Can you link to where the example comes from? (edit: updated top post with working link)

@oksidans
Copy link
Author

It is in the documentation, under the Iterating Over Arrays section - Reduction Iteration example for summation of all elements in an array
Here is the link https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.nditer.html

@eric-wieser
Copy link
Member

eric-wieser commented Jan 16, 2018

Looks like it fails for the same reason that the following does:

x = np.lib.stride_tricks.as_strided(0, shape=(4,), strides=(0,))
x += np.arange(len(x))
x
# array([3, 3, 3, 3]), expecting array([6, 6, 6, 6])

@eric-wieser
Copy link
Member

Strangely, using np.add(y, x, out=y) works correctly

@eric-wieser
Copy link
Member

eric-wieser commented Jan 16, 2018

Looks like a regression in 1.13 - 1.12 and 1.11 work correctly.

I suspect this is due to the overlap-detection added in 1.13 (#8043, @pv)

@eric-wieser
Copy link
Member

eric-wieser commented Jan 16, 2018

@pv
Copy link
Member

pv commented Jan 16, 2018 via email

@pv
Copy link
Member

pv commented Jan 16, 2018 via email

@eric-wieser
Copy link
Member

eric-wieser commented Jan 17, 2018

Thanks for chiming in

Looking at these lines from @pv's PR, it seems that this change was deliberate, and the out argument behavior is a lingering bug (I can no longer reproduce this), and the documentation is out of date

@seberg
Copy link
Member

seberg commented Jan 17, 2018

Yeah, I somewhat guess it is a change in behaviour that was in some sense anticipated. Is this actually creating problems for you? The nditer API, is a bit too close to the C-API for its own good/nice usage sometimes....

@seberg
Copy link
Member

seberg commented Jan 30, 2024

Closing, this was never a bug/regression, but a deliberate change in our API to protect the vast majority of users (of course some like me in my early days had code that did use this).

@seberg seberg closed this as completed Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants