Skip to content

Conversation

@colesbury
Copy link
Member

These are similar to numpy.take and numpy.put. The take function allows
you to linearly index into a tensor without viewing it as a 1D tensor
first. The output has the same shape as the indices. The put function
copies value into a tensor also using linear indices.

I'll add these to ATen+Variable in a subsequent PR

@colesbury
Copy link
Member Author

Two differences from the NumPy implementations:

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.take.html
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.put.html

numpy.take has an optional axis argument, which behaves like index_select. I didn't include this. We can add it later without breaking backwards compatibility.

numpy.put repeats the values if necessary to make them as long as indices. This is strange, since most other functions use broadcasting semantics. Currently, Tensor.put_ requires values to have the same length as indices. We can change this without breaking backwards compatibility.

Copy link
Contributor

@apaszke apaszke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general. A few minor comments. BTW putScalar might be useful as well

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@colesbury colesbury force-pushed the take_put branch 2 times, most recently from 087ff0f to 05ff99e Compare October 24, 2017 23:30
@colesbury colesbury force-pushed the take_put branch 2 times, most recently from 6a5036a to a5caa6a Compare October 31, 2017 22:27
@colesbury
Copy link
Member Author

colesbury commented Oct 31, 2017

I've added an accumulate option to put_ which handles duplicate elements (by summing them). This is necessary for the backwards of take to be correct when indices contains duplicate elements.

(The accumulate=True option is substantially slower, so accumulate is False by default).

These are similar to numpy.take and numpy.put. The take function allows
you to linearly index into a tensor without viewing it as a 1D tensor
first. The output has the same shape as the indices. The put function
copies value into a tensor also using linear indices.
@soumith soumith merged commit 7c0b16c into pytorch:master Nov 1, 2017
@colesbury colesbury deleted the take_put branch November 1, 2017 15:54
ngimel pushed a commit to ngimel/pytorch that referenced this pull request Nov 3, 2017
I have no idea how it worked on cuda 8, but apparently this fixes failures on cuda 9. cc @colesbury
soumith pushed a commit that referenced this pull request Nov 3, 2017
I have no idea how it worked on cuda 8, but apparently this fixes failures on cuda 9. cc @colesbury
colesbury pushed a commit that referenced this pull request Nov 6, 2017
I have no idea how it worked on cuda 8, but apparently this fixes failures on cuda 9. cc @colesbury
@nlgranger
Copy link

nlgranger commented Feb 22, 2018

@colesbury thanks for this work and especially the accumulate option. Are there any plans to add an axis argument to put_ as well? Specifying the index for each individual element of a tensor can take quite a lot of place in memory.

@alok
Copy link

alok commented Mar 5, 2018

It'd be nice to have an axis argument added, since sklearn.utils.safe_indexing (used by sklearn.utils.shuffle, which is handy for sampling multiple tensors along the same indices) uses X.take(indices,axis=0) and PyTorch fails.

@fmassa
Copy link
Member

fmassa commented Mar 5, 2018

From what I see, numpy doesn't have an axis argument for put, and take behavior can be simulated with advanced indexing.
@alok if I understand correctly, what you are looking for can be achieved with tensor.index_select

wuhuikx pushed a commit to wuhuikx/pytorch that referenced this pull request Jan 30, 2020
I have no idea how it worked on cuda 8, but apparently this fixes failures on cuda 9. cc @colesbury
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

Successfully merging this pull request may close these issues.

7 participants