TFGENZOO.flows.affine_coupling module

class TFGENZOO.flows.affine_coupling.AffineCoupling(mask_type: TFGENZOO.flows.affine_coupling.AffineCouplingMask = <AffineCouplingMask.ChannelWise: 1>, scale_shift_net: tensorflow.python.keras.engine.base_layer.Layer = None, scale_shift_net_template: Callable[[tensorflow.python.keras.engine.input_layer.Input], tensorflow.python.keras.engine.training.Model] = None, scale_type='safe_exp', **kwargs)[source]

Bases: TFGENZOO.flows.flowbase.FlowComponent

Affine Coupling Layer

Sources:

https://github.com/masa-su/pixyz/blob/master/pixyz/flows/coupling.py

Note

  • forward formula
    [x1, x2] = split(x)
    log_scale, shift = NN(x1)
    scale = sigmoid(log_scale + 2.0)
    z1 = x1
    z2 = (x2 + shift) * scale
    z = concat([z1, z2])
    LogDetJacobian = sum(log(scale))
  • inverse formula
    [z1, z2] = split(x)
    log_scale, shift = NN(z1)
    scale = sigmoid(log_scale + 2.0)
    x1 = z1
    x2 = z2 / scale - shift
    z = concat([x1, x2])
    InverseLogDetJacobian = - sum(log(scale))
  • implementation notes
    in Glow’s Paper, scale is calculated by exp(log_scale),
    but IN IMPLEMENTATION, scale is done by sigmoid(log_scale + 2.0)

Examples

>>> import tensorflow as tf
>>> from TFGENZOO.flows.affine_coupling import AffineCoupling
>>> from TFGENZOO.layers.resnet import ShallowResNet
>>> af = AffineCoupling(scale_shift_net_template=ShallowResNet)
>>> af.build([None, 16, 16, 4])
>>> af.get_config()
    {'name': 'affine_coupling_1', ...}
>>> inputs = tf.keras.Input([16, 16, 4])
>>> af(inputs)
(<tf.Tensor 'affine_coupling_3_2/Identity:0' shape=(None, 16, 16, 4) dtype=float32>,
 <tf.Tensor 'affine_coupling_3_2/Identity_1:0' shape=(None,) dtype=float32>)
>>> tf.keras.Model(inputs, af(inputs)).summary()
Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_3 (InputLayer)         [(None, 16, 16, 4)]       0
_________________________________________________________________
affine_coupling (AffineCoupl ((None, 16, 16, 4), (None 2389003
=================================================================
Total params: 2,389,003
Trainable params: 0
Non-trainable params: 2,389,003
_________________________________________________________________
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.affine_coupling.AffineCouplingMask[source]

Bases: enum.Enum

An enumeration.

ChannelWise = 1
class TFGENZOO.flows.affine_coupling.LogScale(log_scale_factor: float = 3.0, **kwargs)[source]

Bases: tensorflow.python.keras.engine.base_layer.Layer

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).

call(x: tensorflow.python.framework.ops.Tensor)[source]

This is where the layer’s logic lives.

Parameters
  • inputs – Input tensor, or list/tuple of input tensors.

  • **kwargs – Additional keyword arguments.

Returns

A tensor or list/tuple of tensors.

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.