-
Notifications
You must be signed in to change notification settings - Fork 327
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
[Feature] Rework to_one_hot
and to_categorical
to take a tensor as parameter
#816
[Feature] Rework to_one_hot
and to_categorical
to take a tensor as parameter
#816
Conversation
to_onehot
and to_categorical
to take a tensor as parameter (WIP)to_onehot
and to_categorical
to take a tensor as parameter
0cd5ed4
to
ae10757
Compare
Hey @riiswa can I help to unblock this? |
Hey, I'm a little bit busy in this moment (exams at school). I think the PR is finished, I juste have a little bug on GPU (created tensor are not on the right device) 😄, and I don't have gpu to test locally. I think you can start the PR review, and when I have time I'll fix this little thing (unless you want to do it yourself) |
to_onehot
and to_categorical
to take a tensor as parameterto_onehot
and to_categorical
to take a tensor as parameter
Np i'll have a look |
63a8764
to
0736e9e
Compare
0736e9e
to
02a7c8e
Compare
@vmoens I'm finally back, and I think this PR is ready for a review😄 |
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.
Sorry to shuffle things a little: don't we want two separate methods for these?
like to_one_hot
and DiscreteTensorSpec.to(OneHotDiscreteTensorSpec)
?
torchrl/data/tensor_specs.py
Outdated
def to_categorical( | ||
self, val: Optional[torch.Tensor] = None, safe: bool = True | ||
) -> Union[DiscreteTensorSpec, torch.Tensor]: | ||
"""Convert a given one-hot tensor in categorical format or convert the spec to the equivalent categorical spec. |
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.
"""Convert a given one-hot tensor in categorical format or convert the spec to the equivalent categorical spec. | |
"""Converts a given one-hot tensor in categorical format or converts the spec to the equivalent categorical spec. |
torchrl/data/tensor_specs.py
Outdated
def to_categorical( | ||
self, val: Optional[torch.Tensor] = None, safe: bool = True | ||
) -> Union[MultiDiscreteTensorSpec, torch.Tensor]: | ||
"""Convert a given one-hot tensor in categorical format or convert the spec to the equivalent categorical spec. |
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.
"""Convert a given one-hot tensor in categorical format or convert the spec to the equivalent categorical spec. | |
"""Converts a given one-hot tensor in categorical format or converts the spec to the equivalent categorical spec. |
torchrl/data/tensor_specs.py
Outdated
def to_onehot( | ||
self, val: Optional[torch.Tensor] = None, safe: bool = True | ||
) -> Union[OneHotDiscreteTensorSpec, torch.Tensor]: | ||
"""One-hot encode a given tensor or convert the spec to the equivalent one-hot spec. |
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.
"""One-hot encode a given tensor or convert the spec to the equivalent one-hot spec. | |
"""Encodes a discrete tensor from the spec domain into its one-hot correspondent, or converts the spec to the equivalent one-hot spec. |
torchrl/data/tensor_specs.py
Outdated
def to_onehot( | ||
self, val: Optional[torch.Tensor] = None, safe: bool = True | ||
) -> Union[MultiOneHotDiscreteTensorSpec, torch.Tensor]: | ||
"""One-hot encode a given tensor or convert the spec to the equivalent one-hot spec. |
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.
"""One-hot encode a given tensor or convert the spec to the equivalent one-hot spec. | |
"""Encodes a discrete tensor from the spec domain into its one-hot correspondent, or converts the spec to the equivalent one-hot spec. |
Yes it's cleaner, but in case of |
My idea (but happy to challenge it) was that some day in the future we may code -- say -- a We could also map By I agree that |
For the moment I have written a specific function, let's work on the generic version in the future after implementing the |
to_onehot
and to_categorical
to take a tensor as parameterto_one_hot
and to_categorical
to take a tensor as parameter
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 thanks for this!
Description
As mentioned in the closed PR #783 we want to update the methods
to_onehot
andto_categorical
to support value conversion in addition to spec conversion.This is what currently happens
To keep the current behaviour (conversion of the spec), I decided that
val
will be an optional parameter. The documentation will be well written to explain the different behaviours of theses methods.Motivation and Context
Also related to the issue #781
Types of changes
What types of changes does your code introduce? Remove all that do not apply:
Checklist
Go over all the following points, and put an
x
in all the boxes that apply.If you are unsure about any of these, don't hesitate to ask. We are here to help!