Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The 1×1 convolution and ActNorm

Two linear layers that rescale, with log-determinants read off a triangular factor or a diagonal

03 · The 1×1 convolution and ActNorm

Open In Colab

Notebooks 00_rotation_zoo, 01_householder_orthogonal and 02_fixed_pca_warmstart mix the coordinates with rotations, which preserve volume, so their log-determinant is zero (2). Glow Kingma & Dhariwal, 2018 added two linear layers that rescale as well, so they carry a real log-determinant, but each is stored so that the log-determinant costs O(Dx)\mathcal{O}(D_x) rather than the O(Dx3)\mathcal{O}(D_x^3) of a dense determinant:

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.12.0

jax             : 0.12.1
numpy           : 2.4.3
flowjax         : 19.1.0
pipekit_train   : 0.3.4
gauss_flows_maps: 0.3.0

Compiler    : Clang 21.1.4 
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

1. The invertible 1×1 convolution

An image with PP positions holds a vector of channels at each position, and a 1×1 convolution applies the same matrix to every one of them,

zp=Axp\vect{z}_p = \mat{A} \vect{x}_p

where xp∈RDx\vect{x}_p \in \mathbb{R}^{D_x} is the channel vector at position p=1,…,Pp = 1, \ldots, P. It generalises the fixed permutation of the channels that RealNVP placed between coupling layers Dinh et al., 2017: instead of a hard shuffle, a learned mixing. Since the same matrix acts at every position, the Jacobian of the whole image is block diagonal with PP copies of A\mat{A}, and

log⁡∣det⁡Jf(x)∣=Plog⁡∣det⁡A∣\log \vert \det \mat{J}_f(\vect{x}) \vert = P \log \vert \det \mat{A} \vert

Invertible1x1Conv stores A\mat{A} as the product of a unit lower-triangular L\mat{L} and an upper-triangular U\mat{U} with a positive diagonal,

A=LU\mat{A} = \mat{L} \mat{U}

so the determinant is the product of the diagonal of U\mat{U},

log⁡∣det⁡A∣=∑d=1Dxlog⁡Udd\log \vert \det \mat{A} \vert = \sum_{d=1}^{D_x} \log U_{dd}

which costs O(Dx)\mathcal{O}(D_x). The layer keeps log⁡Udd\log U_{dd} as its free parameter log_diag_u, and the off-diagonal entries of both factors as lower_off and upper_off. A new layer starts near the identity, so we set all three by hand to get a matrix worth looking at.

The header of the cell above declares off_diagonal, the Dx(Dx−1)/2=6D_x (D_x - 1) / 2 = 6 entries strictly below or above a diagonal. We put them into a fresh layer.

We read A\mat{A} back from the layer, as the images of the basis vectors, and compare the layer’s log-determinant with (4) and with the dense determinant.

The three log-determinants and the round trip:

layer log_det     : +0.500000
sum of log U_dd   : +0.500000
dense log|det A|  : +0.500000
round trip error  : 1.1e-16

On an image the layer acts on each position’s channel vector, so we vmap it over the positions of an 8 × 8 image and add up the log-determinants.

sum over positions  : 32.0000
P log|det A|        : 32.0000

The layer’s log-determinant is (4) exactly, it agrees with the dense determinant, the inverse recovers the input to machine precision, and over the image it adds up to (2). Figure 1 shows the two factors and their product. Only the log-determinant is cheap: Invertible1x1Conv.inverse solves a dense linear system with A\mat{A}, which costs O(Dx3)\mathcal{O}(D_x^3).

<Figure size 1000x320 with 4 Axes>

Figure 1:The 1×1 convolution’s matrix A\mathbf{A} (right) is stored as a unit lower-triangular L\mathbf{L} (left) times an upper-triangular U\mathbf{U} with a positive diagonal (middle). Its log-determinant is the sum of the log-diagonal of U\mathbf{U}.

2. Which matrices the LU form reaches

Storing A\mat{A} as LU\mat{L} \mat{U} with a positive diagonal makes it invertible by construction, whatever the parameters: det⁡A=∏dUdd>0\det \mat{A} = \prod_d U_{dd} > 0, so no gradient step can make it singular. The price is that not every invertible matrix has this form. An LU factorisation without row exchanges and with positive pivots exists exactly when every leading principal minor, the determinant of the top-left k×kk \times k block, is positive. That rules out every matrix with det⁡A<0\det \mat{A} < 0, a reflection for instance, and also matrices such as the 90° rotation, whose top-left entry is zero.

We check three 2 × 2 matrices and the layer’s own.

90° rotation   minors [0. 1.], reachable: False
reflection     minors [ 1. -1.], reachable: False
30° rotation   minors [0.87  1.007], reachable: True
the layer's A  minors [1.649 1.221 1.492 1.649], reachable: True

Every matrix near the identity has positive leading minors, and a layer starts at the identity, so this limit rarely bites in training. Glow keeps a fixed permutation in front of the factors, A=ΠLU\mat{A} = \gmat{\Pi} \mat{L} \mat{U}, and lets the diagonal of U\mat{U} take either sign with a fixed sign pattern Kingma & Dhariwal, 2018; LULinearPermute in gauss_flows adds the permutation Π\gmat{\Pi} and keeps the diagonal positive.

3. Training keeps it invertible

We train the 1×1 convolution as a flow on its own, by maximum likelihood. The data are 2,000 vectors of four correlated coordinates, centred, so the best a linear flow with a standard normal base can do is to whiten them. Its negative log-likelihood is then the entropy of the Gaussian fit, 12log⁡det⁡(2πeΣ^x)\frac{1}{2} \log \det(2 \pi e \hat{\gmat{\Sigma}}_x), from (2).

The flow has a standard normal base and the 1×1 convolution as its map from data to latent, so flowjax’s Transformed takes its inverse as the map from latent to data.

We train on the whole data set as one batch, with Adam at a learning rate of 10-2 for 1,500 steps.

We run it and take the trained 1×1 convolution out of the flow.

We check the trained layer the same way as in section 1, and look at the covariance of its output.

final NLL 7.4155, Gaussian fit 7.4155
layer log_det -1.7398, dense -1.7398
det A = 0.1756, round trip 3.6e-15
covariance of z:
 [[ 1.  0.  0. -0.]
 [ 0.  1. -0. -0.]
 [ 0. -0.  1. -0.]
 [-0. -0. -0.  1.]]
<Figure size 600x350 with 1 Axes>

Figure 2:The negative log-likelihood of the 1×1 convolution over 1,500 Adam steps falls to the entropy of the Gaussian fit (dashed), the best any linear flow with a standard normal base can do.

The trained layer reaches the Gaussian fit to four decimals and whitens the data, its output covariance is the identity to three decimals, its log-determinant still equals the dense one, and det⁡A\det \mat{A} stays positive with an exact inverse. The LU form keeps the layer a valid bijection throughout training at no cost.

4. ActNorm

ActNorm is a per-channel shift and scale,

z=diag(s)−1(x−b)\vect{z} = \mathrm{diag}(\vect{s})^{-1} (\vect{x} - \vect{b})

with a location b\vect{b} and a positive scale s\vect{s}. Its Jacobian is diagonal, so

log⁡∣det⁡Jf(x)∣=−∑d=1Dxlog⁡sd\log \vert \det \mat{J}_f(\vect{x}) \vert = -\sum_{d=1}^{D_x} \log s_d

times PP on an image, where the same s\vect{s} acts at every position. gauss_flows keeps each sd=10−5+softplus(⋅)s_d = 10^{-5} + \mathrm{softplus}(\cdot) positive through a Reparametrised leaf, starting at 10−5+log⁡2≈0.69310^{-5} + \log 2 \approx 0.693 with b=0\vect{b} = \vect{0}.

What gives ActNorm its name is the data-dependent initialisation Kingma & Dhariwal, 2018: before training, set b\vect{b} to the mean of a batch and each sds_d to its standard deviation,

sd2=1N∑n=1N(xn,d−bd)2s_d^2 = \frac{1}{N} \sum_{n=1}^{N} (x_{n,d} - b_d)^2

so the layer’s output starts with zero mean and unit variance in every channel. ActNorm has no from_data constructor yet (gauss_flows#112), so we write one.

We test it on channels with very different offsets and scales, and compare the starting negative log-likelihood of a flow with a default ActNorm and with a data-initialised one.

The best a per-channel shift and scale can do is the diagonal Gaussian fit, whose negative log-likelihood is 12∑dlog⁡(2πesd2)\frac{1}{2} \sum_d \log(2 \pi e s_d^2) with sds_d from (7).

output mean [-0. -0.  0.  0.]
output std  [1. 1. 1. 1.]
NLL with a default ActNorm        : 48.7159
NLL with a data-initialised one   : 6.7398
NLL of the diagonal Gaussian fit  : 6.7398
<Figure size 1100x380 with 2 Axes>

Figure 3:Left: four channels with offsets 5, −2, 0 and 1 and scales 3, 0.5, 2 and 1. Right: the same channels after a data-initialised ActNorm, each with zero mean and unit variance.

The data-initialised layer starts at the diagonal Gaussian fit to every digit printed, about 6.74 nats against 48.7 for the default, whose single scale of 0.693 ignores the offsets and the scales of the channels. Glow used this initialisation so that every layer of a deep stack starts with well-scaled inputs Kingma & Dhariwal, 2018; with it, training begins from the best per-channel affine fit rather than far from it.

5. How the layers fit together

A step of Glow chains ActNorm, a 1×1 convolution and a coupling layer, and their log-determinants add (4). The rotations of notebooks 00_rotation_zoo to 02_fixed_pca_warmstart can take the place of the 1×1 convolution: the choice is between a map that only rotates and costs nothing, and one that also rescales and costs the sum of a triangular diagonal.

Recap

LayerEquationLog-determinantKept valid byIn code
rotation(1)0, (2)an orthogonal parametrisationHouseholderRotation, OrthogonalRotation, FixedRotation
1×1 convolution(1), (3)P∑dlog⁡UddP \sum_d \log U_{dd}, (2), (4)the LU form, positive leading minorsInvertible1x1Conv, LULinearPermute
ActNorm(5)−P∑dlog⁡sd-P \sum_d \log s_d, (6)a positive scale, data initialisation (7)ActNorm, ActNorm1D

Next. With marginal maps from Part 1 and the linear layers of Part 2, Part 3 assembles the classic algorithm that alternates them until the data are Gaussian: Rotation-Based Iterative Gaussianization, the loop of marginal passes and rotations whose convergence 00_rotation_zoo previewed.

References
  1. Kingma, D. P., & Dhariwal, P. (2018). Glow: Generative Flow with Invertible 1x1 Convolutions. Advances in Neural Information Processing Systems (NeurIPS).
  2. Dinh, L., Sohl-Dickstein, J., & Bengio, S. (2017). Density Estimation using Real NVP. International Conference on Learning Representations (ICLR).