TFGENZOO.flows.inv1x1conv module

class TFGENZOO.flows.inv1x1conv.Inv1x1Conv(log_det_type: str = 'slogdet', **kwargs)[source]

Bases: TFGENZOO.flows.flowbase.FlowComponent

Invertible 1x1 Convolution Layer

Sources:

https://arxiv.org/pdf/1807.03039.pdf https://github.com/openai/glow/blob/master/model.py#L457-L472

Note

  • forward formula
    \[\begin{split}\forall i, j: z_{i, j} &= Wx_{i, j} \\ LogDetJacobian &= hw \log|det(W)|\\ , where &\\ W &\in \mathbb{R}^{c imes c}\\ x &\in \mathbb{R}^{b \times h\times w \times c}\ \ \ ({\rm batch, height, width, channel})\end{split}\]
  • inverse formula
    \[\begin{split}\forall i, j: x_{i, j} &= W^{-1} z_{i, j}\\ InverseLogDetJacobian &= - h w \log|det(W)|\\ , where &\\ W &\in \mathbb{R}^{c\times c}\\ x &\in \mathbb{R}^{b \times h\times w \times c}\ \ \ ({\rm batch, height, width, channel})\end{split}\]

Examples

>>> import tensorflow as tf
>>> from TFGENZOO.flows import Inv1x1Conv
>>> ic = Inv1x1Conv()
>>> ic.build([None, 16, 16, 4])
>>> ic.get_config()
{'name': 'inv1x1_conv_1', 'trainable': {}, 'dtype': 'float32'}
>>> inputs = tf.keras.Input([16, 16, 4])
>>> tf.keras.Model(inputs, ic(inputs)).summary()
Layer (type)                 Output Shape              Param #
=================================================================
input_3 (InputLayer)         [(None, 16, 16, 4)]       0
_________________________________________________________________
inv1x1_conv_1 (Inv1x1Conv)   ((None, 16, 16, 4), (None 17
=================================================================
Total params: 17
Trainable params: 0
Non-trainable params: 17
_________________________________________________________________
build(input_shape: tensorflow.python.framework.tensor_shape.TensorShape)[source]

Creates the variables of the layer (optional, for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.

This is typically used to create the weights of Layer subclasses.

Parameters

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

forward(x: tensorflow.python.framework.ops.Tensor, **kwargs)[source]
get_config()[source]

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Returns

Python dictionary.

inverse(z: tensorflow.python.framework.ops.Tensor, **kwargs)[source]
class TFGENZOO.flows.inv1x1conv.Inv1x1Conv2DWithMask(**kwargs)[source]

Bases: TFGENZOO.flows.flowbase.FlowComponent

Invertible 1x1 Convolution Layer (2D) with Mask

Sources:

https://arxiv.org/pdf/1807.03039.pdf https://github.com/openai/glow/blob/master/model.py#L457-L472

Note

  • forward formula
    \[\begin{split}\forall i: z_{i} &= Wx_{i} \\ LogDetJacobian &= t \log|det(W)|\\ , where &\\ W &\in \mathbb{R}^{c imes c}\\ x &\in \mathbb{R}^{b \times h\times w \times c}\ \ \ ({\rm batch, timestep, channel})\end{split}\]
  • inverse formula
    \[\begin{split}\forall i: x_{i} &= W^{-1} z_{i}\\ InverseLogDetJacobian &= - t \log|det(W)|\\ , where &\\ W &\in \mathbb{R}^{c\times c}\\ x &\in \mathbb{R}^{b \times t\times c}\ \ \ ({\rm batch, timestep, channel})\end{split}\]
  • mask notes
    mask shape is [B, T, M] where M may be 1
    reference glow-tts
build(input_shape: tensorflow.python.framework.tensor_shape.TensorShape)[source]

Creates the variables of the layer (optional, for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.

This is typically used to create the weights of Layer subclasses.

Parameters

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

forward(x: tensorflow.python.framework.ops.Tensor, mask: tensorflow.python.framework.ops.Tensor = None, **kwargs)[source]
Parameters
  • x (tf.Tensor) – base input tensor [B, T, C]

  • mask (tf.Tensor) – mask input tensor [B, T, M] where M may be 1

Returns

latent variable tensor [B, T, C] ldj (tf.Tensor): log det jacobian [B]

Return type

z (tf.Tensor)

Notes

  • mask’s example
    [[[True], [True], [True], [False],
    [[True], [False], [False], [False],
    [[True], [True], [True], [True]],
    [[True], [True], [True], [True]]]
get_config()[source]

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Returns

Python dictionary.

inverse(z: tensorflow.python.framework.ops.Tensor, mask: tensorflow.python.framework.ops.Tensor = None, **kwargs)[source]
TFGENZOO.flows.inv1x1conv.regular_matrix_init(shape: Tuple[int, int], dtype=None)[source]

initialize with orthogonal matrix

Sources:

https://github.com/openai/glow/blob/master/model.py#L445-L451

Parameters
  • shape – generated matrix’s shape [C, C]

  • dtype

Returns

w_init, orthogonal matrix [C, C]

Return type

np.array