-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[WIP] randint function #6136
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
[WIP] randint function #6136
Conversation
|
This needs documentation in |
|
|
||
| Tensor randint(const Type& dtype, IntList size, Generator* generator) { | ||
| Tensor result = dtype.tensor(size); | ||
| return result.random_(0, 1, generator); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| return result; | ||
| } | ||
|
|
||
| Tensor randint(const Type& dtype, IntList size, int64_t low, int64_t high, Generator* generator) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| - func: randperm_out(Tensor result, int64_t n, *, Generator* generator=nullptr) -> Tensor | ||
| variants: function | ||
|
|
||
| - func: randint(Type dtype, IntList size, int64_t low, int64_t high, *, Generator* generator=nullptr) -> Tensor |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Left : adding docs and test, and remove the bug on size = (5)
|
These are the behaviours on corner cases otherwise working fine :
The first outcome is bothering me. Any fix? (Others are fine in my opinion) |
|
@pytorchbot test this please The behavior in the first case is just a Python-ism. |
|
@ezyang yeah that case is working (I should have included it in my examples). Also torch.randn((3)) works right, so therefore torch.randint((3,5,(4)) must also work for sake of continuity right? EDIT :
|
|
@pytorchbot test this please |
|
So, perhaps what you are suggesting is that randint should have an option |
|
@ezyang No we need to pass the size as a tuple only as we provide EDIT : I am suggesting to have option for Intlist to accept tuple of size 1 as well, which it somehow does for torch.randn |
|
No, what I am saying is that It accidentally works in Best not to get too crazy with the overloads here, IMO. |
|
@ezyang By the way if I overload it as torch.randint(int,int,int) then torch.randint(1,5,(3)) would work right. I can do that if you want. (Hacky I know :P ) |
|
I hate it! Leave it the way it is ;) |
|
Looks reasonable, but this still needs doc and tests. |
|
Will commit by tommorow @ssnl :) |
|
I am having doubt how should I document the function.
Now I think it might confuse some people as (in general) it's not possible for first argument to be optional before second and third arguments (They dont know I used function overloading for the feature). Do you think it's alright to document it like this?? |
|
We already have docs where first arg is written as optional, e.g. http://pytorch.org/docs/master/torch.html#torch.arange. So it should be fine. |
Tested with manual seeds in some test cases as well. Seems fine to me (check documentation though)
|
@ssnl I guess it's ready to merge now? |
|
@ssnl is there anything more to add or correct now? |
ssnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally looks great except for two minor things.
torch/_torch_docs.py
Outdated
| The shape of the tensor is defined by the variable argument :attr:`sizes`. | ||
| Args: | ||
| low (int, optional): Lowest (positive) integer to be drawn from the distribution. Default: 0. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| - func: randn_like(Tensor self, *, Type dtype) -> Tensor | ||
| variants: function | ||
|
|
||
| - func: randint(Type dtype, int64_t high, IntList size, *, Generator* generator=nullptr) -> Tensor |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_torch_docs.py
Outdated
|
|
||
| add_docstr(torch.randint, | ||
| r""" | ||
| randint(low=0, high, sizes, out=None) -> Tensor |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
ssnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but need some owner to look at this.
torch/_torch_docs.py
Outdated
| high (int): One above the highest integer to be drawn from the distribution. | ||
| sizes (tuple): a tuple defining the shape of the output tensor. | ||
| out (Tensor, optional): the output tensor | ||
| dtype (torch.dpython:type, optional) – the desired type of returned Tensor. Default: torch.float32 |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
Can we merge this now? |
colesbury
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
|
|
|
It'll probably come in next release! |
|
I think some |
|
Looks like @vadimkantorov is right. in the random functions section, look here |
|
Ohh I see! |
Added randint functionality as requested in [#5874].