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

Add default values to constraints to make grad propogate properly #872

Merged
merged 5 commits into from
Jan 14, 2021

Conversation

fehiepsi
Copy link
Member

@fehiepsi fehiepsi commented Jan 13, 2021

Fixes #871.

In pyro-ppl/pyro#2447, @fritzo proposed to implement a safe_mask function, but it seems to me that this is an easier to implement solution.

Copy link
Member

@fritzo fritzo left a comment

Choose a reason for hiding this comment

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

This is a great idea, especially since constraints do the checking.

Could I suggest a different interface, so as to be compatible with PyTorch, where we might need to know precision and device placement?

class Constraint:
    def default_like(self, prototype):
        raise NotImplementedError

class Positive(Constraint):
    def default_like(self, prototype):
        return np.ones(prototype.shape, dtype=prototype.dtype)

This is similar to torch.zeros_like() and similar functions. If you're ok with this, I'd like to copy this pattern in PyTorch/Pyro. We could alternatively name this .feasible_like().

numpyro/distributions/distribution.py Outdated Show resolved Hide resolved
@fehiepsi
Copy link
Member Author

Thanks, @fritzo! I'll update the api. It looks reasonable to me.

Copy link
Member

@fritzo fritzo left a comment

Choose a reason for hiding this comment

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

Hmm I think I prefer .feasible_like() over .default_like(), since the former evokes the language of constraints. @fehiepsi do you have a preference?

numpyro/distributions/constraints.py Outdated Show resolved Hide resolved
Copy link
Member

@fritzo fritzo left a comment

Choose a reason for hiding this comment

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

Looks great! Mind if I port these to PyTorch, or would you like to?

@fehiepsi
Copy link
Member Author

Thanks for reviewing and valuable feedbacks, @fritzo! I would appreciate if you port those to PyTorch. :)

@fritzo fritzo merged commit 85289ef into pyro-ppl:master Jan 14, 2021
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.

Gradient propagation failure for masked invalid values
2 participants