Flowiﬁcation: Everything is a Normalizing Flow
Abstract
We develop a method that can be used to calculate the likelihood contribution of linear and convolutional layers allowing multi-layer perceptrons and convolutional networks to be converted into normalizing ﬂows.
We term this process ﬂowiﬁcation .
In some cases ﬂowiﬁcation requires the addition of uncorrelated noise to the model but in the simplest case no additional parameters.
The technique we develop can be applied to a broad range of architectures, allowing them to be used for a wide range of tasks.
Our models also allow existing density estimation techniques to be combined with high performance feature extractors.
In contrast to standard density estimation techniques that require speciﬁc architectures and specialized knowledge, our approach can leverage design knowledge from different domains and is a step closer to the realization of general purpose architectures.
We investigate the efﬁcacy of linear and convolutional layers for the task of density estimation on standard datasets.
Introduction
Density estimation techniques have proven effective on a wide variety of downstream tasks such as sample generation and anomaly detection [1–6].
Normalizing ﬂows and autoregressive models perform very well at density estimation but do not easily scale to large dimensions [6–8] and have to satisfy strict design constraints to ensure efﬁcient computation of their Jacobians and inverses.
Advances in other areas of machine learning cannot be utilized as ﬂow architectures because they are not typically seen as being invertible; this restricts the application of highly optimized architectures from many domains to density estimation, and the use of the likelihood for diagnosing these architectures.
Methods using standard convolutions and residual layers for density estimation have been developed for architectures with speciﬁc properties [9–12].
These methods do not provide a recipe for converting general architectures into ﬂows.
There is no known correspondence between normalizing ﬂows and the operations deﬁned by linear and convolutional layers.
In this paper we show that a large proportion of machine learning models can be trained as normalizing ﬂows.
The forward pass of these models remains unchanged apart from the possible addition of uncorrelated noise.
To demonstrate our formulation works we apply it to fully connected layers, convolutions and residual connections.
The contributions of this paper include:
⇤ Equal contribution.
• In §3.1 we show that linear layers induce densities as augmented normalizing ﬂows [13] with the multi-scale architecture used in RealNVPs [4].
We also show how these layers can be viewed as funnels [14] to increase their expressivity.
We term this process ﬂowiﬁcation .
• In §3.2 we argue that most ML architectures can be decomposed into simple building blocks that are easy to ﬂowify.
As an example, we derive the speciﬁcs for two dimensional convolutional layers and residual blocks.
• In §4 we ﬂowify multi-layer perceptrons and convolutional networks and train them as normalizing ﬂows using the likelihood.
This demonstrates that models built from standard layers can be used for density estimation directly.
Background Normalizing ﬂows
Given a base probability density { p 0 ( z ) | z 2 Z } and a diffeomorphism f : X ! Z , the pullback along f induces a probability density { p ( x ) | x 2 X } on X , where the likelihood of any x 2 X is given by p ( x ) =
p 0 ( f ( x )) | det ( J fx ) | , where J fx is the Jacobian of f evaluated at x .
Thus, the log-likelihoods of the two densities are related by an additive term, which will be referred to as the likelihood contribution V ( x, z ) [15].
Normalizing ﬂows [1] parametrize a family f ✓ of invertible functions from X to Z .
The parameters ✓ are then optimized to maximize the likelihood of the training data.
A lot of development has gone into constructing ﬂexible invertible functions with easy to calculate Jacobians where both the forward and inverse passes are fast to compute [4–6, 16, 17].
As the function f must be invertible it is required to preserve the dimension of the data.
This limits the expressivity of f and makes it expensive to model high dimensional data distributions.
To reduce these issues several works have studied dimension altering variants of ﬂows [13, 15, 18–21].
Dimension altering generalizations of normalizing ﬂows
Reducing the dimensionality A simple method for altering the dimension of a ﬂow is to take the output of an intermediate layer z 0 and partition it into two pieces z 0 = { z 0 1 , z 0 2 } .
Multiscale architectures [4] match z 0 2 directly to a base density and apply further transformations z 0 1 .
Funnels [14] generalize this by allowing z 0 2 to depend on z 0 1 , i.e. they work with the model p ( z 0 )
=
p ( f 0 ( z 0 1 )) p ( z 0 2 | f 0 ( z 1 ))
| det J f 0 z 0 1 | where the conditional distribution p ( z 0 2 | f 0 ( z 1 )) is trainable.
It is useful to think of these factorization schemes as dimension reducing mechanisms from dim( z 0 ) to dim( z 0 1 ) .
Increasing the dimensionality Dimension increasing ﬂow layers can improve a models ﬂexibility, as demonstrated by augmented normalizing ﬂows [13].
To increase the dimensionality, x is embedded into a larger dimensional space and data independent noise u is added to the embedding to obtain a distribution with support of nonzero measure.
This noise addition x 7! ( x, u ) is similar to dequantization [4, 22], but is orthogonal to the distribution of x and increases its dimension from dim x to dim x + dim u .
Under an augmentation the likelihood of x can be estimated using
[Equation] [Equation]
In practice, we estimate this expectation value by sampling u everytime a datapoint is passed through the network.
This means the integral is estimated with a single sample as in surVAEs [15].
Flowiﬁcation
Suppose A is a network architecture with parameter space ⇥ .
Then for any choice of ✓ 2 ⇥ the network with parameters ✓ realizes a function A ✓ : R D ! R C for some D and C .
Similarly, a normalizing ﬂow model F is a parametric distribution on some R E , where for any choice of  from their parameter space  they deﬁne a density function F  on R E .
In this work we show that a large class of neural network architectures can be thought of as a ﬂow model by constructing a map n network architectures o !
n ﬂowmodels o .
The embedding of A to its ﬂowiﬁcation F A results in a ﬂow model that can realize density functions on the augmented space R D ⇥ R N for some N  0 , which in turn induces a density on R D by integrating out the component on R N .
The parameter space of F A factorises as ⇥ ⇥  where ⇥ is the parameter space of A and also that of the forward pass of F A , while  parametrises the inverse pass of F A .
In the simplest case  = ; , i.e. ﬂowiﬁcation does not require additional parameters.
It is in this sense that we claim that a large fraction of machine learning models are normalizing ﬂows.
Terminology In what follows we work with conditional distributions such as p ( z | x ) and it will be practical to think of them as “stochastic functions” p : x 7! z , that take an input x and produce an output z ⇠ p ( z | x ) .
Conversely, we think of a function f : x 7! z as the Dirac  -distribution f
( z | x ) =
( z  f ( x )) .
These deﬁnitions allow us to have a uniﬁed notation for deterministic and stochastic functions such that we can talk about them in the same language.
Consequently, when we say "stochastic function", it will include deterministic functions as a corner case.
Depending on whether f and f  1 are deterministic or stochastic, we talk about left, right or two-sided inverses.
We will be careful to be precise about this.
Method In the following we consider the standard building blocks of machine learning architectures and enrich them by deﬁning (stochastic-)inverse functions and calculating the likelihood contribution of each layer.
Treating each layer separately allows density estimation models to be built through composition [15].
The stochastic inverse can use the funnel approach, which increases the parameter count, or the multi-scale approach, which does not.
For simplicity we will only consider conditional densities in the inverse as this is more general, though it is not required.
We will refer to this process as ﬂowiﬁcation and the enriched layers as ﬂowiﬁed ; non-ﬂowiﬁed layers will be called standard layers .
Flowiﬁed layers can then be seen as simultaneously being
• Flow layers that are invertible, their likelihood contribution is known and therefore can be used to train the model to maximize the likelihood.
• Standard layers that can be trained with losses other than the likelihood, but for which the likelihood can be calculated after this training with ﬁxed weights in the forward direction.
3.1 Linear Layers
Let L W,b : R n ! R m denote the the linear layer of a neural network with parameters deﬁned by a weight matrix W 2 R m ⇥ n and bias b 2 R m .
Formally, L W,b is deﬁned as the afﬁne function
[Equation]
Deﬁnition 1.
Let  ( z | x ) : R n ! R m be a stochastic function.
We say that  is linear in expectation if there exists W 2 R m ⇥ n and b 2 R m such that for any x 2 R n the expected value of  coincides with the application of L W,b
[Equation]
[Equation] Similarly, we say that a stochastic function ( z | x ) is convolutional in expectation if the deterministic function x 7! E z ⇠ ( z | x ) [ z ] is a convolutional layer.
In this section we ﬂowify linear layers, by which we mean we construct a pair of stochastic functions, a forward L ( z | x ) : R n !
R m and an inverse L  1 ( x | z ) : R m ! R n such that the forward is linear in expectation and is compatible with the inverse in a way that will be made precise in the following paragraphs.
SVD parametrization
To build a ﬂowiﬁed linear layer, the ﬁrst step is to parametrize the weight matrices by the singular value decomposition (SVD)[23].
This involves writing W 2 R m ⇥ n as a product W = V ⌃ U , where
U 2 R n ⇥ n is orthogonal, ⌃ 2 R m ⇥ n is diagonal and V {+
+}
R m ⇥ m is orthogonal.
This parametrization is particularly useful for our purposes because the orthogonal transformations are easily invertible and do not contribute to the likelihood, and the non-invertible piece of the transformation is localized to ⌃ .
Parametrizing U and V
We generate elements of the special orthogonal group SO ( d ) by applying the matrix-exponential to elements of the Lie algebra so ( d ) of skew-symmetric matrices.
We parametrize so ( d ) and perform gradient descent there.
As the Lie-algebra is a vector space, this is signiﬁcantly easier than working directly with SO ( d ) .
See Appendix G for details.
Parametrizing ⌃
The matrix ⌃ is of shape m ⇥ n containing the singular values on the main diagonal.
We ensure maximal rank of ⌃ , by parameterizing the logarithm of the main diagonal, this way all singular values are greater than 0.
It is important to note that this parametrization is not without loss of generality.
In particular, it does not include matrices of non-maximal rank nor orientation reversing ones, where either
U 2 O ( n ) \ SO ( n ) or V 2 O ( m ) \ SO ( m ) .
This implementation detail does not change the general perspective we provide of linear layers as normalizing ﬂows, but instead simpliﬁes the implementation of ﬂowiﬁed layers.
Reducing the dimensionality
Deﬁnition 2.
We call the tuple ( L ( z | x ) , L  1 ( x | z )) a dimension decreasing ﬂowiﬁed linear layer if
L is dimension decreasing, linear in expectation and the following conditions are satisﬁed
(i)
The forward is deterministic, given by L ( z | x ) =
L W,b ( x ) , (ii)
The layer is right-invertible, L  L  1 = id z , (iii)
The likelihood contribution of L can be exactly computed.
To ﬂowify dimension decreasing linear layers, we deﬁne the forward function L as a standard linear layer with parameters W and b ,
[Equation]
Since W is parametrized by the SVD decomposition, W = V ⌃ U , we need to invert V, U and ⌃ separately.
As V and U are rotations, they are invertible in the usual sense.
To construct a stochastic inverse to ⌃ , we think of it as a funnel [14] and use a neural network p inv (( Ux ) ( m :) | ⌃ Ux ) that models the n  m dropped coordinates as a function of the m non-dropped coordinates.
Again, this is not required to calculate the likelihood under the model, even a ﬁxed distribution could be used, but introducing some trainable parameters signiﬁcantly improves the performance of the ﬂow that is deﬁned by the layer.
We use ⌃  1 to denote this stochastic inverse to ⌃ .
The stochastic inverse function L  1 can then be written as
[Equation]
Since the rotations don’t contribute to the log-likelihood, the likelihood of data under a dimension decreasing ﬂowiﬁed linear layer is
[Equation]
where log ⌃ denotes the sum of the logarithms of the diagonal elements of ⌃ .
Theorem 3.
The above choices for L and L  1 deﬁne a dimension decreasing ﬂowiﬁed linear layer.
Sketch of proof.
The deﬁnition of the forward pass (9) makes the forward pass linear in expectation and satisﬁes (i) by deﬁnition.
Unpacking the deﬁnitions and decomposing W into its SVD form yields right-invertibility (ii) which in turn implies that the likelihood contribution can be exactly computed (iii).
When the inverse density is not made to be conditional the above ideas can be visualized as a standard multi-scale ﬂow architecture [4] as shown in Fig.
[Figure]
Increasing the dimensionality
Deﬁnition 4.
We deﬁne the Moore-Penrose pseudoinverse L + W,b of a linear layer L W,b : R n ! R m as the afﬁne transformation R m !
R n [Equation] [Equation]
where W + denotes the Moore-Penrose pseudoinverse of the matrix W .
Deﬁnition 5.
We call the tuple ( L ( z | x ) , L  1 ( x | z ))
a dimension increasing ﬂowiﬁed linear layer if
L is dimension increasing, linear in expectation and the following conditions are satisﬁed
(iv) The inverse L  1 is deterministic, given by L  1 ( x | z )
=
L + W,b ( z ) , (v) The layer is left-invertible, L  1  L = id x , (vi) The likelihood contribution of L can be bounded from below.
To construct dimension increasing ﬂowiﬁed linear layers, we rely again on the SVD parametrization where the only nontrivial component is ⌃ .
In this case ⌃ is a dimension increasing operation and we think of it as an augmentation step [13] composed with diagonal scaling.
To augment, we sample m  n coordinates from a distribution p ( u ) with zero mean and then apply a scaling in m dimensions.
The likelihood contribution is then given by
[Equation]
where log ⌃ denotes the sum of the logarithms of the m scaling parameters.
The inverse function
L  1 is the composition of the inverse rotations, the inverse scaling and the dropping of the sampled coordinates.
This sequence of steps is visualized in Fig.
[Figure]
Theorem 6.
The above choices for L and L  1 deﬁne a dimension increasing ﬂowiﬁed linear layer.
Sketch of proof.
The augmentation step [13] results in a lower bound on the likelihood contribution, implying (vi).
Since E u ⇠ p ( u ) = 0 , the augmentation does not inﬂuence the expected value of z , i.e. the forward pass is linear in expectation.
Simple calculations using the SVD decomposition then imply both (iv) and (v).
Preserving the dimensionality
Dimension preserving layers are a corner case of both of the above scenarios, where padding and sampling are not needed in either direction and the layer is non-stochastically invertible.
All this implies (i),(ii),(iii),(iv) and (v) are satisﬁed.
3.2 Convolutional layers
Convolutions can be seen as a dimension increasing coordinate repetition followed by a matrix multiplication with weight sharing.
In the previous section we derived the speciﬁcs of matrix multiplication.
We begin this section with the details of coordinate repetition after which we put the pieces together to build a ﬂowiﬁed convolutional layer.
In Appendix H we describe an alternative approach relying on the Fourier transform.
Repeating coordinates In this paragraph we focus on the N -fold repetition of a single scalar coordinate x .
This signiﬁcantly simpliﬁes notation, but the technique generalizes in an obvious way.
Intuitively, the idea is to expand the one dimensional volume to N dimensions by ﬁrst embedding and then increasing the volume of the embedding such that the volume in the N  1 directions complementary to the embedding can be controlled.
We have seen in §2 that the operation
[Equation]
has likelihood contribution E u [  log p ( u )] .
Now, we can apply any N -dimensional rotation R N which maps (1 , 0 ) to 1 p N (1 , 1 ) to obtain
[Equation]
Note that this rotation does not contribute to the likelihood.
Finally, we apply a diagonal scaling in N dimensions with factor p N such that
[Equation]
where the ﬁnal scaling has likelihood contribution N log( p N ) =
( N/ 2) log N and x is now repeated
N times.
The overall contribution to the likelihood of the embedding (17) is
[Equation]
By construction, the padding distribution R N (0 , p N u ) is orthogonal to the diagonal embedding x 7!
( x, ..., x ) of the data distribution.
The inverse function is given by the projection to the diagonal embedding,
[Equation]
General architectures Now that the likelihood contribution of arbitrary linear layers and coordinate repetition has been computed it is possible to ﬂowify more general architectures such as convolutions and residual connections.
It is important to note that just because an architecture works well for certain tasks, it is not clear if its ﬂowiﬁed version will perform well at density estimation.
Decomposing convolutional layers To ﬂowify convolutional layers, we decompose it as a sequence of building blocks that are easy to ﬂowify separately.
A standard convolutional layer performs the following sequence of steps:
Padding of the input image with zeros to increase its size.
Unfolding of the padded image into tiles.
This step replicates the data according to the kernel size and stride.
Applying a linear layer.
Finally, we apply the same linear layer to each of the tiles produced in the previous step.
The outputs then correspond to the pixels of the output image.
Flowiﬁcation Steps 1 and 3 are already ﬂowiﬁed, i.e. their likelihood contribution is computed and an inverse is constructed, in §3.1.
We denote their ﬂowiﬁcation with Pad and Linear , respectively.
Step 2 ﬁts into the discussion of the previous paragraph of repeating coordinates, where both its inverse (19) and its likelihood contribution (18) are given.
We will denote this operation by Unfold .
Deﬁnition 7.
Let Linear , Unfold and Pad be as above and deﬁne C and C  1 be the following stochastic func- tions
[Equation]
call the resulting layer ( C , C  1 ) a ﬂowiﬁed convolutional layer .
Augment
SVD SVD
A ﬂowiﬁed convolutional layer ( C , C  1 ) is then convolutional in expectation (Deﬁnition 1), i.e. there exists a convolutional layer C ✓ with parameters ✓ such that
[Equation]
The ﬂowiﬁcation of a convolution without padding can be seen in Fig.
The Unfold operation is implemented as coordinate duplication and Linear is a ﬂowiﬁed linear layer parameterized by the
SVD.
Activation functions Functions that are surjective onto R and invertible ﬁt well in our framework as they can be used out of the box without any modiﬁcations.
In our experiments we use LeakyReLU and rational-quadratic splines [5] as activations functions.
Non-invertible activations can also be used when equipped with additional densities [15].
Residual connections Residual connections can be seen as coordinate duplication followed by two separate computational graphs { f 1 , f 2 } with the outputs recombined in a sum.
The sum can be inverted by deﬁning a density over one of the summands p ( f 1 ( x + u ) | f 1 ( x + u ) , f 2 ( x  u )) and sampling from this density, which will also deﬁne the likelihood contribution.
Then, if the likelihood contribution can be calculated for each individual computational graph, the likelihood of the total operation can be calculated [15].
Experiments
To test the constructions described in the previous section we ﬂowify multilayer perceptrons and convolutional architectures and train them to maximize the likelihood of different datasets.
Tabular data In this section we study a selection of UCI datasets [24] and the BSDS 300 collection of natural images [25] using the preprocessed dataset used by masked autoregressive ﬂows [17, 26].
We compare the performance with several baselines for comparison in Table 1.
We see that the ﬂowiﬁed models have the right order of magnitude for the likelihood but are not competitive.
[Table]
Image Data
In this section we use the MNIST [27] and CIFAR10 [28] datasets with the standard training and test splits.
The data is uniformly dequantized as required to train on image data [29, 30].
For both datasets we trained networks consisting only of ﬂowiﬁed linear layers ( F MLP) and also networks consisting of convolutional layers followed by dense layers ( F C ONV 1).
To minimize the number of augmentation steps that occur in each model we deﬁne additional architectures with similar numbers of parameters but with non-overlapping kernels in the convolutional layers ( F C ONV 2).
The exact architectures can be found in Appendix F.1.
The ﬂowiﬁed layers sample from N (0 , a ) for dimension increasing operations, where a is a per-layer trainable parameter.
We use rational quadratic splines [5] with 8 knots and a tail bound of 2 as activation functions, where the same function is applied per output node.
We also ran experiments with coupling layers using rational quadratic splines [5] mixed into F C ONV 2, in all other cases the parameters of the model are not data-dependent operations.
The improved performance of these models suggests that ﬂowiﬁed layers do not mismodel the density of the data, but they do lack the capacity to model it well.
Samples from these models are shown in Appendix C.
[Table]
The results of the density modelling can be seen in Table.
The images in the left column of Fig.are generated by sampling from a standard gaussian in the latent space and taking the expected output of the inverse in every layer.
The images in the right column use the same latent samples as the left column but also sample from the distribution deﬁned by the inverse pass of the layers.
As seen in Fig.
2 the F MLP models are outperformed by the F C ONV models and the convolutional models with non-overlapping kernels achieve better results than the ones with overlapping kernels.
This suggests both that the inductive bias of the convolution is useful for modelling distributions of images and that the augmentation step costs more in terms of likelihood than it provides in terms of increased expressivity.
Related Work
Several works have developed methods that allow standard layers to be made invertible, but these approaches restrict the space of the models, whereas we consider networks in their full generality.
Invertible ResNets [9, 10] require that each residual block has a Lipschitz constant less than one, but even with this restriction they attain competitive results on both classiﬁcation and density estimation.
The same Lipschitz constraint can also be applied to other networks [11].
In these architectures the multi-scale architecture used in RealNVPs [4] is not leveraged, and so no information is discarded by the model.
It is unclear why this approach outperforms ﬂowiﬁed layers, as seen in Table.
2, but it could be due to the preservation of information through the model, the very large number of parameters that are used in these approaches, the restricted subspace of the models, or some combination of these three.
It can also be shown that convolutions with the same number of input and output channels can be made invertible [12].
These layers perform poorly at the task of density estimation and are outperformed by ﬂowiﬁed layers.
This is likely due to the increased expressivity that comes from considering a larger space of architectures.
There have been several works that develop convolution inspired invertible transformations [31–33], but these architectures consider restricted transformations to maintain invertibility.
Future Work and Conlusion
Our experiments suggest that ﬂowiﬁed convolutional networks do not match the density estimation performance of similarly sized normalizing ﬂows.
A possible explanation is that the dimension reducing steps discard information and more expressive encoding layers are necessary to transform the distributions before reducing the dimensionality.
This is supported by the experiments using NSF layers in-between the ﬂowiﬁed layers (see App. C).
The addition of NSF layers leads to improved performance both in terms of visual quality and also BPD values.
Possibly the main limitation of a network consisting purely of ﬂowiﬁed layers is the fact that, unlike ﬂow layers, the forward passes of standard linear and convolutional layers are not data-dependent.
This is reinforced by the fact that the entanglement capability typically used in ﬂows also appears in attention mechanisms, which have been shown to excel at capturing complex statistical structures [34–36].
With further development – such as increased capacity given to the inverse density, or data dependent parameters in the forward pass – standard architectures could become competitive density estimators in their own right and allow for general purpose models to be developed.
The focus of this work was on employing standard layers for density estimation, but it is possible that designing data dependent variants of standard layers that are more ﬂow-like could improve their performance on tasks such as classiﬁcation and regression.
The ﬂowiﬁcation procedure provides a useful means for designing such models and demonstrates that standard architectures can be considered a subset of normalizing ﬂows, a correspondence that has not previously been demonstrated
The code for reproducing our experiments is available under
MIT license at https://github.com/balintmate/flowification .
Acknowledgement
The authors would like to acknowledge funding through the SNSF Sinergia grant called Robust Deep
Density Models for High-Energy Particle Physics and Solar Flare Analysis (RODEM) with funding number CRSII 5 _
[-Fold and Unfold operations of PyTorch and its essence is summarised in the following piece of pseudocode, 1 #Let X be the input image 
-]
[-in_channels , out_channels = 4, signal_length , kernel_size = 32, batch_size =
-]
[-z2 = z
-]