spqr

The spqr (acromyn for SPline Razionale Quadratica) module implements a coupling layer type bijector with a rational quadratic spline acting as conditioner.

class spqr.NeuralSplineFlow(*args, **kwargs)[source]

Neural Spline Flow bijector.

This is a coupling layer type bijector with rational quadratic spline acting as transformer. The coupling layer architecture can be defined as a list of masks (or a number of splits) that decides which variable is conditioned and which is the conditioner in each layer (or better, in each transformation step). Suppose we want to transform 3 variables using the mask [1,-1], where the negative number indicates the second part of the split as conditioner. Two coupling layers will be defined: the first one maps the feature \(x_1\) to itself and acts on features \(x_0\) and \(x_2\). A second coupling layer acts on these transformed variables \(x_0^\prime, x_1, x_2^\prime\) masking the feature \(x_{-1}\), i.e. \(x_0\) and trnsforming the others. The user may want to specify only in how many chunks he wants to split the number of variables. For this case the splits paramenter is defined: a corresponding number of coupling layers is created, where the j-th layer has a fration j / nsplit of input features masked.

See the RealNVP documentation for more infos.

Parameters
  • splits (int) – number of splits for the variables.

  • masks (list[int]) – list of masks for variables.

  • spline_params (dict) – dictionary of parameters for SplineInitializer.

Raises

ValueError

__init__(splits=None, masks=None, spline_params={})[source]

Default constructor

class spqr.SplineInitializer(nbins=128, border=4, hidden_layers=[512, 512], min_bin_gap=0.001, min_slope=0.001)[source]

Creates a rational quadratic spline with trainable parameters.

Parameters
  • nbins (int, optional) – Number of spline bins, defaults to 128.

  • border (float, optional) – Spline border, defaults to 4.

  • hidden_layers – Dimensions of each dense layer, defaults to [512, 512].

  • min_bin_gap (float, optional) – Minimum distance between subsequent bins, defaults to 1e-3.

  • min_slope (float, optional) – Mimimum spline slope in each bin, defaults to 1e-3.

Note

For more informations about rational quadratic spline see the original article by Durkan et al.

__call__(x, nunits)[source]

Returns a rational quadratic spline with learnable parameters.

Parameters
  • x (tensorflow.Tensor) – The spline input.

  • nunits (int) – Number of splines.

Returns

Rational quadratic spline with learnable parameters.

Return type

tensorflow_probability.bijectors.RationalQuadraticSpline

__init__(nbins=128, border=4, hidden_layers=[512, 512], min_bin_gap=0.001, min_slope=0.001)[source]

Constructor method.

class spqr.SplineBlock(*args, **kwargs)[source]

tf.keras layers block used for learning parameters (knots) of rational quadratic splines.

Inherits from :class: tensorflow.keras.layers.Layer.

Parameters
  • nunits (int) – Number of splines.

  • nbins (int) – Number of bins for each spline. Note that the total number of spline parameters is 3*nbins - 1: nbins for x and y bin coordinates respectively and nbins - 1 for slopes.

  • border (float) – The border of the splines. Spline bins are defined in the interval [-border, border], outside the relation between x and y is y=x.

  • hidden_layers – Dimensions of each dense layer, defaults to [512, 512].

  • min_bin_gap (float, optional) – Minimum distance between subsequent bins, defaults to 1e-3.

  • min_slope (float, optional) – Mimimum spline slope in each bin, defaults to 1e-3.

__init__(nunits, nbins, border, hidden_layers=[512, 512], min_bin_gap=0.001, min_slope=0.001)[source]

Constructor method.

call(units)[source]

Returns the units tensor transformed by the neural network.

Parameters

units (tensorflow.Tensor) – Input tensor.

Returns

One tensor for bin x coordinates (widths), one for y coordinates (heights) and one for slopes.

Return type

tensorflow.Tensor