PlanarNormalizingFlow

class tfsnippet.layers.PlanarNormalizingFlow(w_initializer=<tensorflow.python.ops.init_ops.RandomNormal object>, w_regularizer=None, b_initializer=<tensorflow.python.ops.init_ops.Zeros object>, b_regularizer=None, u_initializer=<tensorflow.python.ops.init_ops.RandomNormal object>, u_regularizer=None, trainable=True, name=None, scope=None)

Bases: tfsnippet.layers.flows.base.FeatureMappingFlow

A single layer Planar Normalizing Flow (Danilo 2016) with tanh activation function, as well as the invertible trick. The x and y are assumed to be 1-D random variable (i.e., value_ndims == 1)

\[\begin{split}\begin{aligned} \mathbf{y} &= \mathbf{x} + \mathbf{\hat{u}} \tanh(\mathbf{w}^\top\mathbf{x} + b) \\ \mathbf{\hat{u}} &= \mathbf{u} + \left[m(\mathbf{w}^\top \mathbf{u}) - (\mathbf{w}^\top \mathbf{u})\right] \cdot \frac{\mathbf{w}}{\|\mathbf{w}\|_2^2} \\ m(a) &= -1 + \log(1+\exp(a)) \end{aligned}\end{split}\]

Attributes Summary

axis Get the feature axis/axes.
explicitly_invertible Whether or not this flow is explicitly invertible?
name Get the name of this object.
require_batch_dims Whether or not this flow requires batch dimensions.
value_ndims Get the number of value dimensions in both x and y.
variable_scope Get the variable scope of this object.
x_value_ndims Get the number of value dimensions in x.
y_value_ndims Get the number of value dimensions in y.

Methods Summary

__call__(…) <==> x(…)
apply(input) Apply the layer on input, to produce output.
build([input]) Build the layer, creating all required variables.
inverse_transform(y[, compute_x, …]) Transform y into x, and compute the log-determinant of f^{-1} at y (i.e., \(\log \det \frac{\partial f^{-1}(y)}{\partial y}\)).
invert() Get the inverted flow from this flow.
transform(x[, compute_y, compute_log_det, name]) Transform x into y, and compute the log-determinant of f at x (i.e., \(\log \det \frac{\partial f(x)}{\partial x}\)).

Attributes Documentation

axis

Get the feature axis/axes.

Returns:
The feature axis/axes, as is specified
in the constructor.
Return type:int or tuple[int]
explicitly_invertible

Whether or not this flow is explicitly invertible?

If a flow is not explicitly invertible, then it only supports to transform x into y, and corresponding \(\log p(x)\) into \(\log p(y)\). It cannot compute \(\log p(y)\) directly without knowing x, nor can it transform x back into y.

Returns:
A boolean indicating whether or not the flow is explicitly
invertible.
Return type:bool
name

Get the name of this object.

require_batch_dims

Whether or not this flow requires batch dimensions.

value_ndims

Get the number of value dimensions in both x and y.

Returns:The number of value dimensions in both x and y.
Return type:int
variable_scope

Get the variable scope of this object.

x_value_ndims

Get the number of value dimensions in x.

Returns:The number of value dimensions in x.
Return type:int
y_value_ndims

Get the number of value dimensions in y.

Returns:The number of value dimensions in y.
Return type:int

Methods Documentation

__call__(...) <==> x(...)
apply(input)

Apply the layer on input, to produce output.

Parameters:input (Tensor or list[Tensor]) – The input tensor, or a list of input tensors.
Returns:The output tensor, or a list of output tensors.
build(input=None)

Build the layer, creating all required variables.

Parameters:input (Tensor or list[Tensor] or None) – If build() is called within apply(), it will be the input tensor(s). Otherwise if it is called separately, it will be None.
inverse_transform(y, compute_x=True, compute_log_det=True, name=None)

Transform y into x, and compute the log-determinant of f^{-1} at y (i.e., \(\log \det \frac{\partial f^{-1}(y)}{\partial y}\)).

Parameters:
  • y (Tensor) – The samples of y.
  • compute_x (bool) – Whether or not to compute \(x = f^{-1}(y)\)? Default True.
  • compute_log_det (bool) – Whether or not to compute the log-determinant? Default True.
  • name (str) – If specified, will use this name as the TensorFlow operational name scope.
Returns:

x and the (maybe summed) log-determinant.

The items in the returned tuple might be None if corresponding compute_? argument is set to False.

Return type:

(tf.Tensor, tf.Tensor)

Raises:
invert()

Get the inverted flow from this flow.

The transform() will become the inverse_transform() in the inverted flow, and the inverse_transform() will become the transform() in the inverted flow.

If the current flow has not been initialized, it must be initialized via inverse_transform() in the new flow.

Returns:The inverted flow.
Return type:tfsnippet.layers.InvertFlow
transform(x, compute_y=True, compute_log_det=True, name=None)

Transform x into y, and compute the log-determinant of f at x (i.e., \(\log \det \frac{\partial f(x)}{\partial x}\)).

Parameters:
  • x (Tensor) – The samples of x.
  • compute_y (bool) – Whether or not to compute \(y = f(x)\)? Default True.
  • compute_log_det (bool) – Whether or not to compute the log-determinant? Default True.
  • name (str) – If specified, will use this name as the TensorFlow operational name scope.
Returns:

y and the (maybe summed) log-determinant.

The items in the returned tuple might be None if corresponding compute_? argument is set to False.

Return type:

(tf.Tensor, tf.Tensor)

Raises:

RuntimeError – If both compute_y and compute_log_det are set to False.