-
Notifications
You must be signed in to change notification settings - Fork 74
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] CompositeDistribution #517
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
04f7d6a
init
vmoens 00eabbe
amend
vmoens 65bb04d
init
vmoens af6b7e1
init
vmoens ebba91d
amend
vmoens 4449e4c
Merge remote-tracking branch 'origin/main' into composite_dist
vmoens 6871eeb
fix and amend
vmoens a316ad5
lint
vmoens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import torch | ||
from tensordict import TensorDict, TensorDictBase | ||
from tensordict._tensordict import unravel_keys | ||
from tensordict.utils import NestedKey | ||
from torch import distributions as d | ||
|
||
|
||
class CompositeDistribution(d.Distribution): | ||
"""A composition of distributions. | ||
|
||
Groups distributions together with the TensorDict interface. | ||
|
||
Args: | ||
params (TensorDictBase): a nested key-tensor map where the root entries | ||
point to the sample names, and the leaves are the distribution parameters. | ||
Entry names must match those of ``distribution_map``. | ||
|
||
distribution_map (Dict[NestedKey, Type[torch.distribution.Distribution]]): | ||
indicated the distribution types to be used. The names of the distributions | ||
will match the names of the samples in the tensordict. | ||
|
||
Keyword Arguments: | ||
extra_kwargs (Dict[NestedKey, Dict]): a possibly incomplete dictionary of | ||
extra keyword arguments for the distributions to be built. | ||
|
||
Examples: | ||
>>> params = TensorDict({ | ||
... "cont": {"loc": torch.randn(3, 4), "scale": torch.rand(3, 4)}, | ||
... ("nested", "disc"): {"logits": torch.randn(3, 10)} | ||
... }, [3]) | ||
>>> dist = CompositeDistribution(params, | ||
... distribution_map={"cont": d.Normal, ("nested", "disc"): d.Categorical}) | ||
>>> sample = dist.sample((4,)) | ||
>>> sample = dist.log_prob(sample) | ||
>>> print(sample) | ||
TensorDict( | ||
fields={ | ||
cont: Tensor(shape=torch.Size([4, 3, 4]), device=cpu, dtype=torch.float32, is_shared=False), | ||
cont_log_prob: Tensor(shape=torch.Size([4, 3, 4]), device=cpu, dtype=torch.float32, is_shared=False), | ||
nested: TensorDict( | ||
fields={ | ||
disc: Tensor(shape=torch.Size([4, 3]), device=cpu, dtype=torch.int64, is_shared=False), | ||
disc_log_prob: Tensor(shape=torch.Size([4, 3]), device=cpu, dtype=torch.float32, is_shared=False)}, | ||
batch_size=torch.Size([4]), | ||
device=None, | ||
is_shared=False)}, | ||
batch_size=torch.Size([4]), | ||
device=None, | ||
is_shared=False) | ||
""" | ||
|
||
def __init__(self, params: TensorDictBase, distribution_map, *, extra_kwargs=None): | ||
self._batch_shape = params.shape | ||
if extra_kwargs is None: | ||
extra_kwargs = {} | ||
dists = {} | ||
for name, dist_class in distribution_map.items(): | ||
dist_params = params.get(name, None) | ||
kwargs = extra_kwargs.get(name, {}) | ||
if dist_params is None: | ||
raise KeyError | ||
dist = dist_class(**dist_params, **kwargs) | ||
dists[name] = dist | ||
self.dists = dists | ||
|
||
def sample(self, shape=None): | ||
if shape is None: | ||
shape = torch.Size([]) | ||
samples = {name: dist.sample(shape) for name, dist in self.dists.items()} | ||
return TensorDict( | ||
samples, | ||
shape + self.batch_shape, | ||
) | ||
|
||
def rsample(self, shape=None): | ||
if shape is None: | ||
shape = torch.Size([]) | ||
return TensorDict( | ||
{name: dist.rsample(shape) for name, dist in self.dists.items()}, | ||
shape + self.batch_shape, | ||
) | ||
|
||
def log_prob(self, sample: TensorDictBase): | ||
d = { | ||
_add_suffix(name, "_log_prob"): dist.log_prob(sample.get(name)) | ||
for name, dist in self.dists.items() | ||
} | ||
sample.update(d) | ||
return sample | ||
|
||
def cdf(self, sample: TensorDictBase): | ||
cdfs = { | ||
_add_suffix(name, "_cdf"): dist.cdf(sample.get(name)) | ||
for name, dist in self.dists.items() | ||
} | ||
sample.update(cdfs) | ||
return sample | ||
|
||
def icdf(self, sample: TensorDictBase): | ||
"""Computes the inverse CDF. | ||
|
||
Requires the input tensordict to have one of `<sample_name>+'_cdf'` entry | ||
or a `<sample_name>` entry. | ||
|
||
Args: | ||
sample (TensorDictBase): a tensordict containing `<sample>_log_prob` where | ||
`<sample>` is the name of the sample provided during construction. | ||
""" | ||
for name, dist in self.dists.items(): | ||
prob = sample.get(_add_suffix(name, "_cdf"), None) | ||
if prob is None: | ||
try: | ||
prob = self.cdf(sample.get(name)) | ||
except KeyError: | ||
raise KeyError( | ||
f"Neither {name} nor {name + '_cdf'} could be found in the sampled tensordict. Make sure one of these is available to icdf." | ||
) | ||
icdf = dist.icdf(prob) | ||
sample.set(_add_suffix(name, "_icdf"), icdf) | ||
return sample | ||
|
||
|
||
def _add_suffix(key: NestedKey, suffix: str): | ||
key = unravel_keys(key) | ||
if isinstance(key, str): | ||
return key + suffix | ||
return key[:-1] + (key[-1] + suffix,) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we do not want these keys to be parametric?