question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A protocol for numpy.ones_like

See original GitHub issue

I recently sat down with @ericmjl to see if we could make autograd work with cupy by making them both understand protocols like __array_ufunc__

The goal here being to improve all projects to produce and consume numpy protocols rather than explicitly import each other in pair-wise plugin mechanisms.

One issue that we ran into is that autograd needs to produce new arrays, like ones (the gradient of sum) and random (not sure why yet). It would be nice to be able to say “produce an array of ones with a particular shape and dtype, but using the module that created this particular array object”. This would allow autograd to produce dask arrays of ones, cupy arrays of ones, etc…

The numpy.ones_like function almost does this except for the following two issues:

  1. It takes the shape from the given array (which makes perfect sense given its original objective)
  2. There is no protocol for it, so np.ones_like(my_duck_array, ...) produces a numpy array

Also, to be clear, ones here is an example of a larger problem of how to improve dispatch for a wider set of numpy functions.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (14 by maintainers)

github_iconTop GitHub Comments

4reactions
hameerabbasicommented, May 18, 2018

There’s a related discussion over on #8994 about making the three-argument version of np.where dispatch to a ufunc.

Overall, I agree with @mrocklin fairly strongly here that Numpy should move towards a protocol-oriented and ufunc-based architecture where at all possible. This would allow for other projects and the ecosystem as a whole to benefit from this as well.

I would be willing to co-author an (or multiple) NEPs, perhaps in conjunction with @njsmith, @shoyer. Hopefully @mrocklin joins in too.

2reactions
mrocklincommented, May 17, 2018

Conversation seems to have slowed down a bit here.

To summarize what I’m hearing above it sounds like at one point this was considered but then an alternative was put in place. It’s not clear what the reasoning was at the time, but presumably one could do some sleuthing to find out why. There is some mild concern from @shoyer that some code might be built expecting np.ones_like to definitely return a numpy array, which seems like a fair concern that should be discussed, but may not be a blocking issue. No one else has yet voiced strong dissent or support of this issue. Are there any other thoughts or concerns about this topic?

From my limited perspective the world would be a nicer place if Numpy would tend towards the default of making operations ufuncs. This would allow the ecosystem to evolve more quickly towards using generic code in gpu, sparse, and parallel situations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.ones_like — NumPy v1.24 Manual
numpy.ones_like# ... Return an array of ones with the same shape and type as a given array. ... Overrides the data type of...
Read more >
numpy.ones_like() in Python - GeeksforGeeks
The numpy.one_like() function returns an array of given shape and type as a given array, with ones. Syntax: numpy.ones_like(array, ...
Read more >
tf.experimental.numpy.ones_like | TensorFlow v2.11.0
TensorFlow variant of NumPy's ones_like . tf.experimental.numpy.ones_like( a, dtype=None ). Unsupported arguments: order , subok , shape .
Read more >
Python NumPy ones_like() Function Example - Morioh
Python NumPy ones_like() is an inbuilt function that contains four parameters and is used to return an array of the similar shape and...
Read more >
numpy.ones_like Example - Program Talk
python code examples for numpy.ones_like. ... input loadProtocolNcycles must be a integer, list, or numpy array') def _getCyclePoints(P1, P2, Nsteps):.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found