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.

Convergence and stopping

Each RBIG layer removes exactly the marginal divergence it Gaussianizes away, so the per-layer reduction tells the loop when to stop

01 · Convergence and stopping

Open In Colab

Notebook 00 ran a fixed thirty layers. How many does a dataset need, and how can the loop tell from the data that it is done? Laparra et al. Laparra et al., 2011 answer with an identity: each layer lowers the KL divergence from N(0,I)\mathcal{N}(\vect{0}, \mat{I}) by exactly the divergence its marginal step removes, so the per-layer reduction is a convergence signal, and the reductions add up to the divergence of the data. We check the identity on a dataset where the answer is known exactly, build a stopping rule on it, and compare that rule with the one in rbig.

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

numpy     : 2.4.3
scipy     : 1.17.1
matplotlib: 3.11.2
rbig      : 0.1.16

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. A dataset with a known answer

To test a convergence signal we need a dataset whose distance from a standard normal is known. We bend a standard-normal cloud w∼N(0,I)\vect{w} \sim \mathcal{N}(\vect{0}, \mat{I}) into a banana with a shear of strength β\beta,

x=(w1,w2+β(w12−1))\vect{x} = (w_1, w_2 + \beta (w_1^2 - 1))

The shear’s Jacobian is triangular with ones on the diagonal, so its log-determinant is zero and the banana has the entropy of the cloud it came from, H[px]=log⁡(2πe)\mathbb{H}[p_x] = \log(2 \pi e). Its KL divergence from N(0,I)\mathcal{N}(\vect{0}, \mat{I}) is then the expected squared norm it gained, 12E[∥x∥2−∥w∥2]=12β2E[(w12−1)2]\frac{1}{2} \mathbb{E}[\Vert \vect{x} \Vert^2 - \Vert \vect{w} \Vert^2] = \frac{1}{2} \beta^2 \mathbb{E}[(w_1^2 - 1)^2], which is

KL[px∥N(0,I)]=β2\mathrm{KL}[p_x \Vert \mathcal{N}(\vect{0}, \mat{I})] = \beta^2

exactly. We take β=0.7\beta = 0.7, so the loop has 0.49 nats to remove.

The total correlation (5) needs one more number. The first coordinate is standard normal and the joint entropy is log⁡(2πe)\log(2 \pi e), so TC[px]=H[px2]−12log⁡(2πe)\mathrm{TC}[p_x] = \mathbb{H}[p_{x_2}] - \frac{1}{2} \log(2 \pi e), and the density of x2x_2 is a one-dimensional integral over w1w_1, which we evaluate on a grid.

From the density follow the entropy of x2x_2, the total correlation and the KL divergence of the second marginal; by (3) the last two must add up to β2\beta^2.

TC 0.2852, marginal KL 0.2048, sum 0.4900, beta^2 0.4900

Of the 0.49 nats, 0.285 are dependence between the coordinates and 0.205 are the non-Gaussian second marginal. Before running the loop we try the direct estimate of the total correlation in rbig.

rbig.total_correlation -0.0590 nats, exact +0.2852

