tfp.experimental.nn.ConvolutionTransposeVariationalFlipout

ConvolutionTranspose layer class with Flipout estimator.

Inherits From: VariationalLayer, Layer

This layer implements the Bayesian variational inference analogue to a ConvolutionTranspose layer by assuming the kernel and/or the bias are drawn from distributions. By default, the layer implements a stochastic forward pass via sampling from the kernel and bias posteriors,

kernel, bias ~ posterior
outputs = tf.nn.conv_transpose(inputs, kernel) + bias

It uses the Flipout estimator [(Wen et al., 2018)][1], which performs a Monte Carlo approximation of the distribution integrating over the kernel and bias. Flipout uses roughly twice as many floating point operations as the reparameterization estimator but has the advantage of significantly lower variance.

The arguments permit separate specification of the surrogate posterior (q(W|x)), prior (p(W)), and divergence for both the kernel and bias distributions.

Upon being built, this layer adds losses (accessible via the losses property) representing the divergences of kernel and/or bias surrogate posteriors and their respective priors. When doing minibatch stochastic optimization, make sure to scale this loss such that it is applied just once per epoch (e.g. if kl is the sum of losses for each element of the batch, you should pass kl / num_examples_per_epoch to your optimizer).

inputs = tf.transpose(inputs, tf.concat([
    [0], tf.range(2, tf.rank(inputs)), [1]], axis=0))

Examples

We illustrate a Bayesian autoencoder network with variational inference, assuming a dataset of images. Note that this examples is not a variational autoencoder, rather it is a Bayesian Autoencoder which also uses variational inference.

# Using the following substitution, see:
tfn = tfp.experimental.nn
help(tfn.ConvolutionTransposeVariationalReparameterization)
BayesConv2D = functools.partial(
    tfn.ConvolutionVariationalFlipout,
    rank=2,
    padding='same',
    filter_shape=5,
    # Use `he_uniform` because we'll use the `relu` family.
    init_kernel_fn=tf.initializers.he_uniform())
BayesDeconv2D = functools.partial(
    tfn.ConvolutionTransposeVariationalFlipout,
    rank=2,
    padding='same',
    filter_shape=5,
    # Use `he_uniform` because we'll use the `relu` family.
    init_kernel_fn=tf.initializers.he_uniform())

This example uses reparameterization gradients to minimize the Kullback-Leibler divergence up to a constant, also known as the negative Evidence Lower Bound. It consists of the sum of two terms: the expected negative log-likelihood, which we approximate via Monte Carlo; and the KL divergence, which is added via regularizer terms which are arguments to the layer.

References

[1]: Yeming Wen, Paul Vicol, Jimmy Ba, Dustin Tran, and Roger Grosse. Flipout: Efficient Pseudo-Independent Weight Perturbations on Mini-Batches. In International Conference on Learning Representations, 2018. https://arxiv.org/abs/1803.04386

