Skip to content
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

Conversation

riiswa
Copy link
Contributor

@riiswa riiswa commented Jan 10, 2023

Description

As mentioned in the closed PR #783 we want to update the methods to_onehot and to_categorical to support value conversion in addition to spec conversion.

This is what currently happens

spec = torchrl.data.MultOneHotDiscreteTensorSpec([5,10])
sample = spec.rand((2,3))
sample.shape
torch.Size([2, 3, 15])
sample_np =spec.to_numpy(sample)
sample_np.shape
(2, 3, 2)
spec = torchrl.data.MultOneHotDiscreteTensorSpec([5,10])
sample = spec.rand((2,3))
sample.shape
torch.Size([2, 3, 15])
sample_np =spec.to_numpy(sample)
sample_np.shape
(2, 3, 15)

sample_cat = spec.to_categorical(sample)
sample_cat.shape
torch.Size([2, 3, 2])

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

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • New feature (non-breaking change which adds core functionality)
  • Documentation (update in the documentation)

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!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 10, 2023
@riiswa riiswa changed the title [Feature] Rework to_onehot and to_categorical to take a tensor as parameter (WIP) [WIP, Feature] Rework to_onehot and to_categorical to take a tensor as parameter Jan 10, 2023
@riiswa riiswa force-pushed the feature/to_categorical_and_to_one_hot_method_tensor_parameter branch from 0cd5ed4 to ae10757 Compare January 13, 2023 20:38
@vmoens
Copy link
Contributor

vmoens commented Jan 19, 2023

Hey @riiswa can I help to unblock this?

@riiswa
Copy link
Contributor Author

riiswa commented Jan 19, 2023

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)

@riiswa riiswa marked this pull request as ready for review January 19, 2023 12:46
@riiswa riiswa changed the title [WIP, Feature] Rework to_onehot and to_categorical to take a tensor as parameter [Feature] Rework to_onehot and to_categorical to take a tensor as parameter Jan 19, 2023
@vmoens
Copy link
Contributor

vmoens commented Jan 20, 2023

Np i'll have a look

@riiswa riiswa force-pushed the feature/to_categorical_and_to_one_hot_method_tensor_parameter branch 5 times, most recently from 63a8764 to 0736e9e Compare January 24, 2023 01:50
@riiswa riiswa force-pushed the feature/to_categorical_and_to_one_hot_method_tensor_parameter branch from 0736e9e to 02a7c8e Compare February 15, 2023 20:10
@riiswa
Copy link
Contributor Author

riiswa commented Feb 16, 2023

@vmoens I'm finally back, and I think this PR is ready for a review😄
@matteobettini maybe you should also check, because it's your idea

Copy link
Contributor

@vmoens vmoens left a 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)?

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""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.

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""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.

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""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.

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""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.

@riiswa
Copy link
Contributor Author

riiswa commented Feb 16, 2023

Yes it's cleaner, but in case of DiscreteTensorSpec.to(OneHotDiscreteTensorSpec) what can be the use of the parameter? I mean we will always convert it to the same OneHotDiscreteTensorSpec. The method can be named to_one_hot_spec()

@vmoens
Copy link
Contributor

vmoens commented Feb 16, 2023

Yes it's cleaner, but in case of DiscreteTensorSpec.to(OneHotDiscreteTensorSpec) what can be the use of the parameter? I mean we will always convert it to the same OneHotDiscreteTensorSpec. The method can be named to_one_hot_spec()

My idea (but happy to challenge it) was that some day in the future we may code -- say -- a HashDiscreteSpec where a hash key is used to code a discrete value (or some other embedding).
Then it may be useful to have a generic to(SpecType) method that handles all sorts of conversion.

We could also map Bounded to Unbounded with the same structure, etc.

By I agree that to_one_hot_spec is more explicit.

@riiswa
Copy link
Contributor Author

riiswa commented Feb 17, 2023

Then it may be useful to have a generic to(SpecType) method that handles all sorts of conversion.

For the moment I have written a specific function, let's work on the generic version in the future after implementing the HashDiscreteSpec!

@riiswa riiswa changed the title [Feature] Rework to_onehot and to_categorical to take a tensor as parameter [Feature] Rework to_one_hot and to_categorical to take a tensor as parameter Feb 17, 2023
Copy link
Contributor

@vmoens vmoens left a 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!

@vmoens vmoens merged commit 099ced3 into pytorch:main Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants