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

Expose ImproperUniform distribution #2516

Merged
merged 2 commits into from
Jun 2, 2020
Merged

Expose ImproperUniform distribution #2516

merged 2 commits into from
Jun 2, 2020

Conversation

fritzo
Copy link
Member

@fritzo fritzo commented Jun 2, 2020

Addresses #2426
Follows up on @fehiepsi's comment #2495 (review)

This exposes ImproperUniform as a public distribution. It is currently used only in SplitReparam. I plan to use it also for sampling auxiliary variables in CompartmentalModel._sequential_model() and ._vectorized_model(). Those classes currently use dist.Uniform(...).mask(False).expand(shape).to_event(), but I would like to generalize the logic to arbitrary constraints, while adding the safety of a not-implemented .sample() method.

Tested

  • refactoring is covered by existing SplitReparam tests
  • added some unit tests

fehiepsi
fehiepsi previously approved these changes Jun 2, 2020
Copy link
Member

@fehiepsi fehiepsi left a comment

Choose a reason for hiding this comment

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

LGTM. I just have a comment for default args of this distribution.

from .util import broadcast_shape


class ImproperUniform(TorchDistribution):
Copy link
Member

Choose a reason for hiding this comment

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

Just to make sure that you already consider it: should it be better to use the shorter name Improper?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm... I guess there are other improper distributions, e.g. Unit.

"""
arg_constraints = {}

def __init__(self, support, batch_shape=torch.Size(), event_shape=torch.Size()):
Copy link
Member

Choose a reason for hiding this comment

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

I think event_shape is a requirement. Otherwise, we can set it to None, so that the default event_shape can be (0,) * biject_to(support).event_dim. This way, log_prob will have a more appropriate shape.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think i'd rather make them both required than perform a computation on the constraint. Currently our constraints are pretty sloppy- we should be using IndependentConstraint more thoroughly and many of the computations would be wrong. This isn't a big issue yet because we don't use biject_to(support).event_dim for much.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I agree that we should require event_shape. But for batch_shape, I guess it is better to let it have default shape as before (i.e. __init__(self, support, event_shape, batch_shape=torch.Size())) to makes the code less verbose.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree a default of batch_shape=() makes sense, but I find it disorienting for batch_shape to be specified on the right of event_shape: I always see things as batch_shape, event_shape. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Haha, sounds reasonable to me.

Copy link
Member

@fehiepsi fehiepsi left a comment

Choose a reason for hiding this comment

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

Thanks @fritzo , I think this is a great distribution to have!

@fehiepsi fehiepsi merged commit be09d08 into dev Jun 2, 2020
@fritzo
Copy link
Member Author

fritzo commented Jun 2, 2020

Thanks for your careful review @fehiepsi! I'm glad you care about interface elegance 😄

@fritzo fritzo deleted the improper-uniform branch June 5, 2020 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants