-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
gaussian binary tree inference_gym collider model #1349
Open
gisilvs
wants to merge
2
commits into
tensorflow:main
Choose a base branch
from
gisilvs:collider_model_gym
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
103 changes: 103 additions & 0 deletions
103
spinoffs/inference_gym/inference_gym/targets/gaussian_binary_tree.py
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,103 @@ | ||
import functools | ||
|
||
import tensorflow.compat.v2 as tf | ||
from inference_gym.targets import bayesian_model | ||
from inference_gym.targets import model | ||
|
||
import tensorflow_probability as tfp | ||
|
||
tfb = tfp.bijectors | ||
tfd = tfp.distributions | ||
|
||
Root = tfd.JointDistributionCoroutine.Root | ||
|
||
# coupling link could be e.g. tf.nn.tanh | ||
def gaussian_binary_tree_prior_fn(num_layers, initial_scale, nodes_scale, | ||
coupling_link=None): | ||
initial_loc = 0. | ||
nodes = [] | ||
# in the "root" layer (or inverse root, as it is a reversed tree) we have | ||
# 2**num_layers nodes (with depth 2 --> 4 nodes, depth 4 --> 16 nodes) | ||
for i in range(2 ** num_layers): | ||
node = yield Root(tfd.Normal(initial_loc, initial_scale)) | ||
nodes.append(node) | ||
# for the remaining layers, we then sample the respective nodes values | ||
# applying the link function | ||
# we do not do this for the final node, as it is supposed to be observed | ||
for l in range(num_layers, 1, -1): | ||
next_layer_nodes = [] | ||
for i in range(0, (2 ** l), 2): | ||
if coupling_link: | ||
node = yield tfd.Independent( | ||
tfd.Normal(coupling_link(nodes[i]) - coupling_link(nodes[i + 1]), | ||
nodes_scale), 0) | ||
else: | ||
node = yield tfd.Independent( | ||
tfd.Normal(nodes[i] - nodes[i + 1], | ||
nodes_scale), 0) | ||
next_layer_nodes.append(node) | ||
nodes = next_layer_nodes | ||
|
||
|
||
def gaussian_binary_tree_log_likelihood_fn(values, observed_last_node, | ||
nodes_scale, coupling_link=None): | ||
left_node, right_node = values[-2], values[-1] | ||
if coupling_link: | ||
lps = tfd.Normal(loc=coupling_link(left_node) - coupling_link(right_node), | ||
scale=nodes_scale).log_prob(observed_last_node) | ||
else: | ||
lps = tfd.Normal(loc=left_node - right_node, | ||
scale=nodes_scale).log_prob(observed_last_node) | ||
return lps | ||
|
||
|
||
class GaussianBinaryTree(bayesian_model.BayesianModel): | ||
def __init__(self, | ||
num_layers, | ||
observed_last_node, | ||
initial_scale, | ||
nodes_scale, | ||
coupling_link=None, | ||
name='gaussian_binary_tree', | ||
pretty_name='Gaussian Binary Tree'): | ||
"""Construct the Gaussian Binary Tree model.""" | ||
with tf.name_scope(name): | ||
self._prior_dist = tfd.JointDistributionCoroutine(functools.partial( | ||
gaussian_binary_tree_prior_fn, | ||
num_layers=num_layers, | ||
initial_scale=initial_scale, | ||
nodes_scale=nodes_scale, | ||
coupling_link=coupling_link | ||
)) | ||
|
||
self._log_likelihood_fn = functools.partial( | ||
gaussian_binary_tree_log_likelihood_fn, | ||
observed_last_node=observed_last_node, | ||
nodes_scale=nodes_scale, | ||
coupling_link=coupling_link | ||
) | ||
|
||
# todo: what should I use here? | ||
sample_transformations = { | ||
'identity': | ||
model.Model.SampleTransformation( | ||
fn=lambda params: params, | ||
pretty_name='Identity', | ||
dtype=self._prior_dist.dtype, | ||
) | ||
} | ||
|
||
super(GaussianBinaryTree, self).__init__( | ||
default_event_space_bijector=tfb.Identity(), # todo: what should I use here? | ||
event_shape=self._prior_dist.event_shape, | ||
dtype=self._prior_dist.dtype, | ||
name=name, | ||
pretty_name=pretty_name, | ||
sample_transformations=sample_transformations | ||
) | ||
|
||
def _prior_distribution(self): | ||
return self._prior_dist | ||
|
||
def _log_likelihood(self, value): | ||
return self._log_likelihood_fn(value) |
Empty file.
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.
It would be more efficient to write each layer as a single distribution with batch shape:
We'd need to be sure that the CF code does the right thing on batched distributions (which should be treated equivalently to a list of independent dists), but we'd need to do that anyway.