Normalizing Flows on Tori and Spheres
Danilo Jimenez Rezende, George Papamakarios, Sébastien Racanière, Michael S. Albergo, Gurtej Kanwar, Phiala E. Shanahan, Kyle Cranmer
Introduction
Normalizing flows are a flexible way of defining complex distributions on high-dimensional data. A normalizing flow maps samples from a base distribution to samples from a target distribution via a transformation as follows:
The transformation is restricted to be a diffeomorphism: it must be invertible and both and its inverse must be differentiable. This restriction allows us to calculate the target density via a change of variables:
In practice, is often taken to be a simple density that can be easily evaluated and sampled from, and either or its inverse are implemented via neural networks such that the Jacobian determinant is efficient to compute.
A normalizing flow implements two operations: sampling via Equation 1, and evaluating the density via Equation 2. These operations have distinct computational requirements: generating samples and evaluating their density requires only and its Jacobian determinant, whereas evaluating the density of arbitrary datapoints requires only and its Jacobian determinant. Thus, the intended usage of the flow dictates whether , or both must have efficient implementations. For an overview of various implementations and associated trade-offs, see (Papamakarios et al., 2019).
The need for probabilistic modelling of non-Euclidean data often arises in applications where the data is a set of angles, axes or directions (Mardia & Jupp, 2009). Such applications include protein-structure prediction in molecular biology (Hamelryck et al., 2006; Mardia et al., 2007; Boomsma et al., 2008; Shapovalov & Dunbrack Jr, 2011), rock-formation analysis in geology (Peel et al., 2001), and path navigation and motion estimation in robotics (Feiten et al., 2013; Senanayake & Ramos, 2018). Non-Euclidean spaces have also been explored in machine learning, and specifically generative modelling, as latent spaces of variational autoencoders (Davidson et al., 2018; Falorsi et al., 2018; Wang & Wang, 2019; Mathieu et al., 2019; Wang et al., 2019).
Methods
Since and are identified as the same point, the transformation must satisfy appropriate boundary conditions to be a valid diffeomorphism on the circle. The following conditions are sufficient:
The first two conditions ensure that and are mapped to the same point on the circle. The third condition ensures that the transformation is strictly monotonic, and thus invertible. Finally, the fourth condition ensures that the Jacobians agree at and , thus the probability density is continuous.
A restriction in the above conditions is that and are fixed points. Nonetheless, this restriction can be easily overcome by composing a transformation satisfying these conditions with a phase translation , where can be a learnable parameter. Such a phase translation is volume-preserving, so it does not incur a volume correction in the calculation of the probability density.
Given a collection of transformations satisfying the above conditions, we can combine them into a more complex transformation that also meets these conditions. One such mechanism for combining transformations is function composition , which can easily be seen to satisfy Equations 3, 5, 4 and 6. Alternatively, we can combine transformations using convex combinations, as any convex combination defined by
Next, we describe three circle diffeomorphisms that by construction satisfy the above conditions: Möbius transformations, circular splines, and non-compact projections.
We define the Möbius transformation of with centre to be .
An explicit formula for is given by
When , the transformation is just the identity. In general, the variables and in Equation 8 are meant to be -dimensional real vectors. When , the equation also reads correctly if and are taken to be complex numbers. In this case, Equation 8 and the Möbius transformations of the complex plane are related as explained in formula (6) of Kato & McCullagh (2015).
The transformation expands the part of the sphere that is close to , and contracts the rest. Hence, it can transform a uniform base distribution into a unimodal smooth distribution parameterized by . One property of the Möbius transformation is that it does not become more expressive by composing various . This is because the set of transformations , where can be any matrix in , forms a group under function composition (see Theorem 2 of Kato & McCullagh, 2015). Since composition of two such transformations remains a member of the group, their expressivity is not increased.
1.2 Circular Splines (CS)
where the coefficients are chosen so that is strictly monotonically increasing and , , and for all (see Durkan et al., 2019b, for more details).
We can easily restrict to be a diffeomorphism from to itself, by setting and . This construction satisfies the first three sufficient conditions in Equations 3, 4 and 5, and can be used to define probability densities on the closed interval . In addition, by setting , we satisfy the fourth condition in Equation 6, and hence we obtain a valid circle diffeomorphism which we refer to as a circular spline (CS).
Circular splines can be made arbitrarily flexible by increasing the number of segments . Therefore, unlike Möbius transformations, it is not necessary to combine them via convex combinations to increase their expressivity. An advantage of circular splines is that they can be inverted exactly, by first locating the corresponding segment (which can be done in iterations using binary search since the segments are sorted), and then inverting the corresponding rational quadratic (which can be done analytically by solving a quadratic equation).
1.3 Non-Compact Projection (NCP)
Even though the expression for is not defined at the endpoints and , the expression for the gradient is. The transformation satisfies the appropriate boundary conditions,
Therefore, we can extend to by continuity such that and , which yields a valid circle diffeomorphism. Although not immediately obvious, NCP flows and Möbius transformations are intimately related, as explained in Appendix H (see also Downs & Mardia, 2002, Section 2.1).
The above boundary conditions are satisfied when the transformation is affine, but they are not generally satisfied when is an arbitrary diffeomorphism. This limits the type of flow we can put on the non-compact space. Therefore, instead of making more expressive, we choose to increase the expressivity of the NCP flow by combining multiple transformations via convex combinations.
A potential issue with NCP is that, near the endpoints and , evaluating using Equation 10 directly is numerically unstable. To circumvent this numerical difficulty, we can use equivalent linearized expressions when is near the endpoints. For example, for close to we can approximate , whereas for close to we have .
The parameters of the -th transformer are a function of known as the -th conditioner. In order to guarantee that the conditioners are periodic functions of each , we can make be a function of instead. In our experiments, we implemented the conditioners using coupling layers (Dinh et al., 2017). Implementations based on masking (Kingma et al., 2016; Papamakarios et al., 2017) are also possible.
More generally, autoregressive flows can be applied in the same way on any manifold that can be written as a Cartesian product of circles and intervals, such as the -dimensional cylinder. Flows on intervals can be constructed e.g. using regular (non-circular) splines as described in Section 2.1.2. Thus, by taking to be either a circle diffeomorphism or an interval diffeomorphism as required, we can handle arbitrary products of circles and intervals.
Finally, the cylinder is transformed back to the sphere by
The update due to is simply the inverse of the above. Additionally, in Section A.1 we prove that the combined density update due to does not have any singularity whenever is such that and . Since we implement with spline flows (Section 2.1.2), this condition is satisfied by construction, so the flow density is guaranteed to be finite.
3.2 Exponential-Map Flows
Ideally, we would like to specify flows directly on the manifold and avoid mapping between non-diffeomorphic sets. This motivates us to explore exponential-map flows, a mechanism for building flows on spheres proposed by Sei (2013).
satisfy the required conditions, therefore the map
As shown in Appendix A, the density update due to is
Related Work
The study of distributions on objects such as angles, axes and directions has a long history, and is known as directional statistics (Mardia & Jupp, 2009). According to the taxonomy of Navarro et al. (2017), directional statistics traditionally uses three approaches for defining distributions on tori and spheres: wrapping, projecting, and conditioning.
In general, the above three strategies lead to distributions with tractable density evaluation and sampling algorithms only in special cases, which typically yield simple distributions with limited flexibility. One approach for increasing the flexibility of such simple distributions is via combining them into mixtures (see e.g. Peel et al., 2001; Mardia et al., 2007). Such mixtures could be used as base distributions for the flows on tori and spheres that we present in this work. However, unlike flows whose expressivity increases via composition, mixtures generally require a large number of components to represent sharp and complex distributions, and can be harder to fit in practice.
Experiments
We evaluate and compare our proposed flows based on their ability to learn sharp, correlated and multi-modal target densities. We used targets with inverse temperature and normalizer . We varied to create targets with different degrees of concentration/sharpness. The models were trained by minimizing the KL divergence
For an additional evaluation of how well the flows match the target, we used samples from the trained flows to estimate the normalizer via importance sampling:
where . The effective sample size, (Arnaud et al., 2001; Liu, 2008), can be estimated by
Higher ESS indicates that the flow matches the target better (when reliably estimated). We report ESS as a percentage of the actual sample size.
All models were optimized using Adam (Kingma & Ba, 2015) with learning rate , iterations, and batch size . The reported error bars are the standard deviation of the average, computed from independent replicas of each experiment with identical hyper-parameters, but with different initialization of the neural network weights. All shown model densities are kernel density estimates using samples and Gaussian kernel with bandwidth .
Discussion
This work shows how to construct flexible normalizing flows on tori and spheres of any dimension in a numerically stable manner. Unlike many of the distributions traditionally used in directional statistics, the proposed flows can be made arbitrarily flexible, but have tractable and exact density evaluation and sampling algorithms. We conclude with a comparison of the proposed models, a discussion of their limitations, and some preliminary thoughts on how to extend flows to other manifolds of interest to fundamental physics.
Among the flows on the circle, Möbius and NCP performed the best, with CS performing less well for highly concentrated target densities. However, increasing the expressivity of Möbius and NCP required convex combinations, whereas CS can be made more expressive by adding more spline segments. As a result, CS is the cheapest to invert (it can be done analytically), whereas Möbius and NCP (with more than one component) require a root-finding algorithm such as bisection search. Therefore, in practice it may be preferable to use CS if both density evaluation and sampling are required, and use Möbius or NCP otherwise.
2 Towards Normalizing Flows on SU(D)SU𝐷\text{SU}(D) and U(D)U𝐷\text{U}(D)
Acknowledgements
We thank Heiko Strathmann and Alex Botev for discussions and feedback. PES is partially supported by the National Science Foundation under CAREER Award 1841699 and PES and GK are partially supported by the U.S. Department of Energy, Office of Science, Office of Nuclear Physics under grant Contract Number DE-SC0011090. KC is supported by the National Science Foundation under the awards ACI-1450310, OAC-1836650, and OAC-1841471 and by the Moore-Sloan data science environment at NYU.
References
Appendix A Density Transformations on Manifolds
In this section, we explain how to update the density of a distribution transformed from one Riemannian manifold to another by a smooth map. We only consider the case where both manifolds are sub-manifolds of Euclidean spaces.
Since for any , we can always choose such that is of the form , we can restrict ourselves to this case. For such a choice, the Jacobian simplifies to
For , we can simply choose the by matrix made by removing the first column from the identity matrix. Then is equal to with the first column removed:
The product is simply a diagonal matrix of size with diagonal . Taking the determinant and applying Equation 24 concludes the proof. ∎
At first, 1 might seem worrying since the density ratio in that proposition vanishes when is or . So, as approaches the boundary of the interval $$, it seems that the correction term to the density will tend to infinity and lead to numerical instability.
As goes to , the density corrections coming from and combine to
as goes to . In particular, the terms that tend to infinity cancel each other, and the flow is well-behaved. When implementing the flow, numerical stability is achieved by not adding the terms that cancel each other. Finally, we note a subtle point about what we proved: the sequence of transformations will transform a distribution with finite density into another distribution with finite density, but we do not guarantee that the resulting density will be continuous.
In Figure 6, we provide an illustration of the recursive construction in Equations 12, 13 and 14, showing the specific wiring order of the conditional maps inside the flow. This order is the one implied by the recursion. In general, any other order can be used, or a composition of autoregressive flows with multiple orders.
Appendix C Examples of Möbius Transformations
Another family of circle transformations that we considered are Fourier transformations, defined by
We found empirically that this family of transformations is not competitive with the other transformations considered in this paper, especially for highly concentrated densities as shown in Figure 8.
Appendix E Polynomial Exponential Map
The polynomial exponential map of Sei (2013) is the exponential-map flow built using the scalar field
Appendix F Target Densities Used in Experiments
The recursive formulas shown in Equations 12, 13 and 14 require choosing a sequence of axes in order to construct the cylindrical coordinate system. This may introduce artifacts to the density related to this choice of axes. To test if this results in numerical problems, we compare the flow from Equations 12, 13 and 14 on a target density that forms a non-axis-aligned ring against a composition of the same flow with a learned rotation.
More experiments would be necessary to investigate this potential effect in higher dimensions.
Appendix H NCP as a complex Möbius transformation
This form ensures that if and has two real-valued free parameters and .
In what follows we show that for the choice and , the transformation is equivalent to an NCP transform with scale parameter and offset parameter (assuming ). If we define via , the goal is to show that defined via
We begin by expanding Equation 31 in terms of more basic trigonometric quantities,
In order to isolate , only the numerator of the expression above matters as we are only interested in ratios of the imaginary and real parts of this expression, . The numerator can be expanded as
Using the trigonometric formula , we arrive at the final result
Appendix I Application: Multi-Link Robot Arm
Appendix J Application: Learning from samples
In most of the experiments shown on this paper, we trained the models to fit a target density known up to a normalization constant (i.e. an inference problem). In this experiment we train our flow directly on data samples instead.
We trained a flow built from stacking two autoregressive flows. Each flow in the stack used circular splines and standard splines on the interval. The model was trained to maximize the likelihood of the dataset for training steps. Both splines used segments. The neural networks producing the spline parameters are the same as for the other experiments. In Figure 11 (middle) we show samples from the learned model overlaid on Earth’s map and in Figure 11 (right) we show a heat map of the learned density.