-
Notifications
You must be signed in to change notification settings - Fork 246
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
Use ExpandedDistribution in numpyro.plate #616
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,19 +263,16 @@ def process_message(self, msg): | |
frame = CondIndepStackFrame(self.name, self.dim, self.subsample_size) | ||
cond_indep_stack.append(frame) | ||
expected_shape = self._get_batch_shape(cond_indep_stack) | ||
dist_batch_shape = msg['fn'].batch_shape if msg['type'] == 'sample' else () | ||
overlap_idx = max(len(expected_shape) - len(dist_batch_shape), 0) | ||
trailing_shape = expected_shape[overlap_idx:] | ||
# e.g. distribution with batch shape (1, 5) cannot be broadcast to (5, 5) | ||
broadcast_shape = lax.broadcast_shapes(trailing_shape, dist_batch_shape) | ||
if broadcast_shape != dist_batch_shape: | ||
raise ValueError('Distribution batch shape = {} cannot be broadcast up to {}. ' | ||
'Consider using unbatched distributions.' | ||
.format(dist_batch_shape, broadcast_shape)) | ||
batch_shape = expected_shape[:overlap_idx] | ||
if 'sample_shape' in msg['kwargs']: | ||
batch_shape = lax.broadcast_shapes(msg['kwargs']['sample_shape'], batch_shape) | ||
msg['kwargs']['sample_shape'] = batch_shape | ||
if msg['type'] == 'sample': | ||
dist_batch_shape = msg['fn'].batch_shape | ||
if 'sample_shape' in msg['kwargs']: | ||
dist_batch_shape = msg['kwargs']['sample_shape'] + dist_batch_shape | ||
msg['kwargs']['sample_shape'] = () | ||
overlap_idx = max(len(expected_shape) - len(dist_batch_shape), 0) | ||
trailing_shape = expected_shape[overlap_idx:] | ||
broadcast_shape = lax.broadcast_shapes(trailing_shape, dist_batch_shape) | ||
batch_shape = expected_shape[:overlap_idx] + broadcast_shape | ||
msg['fn'] = msg['fn'].expand(batch_shape) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also expand when sample_shape is available at process or postprocess trace handler for consistency? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, maybe at the constructor of ExpandedDist, we can check if base dist is expanded dist. If so, we just need to store There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Let me see how we are doing this in Pyro, I agree multiple layers of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. It is a bit confusing. I like your change because it solves the shape broadcasting issue that we have before (the ValueError in the previous code). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding..
Just by using nested plates, we will never hit this condition where the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I already add this optimization in #617 (just to make sure that we can use |
||
if self.size != self.subsample_size: | ||
scale = 1. if msg['scale'] is None else msg['scale'] | ||
msg['scale'] = scale * self.size / self.subsample_size | ||
|
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.
This optimization is addressed above. Do you think we need extra logic here?
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.
I missed that during merge, I think we should still do an explicit broadcast so that.
dist.expand(5, 4).expand(6, 4)
raises an error. Will fix.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.
Hmm,.expand(5, 4).expand(6, 4)
seems to be invalid, hence an error is expected. Did you mean something like:.expand(5, 1).expand(1, 4)
?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.
You are right!!