input_size ... In Keras, this argument is inferred from the rightmost input shape, i.e., tf.shape(inputs)[-1]. This argument specifies the size of the second from the rightmost dimension of both inputs and kernel. Default value: None.
output_size ... In Keras, this argument is called filters. This argument specifies the rightmost dimension size of both kernel and bias.
filter_shape ... In Keras, this argument is called kernel_size. This argument specifies the leftmost rank dimensions' sizes of kernel.
rank An integer, the rank of the convolution, e.g. "2" for 2D convolution. This argument implies the number of kernel dimensions, i.e.,kernel.shape.rank == rank + 2. In Keras, this argument has the same name and semantics. Default value:2. </td> </tr><tr> <td>strides</td> <td> An integer or tuple/list of n integers, specifying the stride length of the convolution. In Keras, this argument has the same name and semantics. Default value:1. </td> </tr><tr> <td>padding</td> <td> One of"VALID"or"SAME"(case-insensitive). In Keras, this argument has the same name and semantics (except we don't support"CAUSAL"). Default value:'VALID'. </td> </tr><tr> <td>dilations</td> <td> An integer or tuple/list ofrankintegers, specifying the dilation rate to use for dilated convolution. Currently, specifying anydilationsvalue != 1 is incompatible with specifying anystridesvalue != 1. In Keras, this argument is calleddilation_rate. Default value:1. </td> </tr><tr> <td>output_padding</td> <td> Anintor length-ranktuple/list representing the amount of padding along the input spatial dimensions (e.g., depth, height, width). A singleintindicates the same value for all spatial dimensions. The amount of output padding along a given dimension must be lower than the stride along that same dimension. If set toNone(default), the output shape is inferred. In Keras, this argument has the same name and semantics. Default value:None(i.e., inferred). </td> </tr><tr> <td>init_kernel_fn</td> <td> ... Default value:None(i.e., <a href="../../../tfp/experimental/nn/initializers/glorot_uniform"><code>tfp.experimental.nn.initializers.glorot_uniform()</code></a>). </td> </tr><tr> <td>init_bias_fn</td> <td> ... Default value:None(i.e., <a href="https://www.tensorflow.org/api_docs/python/tf/keras/initializers/Zeros"><code>tf.initializers.zeros()</code></a>). </td> </tr><tr> <td>make_posterior_fn</td> <td> ... Default value: <a href="../../../tfp/experimental/nn/util/make_kernel_bias_posterior_mvn_diag"><code>tfp.experimental.nn.util.make_kernel_bias_posterior_mvn_diag</code></a>. </td> </tr><tr> <td>make_prior_fn</td> <td> ... Default value: <a href="../../../tfp/experimental/nn/util/make_kernel_bias_prior_spike_and_slab"><code>tfp.experimental.nn.util.make_kernel_bias_prior_spike_and_slab</code></a>. </td> </tr><tr> <td>posterior_value_fn</td> <td> ... Default valye:tfd.Distribution.sample</td> </tr><tr> <td>unpack_weights_fn</td> <td> Default value:unpack_kernel_and_bias</td> </tr><tr> <td>dtype</td> <td> ... Default value: <a href="https://www.tensorflow.org/api_docs/python/tf#float32"><code>tf.float32</code></a>. </td> </tr><tr> <td>penalty_weight</td> <td> ... Default value:None(i.e., weight is1). </td> </tr><tr> <td>posterior_penalty_fn</td> <td> ... Default value:kl_divergence_monte_carlo. </td> </tr><tr> <td>activation_fn</td> <td> ... Default value:None. </td> </tr><tr> <td>seed</td> <td> ... Default value:None(i.e., no seed). </td> </tr><tr> <td>name</td> <td> ... Default value:None(i.e.,'ConvolutionTransposeVariationalFlipout'`).

activation_fn

also_track

dtype

extra_loss

extra_result

name Returns the name of this module as passed or determined in the ctor.

name_scope Returns a tf.name_scope instance for this class.
penalty_weight

posterior

posterior_penalty_fn

posterior_value_fn

prior

submodules Sequence of all sub-modules.

Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on).

a = tf.Module()
b = tf.Module()
c = tf.Module()
a.b = b
b.c = c
list(a.submodules) == [b, c]
True
list(b.submodules) == [c]
True
list(c.submodules) == []
True

trainable_variables Sequence of trainable variables owned by this module and its submodules.

unpack_weights_fn

variables Sequence of variables owned by this module and its submodules.

Methods

eval

View source

load

View source

save

View source

summary

View source

with_name_scope

Decorator to automatically enter the module name scope.

class MyModule(tf.Module):
  @tf.Module.with_name_scope
  def __call__(self, x):
    if not hasattr(self, 'w'):
      self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))
    return tf.matmul(x, self.w)

Using the above module would produce tf.Variables and tf.Tensors whose names included the module name:

mod = MyModule()
mod(tf.ones([1, 2]))
<tf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)>
mod.w
<tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,
numpy=..., dtype=float32)>

Args
method The method to wrap.

Returns
The original method wrapped such that it enters the module's name scope.

__call__

View source

Call self as a function.