A planar layer moves every point along one direction v by an amount that depends only on its position along w: f(u) = u + v tanh(w·u + b). Its Jacobian determinant is 1 + (w·v) sech2(w·u + b), cheap by the Weinstein–Aronszajn identity. The inverse needs only a one-dimensional root: α + (w·v) tanh(α + b) = w·x, which has exactly one solution when w·v > −1. Below −1 the layer folds the plane over itself, the determinant changes sign, and the change-of-variables formula with a single preimage no longer integrates to 1.
The base is N(0, I). The density is evaluated exactly on a 241 × 241 grid over [−6, 6]2 (the picture shows [−4, 4]2): each layer is inverted by 100 bisection steps on the scalar equation, and the log-determinants are added up along the recovered path; the mass is the sum over the grid times the cell area. Samples: 2 000 seeded draws pushed forward. The grey lines are the images of the base grid lines u1, u2 = −3, …, 3.
code/ch23_normalizing_flows.py (planar_forward, planar_inverse_layer, planar_logdensity, planar_grid_mass).