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

Fix error in Binomial to retain lazy logit initialization #46055

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/test_distributions.py
Expand Up @@ -4005,6 +4005,7 @@ def test_continuous_bernoulli_with_logits_overflow(self):
expected_gradient=tensor_type([0.]))


# TODO: make this a pytest parameterized test
class TestLazyLogitsInitialization(TestCase):
def setUp(self):
super(TestLazyLogitsInitialization, self).setUp()
Expand All @@ -4015,7 +4016,7 @@ def setUp(self):

def test_lazy_logits_initialization(self):
for Dist, params in self.examples:
param = params[0]
param = params[0].copy()
if 'probs' in param:
probs = param.pop('probs')
param['logits'] = probs_to_logits(probs)
Expand All @@ -4034,7 +4035,7 @@ def test_lazy_logits_initialization(self):

def test_lazy_probs_initialization(self):
for Dist, params in self.examples:
param = params[0]
param = params[0].copy()
if 'probs' in param:
dist = Dist(**param)
dist.sample()
Expand Down
10 changes: 2 additions & 8 deletions torch/distributions/binomial.py
@@ -1,4 +1,3 @@
from numbers import Number
import torch
from torch.distributions import constraints
from torch.distributions.distribution import Distribution
Expand Down Expand Up @@ -42,18 +41,13 @@ def __init__(self, total_count=1, probs=None, logits=None, validate_args=None):
raise ValueError("Either `probs` or `logits` must be specified, but not both.")
if probs is not None:
self.total_count, self.probs, = broadcast_all(total_count, probs)
self.total_count = self.total_count.type_as(self.logits)
is_scalar = isinstance(self.probs, Number)
self.total_count = self.total_count.type_as(self.probs)
else:
self.total_count, self.logits, = broadcast_all(total_count, logits)
self.total_count = self.total_count.type_as(self.logits)
is_scalar = isinstance(self.logits, Number)

self._param = self.probs if probs is not None else self.logits
if is_scalar:
batch_shape = torch.Size()
else:
batch_shape = self._param.size()
batch_shape = self._param.size()
super(Binomial, self).__init__(batch_shape, validate_args=validate_args)

def expand(self, batch_shape, _instance=None):
Expand Down