forked from google-deepmind/deepmind-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variational.py
87 lines (72 loc) · 3.04 KB
/
variational.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2020 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Functions and classes for performing variational inference."""
from typing import Callable, Iterable, Optional
import haiku as hk
import jax
import jax.numpy as jnp
from tensorflow_probability.substrates import jax as tfp
tfd = tfp.distributions
class Variational(hk.Module):
"""A module representing the variational distribution q(H | *O).
H is assumed to be a continuous variable.
"""
def __init__(self,
common_layer_sizes: Iterable[int],
activation: Callable[[jax.Array], jax.Array] = jnp.tanh,
output_dim: int = 1,
name: Optional[str] = None):
"""Initialises a `Variational` instance.
Args:
common_layer_sizes: The number of hidden units in the shared dense
network layers.
activation: Nonlinearity function to apply to each of the
common layers.
output_dim: The dimensionality of `H`.
name: A name to assign to the module instance.
"""
super().__init__(name=name)
self._common_layer_sizes = common_layer_sizes
self._activation = activation
self._output_dim = output_dim
self._linear_layers = [
hk.Linear(layer_size)
for layer_size in self._common_layer_sizes
]
self._mean_output = hk.Linear(self._output_dim)
self._log_var_output = hk.Linear(self._output_dim)
def __call__(self, *args) -> tfd.Distribution:
"""Create a distribution for q(H | *O).
Args:
*args: `List[DeviceArray]`. Corresponds to the values of whatever
variables are in the conditional set *O.
Returns:
`tfp.distributions.NormalDistribution` instance.
"""
# Stack all inputs, ensuring that shapes are consistent and that they are
# all of dtype float32.
input_ = [hk.Flatten()(arg) for arg in args]
input_ = jnp.concatenate(input_, axis=1)
# Create a common set of layers, then final layer separates mean & log_var
for layer in self._linear_layers:
input_ = layer(input_)
input_ = self._activation(input_)
# input_ now represents a tensor of shape (batch_size, final_layer_size).
# This is now put through two final layers, one for the computation of each
# of the mean and standard deviation of the resultant distribution.
mean = self._mean_output(input_)
log_var = self._log_var_output(input_)
std = jnp.sqrt(jnp.exp(log_var))
return tfd.Normal(mean, std)