TFGENZOO.flows.actnorm module

class TFGENZOO.flows.actnorm.Actnorm(scale: float = 1.0, logscale_factor: float = 3.0, **kwargs)[source]

Bases: TFGENZOO.flows.flowbase.FlowComponent

Actnorm Layer

Sources:

Note

  • initialize
    mean = mean(first_batch)
    var = variance(first_batch)
    logs = log(scale / sqrt(var)) / logscale_factor
    bias = - mean
  • forward formula
    logs = logs * logscale_factor
    scale = exp(logs)
    z = (x + bias) * scale
    log_det_jacobain = sum(logs) * H * W
  • inverse formula
    logs = logs * logsscale_factor
    inv_scale = exp(-logs)
    z = x * inv_scale - bias
    inverse_log_det_jacobian = sum(- logs) * H * W
calc_ldj

bool flag of calculate log det jacobian

scale

float initialize batch’s variance scaling

logscale_factor

float barrier log value to - Inf

Examples

>>> import tensorflow as tf
>>> import TFGENZOO.flows import Actnorm
>>> ac = Actnorm()
>>> ac.build([None, 16, 16, 4])
>>> ac.get_config()
{'name': 'actnorm_1', ... }
>>> inputs = tf.keras.Input([16, 16, 4])
>>> ac(inputs)
(<tf.Tensor 'actnorm_1_2/Identity:0' shape=(None, 16, 16, 4) dtype=float32>,
 <tf.Tensor 'actnorm_1_2/Identity_1:0' shape=(None,) dtype=float32>)
>>>  tf.keras.Model(inputs, ac(inputs)).summary()
Model: "model_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_3 (InputLayer)         [(None, 16, 16, 4)]       0
_________________________________________________________________
actnorm_1 (Actnorm)          ((None, 16, 16, 4), (None 9
=================================================================
Total params: 9
Trainable params: 0
Non-trainable params: 9
_________________________________________________________________
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).

data_dep_initialize(x: tensorflow.python.framework.ops.Tensor)[source]
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]