It returns a negative number for a quantity that cannot be negative. rbig.total_correlation estimates the joint entropy by fitting a Gaussian to the data, and a Gaussian has the largest entropy at a given covariance (1), so the estimate is too low by the joint negentropy of the banana. That is a bug in rbig (rbig#160). RBIG itself never needs the joint entropy of the raw data, as the next section shows.

2. What one layer removes

The KL divergence from N(0,I)\mathcal{N}(\vect{0}, \mat{I}) splits into the total correlation and one divergence per marginal (3). The marginal step of a layer sets every marginal term to zero and leaves the total correlation where it was (7), and the rotation keeps the total (8). So layer kk lowers the divergence by exactly

Δk=∑d=1DxKL[pxk−1,d∥N(0,1)]\Delta_k = \sum_{d=1}^{D_x} \mathrm{KL}[p_{x_{k-1,d}} \Vert \mathcal{N}(0, 1)]

where xk−1,dx_{k-1,d} is coordinate dd of the state that layer kk receives. Every Δk\Delta_k is non-negative, so the divergence never rises, and the reductions add up to the divergence of the data,

KL[px∥N(0,I)]=∑k=1KΔk+KL[pxK∥N(0,I)]\mathrm{KL}[p_x \Vert \mathcal{N}(\vect{0}, \mat{I})] = \sum_{k=1}^{K} \Delta_k + \mathrm{KL}[p_{\vect{x}_K} \Vert \mathcal{N}(\vect{0}, \mat{I})]

The first marginal step removes only the marginal terms of the data, so the total correlation is what the later layers remove,

TC[px]=∑k=2KΔk+KL[pxK∥N(0,I)]\mathrm{TC}[p_x] = \sum_{k=2}^{K} \Delta_k + \mathrm{KL}[p_{\vect{x}_K} \Vert \mathcal{N}(\vect{0}, \mat{I})]

Each term of Δk\Delta_k is a one-dimensional divergence, which rbig.kl_to_standard_normal estimates from a kernel density by quadrature.

One layer is the marginal step and a rotation, either PCA or a fresh random rotation, as in notebook 00.

The loop records Δk\Delta_k and then applies layer kk.

A layer applied to data that are already standard normal removes nothing, so whatever Δk\Delta_k the estimator reports there is noise. We run eight random-rotation layers on five standard-normal datasets of the same size and take the 95% quantile of Δk\Delta_k from the second layer on as the floor Δ⋆\Delta_\star.

Delta_k on standard-normal data: mean 0.0026, 95% quantile 0.0047

A reduction below Δ⋆\Delta_\star is indistinguishable from no reduction at all. We run thirty layers with PCA rotations and thirty with random rotations on the banana and compare the running sums with the exact divergence and total correlation.

PCA    Delta_1 0.211, sum after 5, 10, 30 layers ['0.443', '0.456', '0.481'], sum from layer 2 0.270
random Delta_1 0.211, sum after 5, 10, 30 layers ['0.388', '0.435', '0.499'], sum from layer 2 0.288
<Figure size 1200x420 with 2 Axes>

Figure 1:Left: the reduction Δ_k each layer makes, with PCA and with random rotations, on a log scale; the band is the estimator’s noise floor, below its 95% quantile Δ⋆. Right: the running sum of the reductions against the exact divergence β² = 0.49 nats. PCA’s reductions fall steadily into the floor; random rotations remove less per layer, unevenly, and need more layers to reach it.

The identity holds to the accuracy of the estimator. The first reduction, 0.211 nats, is the divergence of the raw marginals, against an exact 0.205, and the sums of the later reductions estimate the total correlation at 0.270 (PCA) and 0.288 (random), against an exact 0.285. After thirty layers the running sums reach 0.481 and 0.499 nats against 0.49. That is as close as the estimator allows: each of the thirty reductions carries up to Δ⋆≈0.005\Delta_\star \approx 0.005 nats of noise. The late layers still remove real divergence, but less per layer than the estimator can tell from nothing.

3. A stopping rule

We stop at the first layer where the reduction has been below the floor Δ⋆\Delta_\star for three consecutive layers, the last three layers having removed nothing the estimator can detect.

We read off the depth and the two sums at that depth.

PCA    stops after  9 layers: KL 0.454 (exact 0.490), TC 0.243 (exact 0.285)
random stops after 15 layers: KL 0.467 (exact 0.490), TC 0.256 (exact 0.285)

The rule needs no layer count from us. It stops the PCA loop after 9 layers, with 93% of the divergence removed, and the random-rotation loop after 15, with 95%, because a random rotation keeps exposing structure the earlier ones missed and its reductions keep jumping above the floor. What is left, about 0.02 to 0.04 nats, is the residual term of (4): real divergence, spread over many later layers that each remove less than the floor. Stopping trades that residual for depth. After thirty layers both latents are round clouds.

<Figure size 1350x430 with 3 Axes>

Figure 2:The banana (left) and its latent after thirty RBIG layers with PCA rotations (centre) and random rotations (right). Both loops have reached a round cloud; the reductions of the last layers are inside the estimator’s noise floor.

4. The stopping rule in rbig

rbig.AnnealedRBIG runs the same loop and stops when its record of the total correlation, tc_per_layer_, has changed by less than tol for patience consecutive layers. It records the direct estimate of section 1 after every layer, and we look at the first few entries.

layers 20, tc_per_layer_[:4] [-0.059, -0.1373, -0.0785, -0.0313]

Every entry is negative. Once a PCA rotation has decorrelated the state, a Gaussian fit to it has the summed entropies of Gaussian marginals, so the direct estimate becomes minus the summed marginal negentropies (3), a measure of how far the marginals still are from Gaussian rather than of dependence. It still falls to zero as the loop converges, so it works as a stopping signal, but its values are not total correlations. The total correlation AnnealedRBIG reports is the first entry minus the last.

AnnealedRBIG.total_correlation_reduction -0.0593 nats, exact +0.2852

It inherits the wrong sign of the first entry, which is the direct estimate on the raw data; that is a bug in rbig (rbig#162). The sum of reductions (5) never estimates a joint entropy, which is why it got the total correlation right in section 2. The depth AnnealedRBIG picks depends on tol, and on how noisy its signal is for each rotation.

We sweep tol over three decades for both rotations.

pca    depth for tol 1e-2, 1e-3, 1e-4: [15, 17, 20]
random depth for tol 1e-2, 1e-3, 1e-4: [22, 37, 40]

Each tenfold tighter tol adds layers, and with random rotations at 10-4 the loop runs to the cap of 40: each new rotation moves the record by more than 10-4 even when the state is close to Gaussian. A tolerance only means something next to the noise of the signal it is compared with, which is why our rule uses the measured floor Δ⋆\Delta_\star.

Finally, AnnealedRBIG estimates the entropy of the data as minus the mean log-density of its training points. The banana’s entropy is exact, log⁡(2πe)\log(2 \pi e).

AnnealedRBIG.entropy 2.8426 nats, exact 2.8379

The two agree to 0.005 nats. The entropy comes from the stack’s log-determinants and never needs a Gaussian fit to non-Gaussian data.

Recap

PieceEquationIn code
a banana with a known divergence(1), (2)samples
what layer kk removes(3)rbig.kl_to_standard_normal(points).sum()
the reductions add up to the divergence(4)trace.cumsum()
the total correlation, the RBIG way(5)trace[1:].sum()
the stopping ruleΔk<Δ⋆\Delta_k < \Delta_\star three times in a rowstopping_depth
rbig’s stopping rulechange in tc_per_layer_ below tolAnnealedRBIG(tol=..., patience=...)

Next. PCA and random rotations converged here at different speeds. The next notebook compares the rotations on data where the choice matters.

References
  1. Laparra, V., Camps-Valls, G., & Malo, J. (2011). Iterative Gaussianization: From ICA to Random Rotations. IEEE Transactions on Neural Networks, 22(4), 537–549. 10.1109/TNN.2011.2106511