A mathematical model for automatic differentiation in machine learning
Jerome Bolte, Edouard Pauwels
Introduction
Optimization algorithms based on backpropagation oracles, and more generally on automatic or algorithmic differentiation (AD) , are one of the most widely used training tools for modern learning architectures . They often rely on popular numerical implementations as TensorFlow or PyTorch . However, for nonsmooth, nonconvex losses, AD does not have a stable theory , matching the actual practice. We wish to present a simple mathematical framework addressing this issue. Let us progressively explain our approach.
This discussion was limited to univariate functions, but these pathologies grow in size and in complexity when occurring in higher dimensions. Besides, as the “compositional depth” of functions increases the phenomenon gets more complex, making the geometry of artificial point difficult to grasp.
Canonical surjection between programs functions:
Numerical programs combine basic mathematical functions within an algorithm and return an output. This can be understood in two ways:
Computer science: it is a sequence of instructions with numerical inputs-outputs,
Mathematics: the program is a functionIn the usual mathematical sense. of its arguments.
It is tempting to identify both, but functions can be represented by different programs. This defines a surjection mapping a program to a function (in the class of functions “accessible through coding”).
Algorithmic differentiation:
As presented above, AD is an operation on programs, which takes as argument a program and returns a program with the same input variables. This operation can be “pushed” to the space of functions using the canonical surjection . Remarkably, if we restrict ourselves to programs which only smoothly combine smooth functions, then we have the following fundamental relation, depicted in Figure 2:
In other words, algorithmic differentiation of a program which smoothly combines smooth functions, is equivalent, through the canonical surjection, to derivation.
However practitioners use AD and backpropagation beyond smooth programs with nonsmooth elementary functions or program branching for instance. Can we find a proper operational interpretation of this widespread practice?
Algorithmic differentiation cannot be represented through a variational operator
At first, it is tempting to simply use AD to induce a differential operator on functions generalizing classical differentiation. This operator, say , should:
encompass the outputs of algorithmic differentation for all functions
Unfortunately such an operator does not exist:
There is no nontrivial operator on functions satisfying and .
2 Contribution and related work
We address this impossibility result and provide a class of functions together with an operational nonsmooth differential calculus which is able to cope with spurious behaviours.
We introduce a new class of nonsmooth nonconvex functions, encompassing most objective functions met in machine learning, having appealing stability properties. This allows us to define simple differential objects called selection derivatives. Selection derivatives turn out to have an operational calculus adapted to the analysis of many learning methods, as backpropagation or stochastic first order methods. They thus provide an operational model to capture nonsmooth AD as implemented in current numerical software.
Algorithmic differentiation, algorithms
This framework allows to formalize properly the relationships between, functions, algorithmic differentiation and capture the corresponding notion of critical points as met in practice. These characterize the set of attractors (limit points) for stochastic approximation algorithms based on nonsmooth backpropagation . It is important to stress that these attractors, which models sharply the whole scope of AD-induced stationnarity, are different from the traditional notions as Clarke criticality . This is described in Theorems 3 and 4.
Avoidance of traps:
As sketched above and in the introduction AD produces artificial critical points, i.e. stationary points which are not Clarke critical. These points have a parasitic nature which could be detrimental to training purposes, were they met in practice. We show that randomly initialized mini-batch stochastic gradient method do not lead to artificial critical points (Theorem 4). This result applies to modern machine learning software libraries based on AD , seen as performing operation over the reals, without any modification. Although AD may have unpredictable behavior in nonsmooth contexts, both theoretically and numerically, this result justifies theoretically that the practical impact is somewhat negligible in the context of common machine learning usage.
Related work:
Spurious behaviour of AD in nonsmooth context has been investigated in . In particular, considers qualification conditions allowing to construct AD algorithms which compute proper Clarke subgradients . However qualification is extremely hard to check and almost impossible to enforce in practice. Let us also mention which uses the notion of lexicographic derivatives, but, at this day, algorithmic computations are limited to forward mode for the moment which is of little use in machine learning.
use settings closer to ours. Piecewise smooth functions, selection derivatives and their variational properties are extensively described in . Our approach differs because we adopt more stringent definitions and rigidity assumptions, which allows in turn for much stronger properties. For instance, we fully treat backward algorithmic differentiation which is the most useful tool in machine learning.
Altogether, our contribution is an accessible and elementary framework for the conservative fields recently introduced in , without explicitly requiring the introduction of semialgebraic geometry and o-minimal structures .
Stochastic approximation algorithms are widely used in machine learning contexts . For example describes asymptotics of stochastic subgradient algorithms in nonsmooth, nonconvex settings. In contrast, we do not assume access to subgradients and instead explicitly model the behaviour of AD in optimization contexts. Our convergence results are based on , complemented by a new result on “the avoidance of critical traps” in the line of in the context of long run convergence.
Basic piecewise differentiable functions and selection gradient
We introduce a simple but vast class of functions that model rigorously the machine learning models and losses for applications such as deep learning.
Examples include polynomials, logistic loss, boosting loss, Gaussian likelihood. Observe that the corresponding functions are smooth on their open domains. Note also that if log and exp are not present we obtain the field of rational functions. See Remark 1 in Appendix A.3.
Observe that the representation is never unique, both in and in the sequence . The ReLU, hinge loss, maximal entry, -th largest entry functions are elementary selections. Note also that continuity is part of the definition.
\circ,+,\times) The class of elementary selections is stable by composition, sum and product.
The class is close to the one of piecewise functions, see e.g , but it is also much more disciplined since indices and functions are required to satisfy strong “log-exp” rigidity assumptions.
Functions in can be associated with a flexible notion of generalized derivative based on the selection structure of the underlying function.
This extends to multivariate outputs by applying the definition coordinatewise, which leads to a notion of a selection Jacobian denoted by . (ii) Given a function , a selection derivative is a derivative of the form (3) for a given representation. In that case a selection derivative of is merely denoted by .
The properties of selection derivatives might seem too liberal at first sight and too disconnected from the original function, but this is not the case as shown below.
Remark: Although less transparent, Proposition 2 is not a consequence of Proposition 3. Both results crucially rely on the rigidity of elementary functions in (Definition 3), not only on their piecewise smoothness. This a central novelty of our approach.
2 A calculus for selection derivatives
One has an unusual differential calculus: although it does not involve the linearity of some (sub)differential operator, the selection derivative of a sum gives a sum of selection derivatives provided that the selection is refined.
Then and the function is a selection derivative for .
Proposition 4 extends readily to the case when the outer function is multivariate. For example, we have a sum rule for full-domain functions in . Indeed, if and are elementary selections then and
Programs and elementary selections
Numerical programs encode numerical functions by combining elementary functions using a predecessor relation which models program execution. In what follows, can be seen as an estimate of the memory footprint of a programWe consider programs which do not overwrite values in memory, while and the number of inputs and outputs respectively.
By definition a program encodes a function, but the representation is not unique. We express this fact below through the canonical surjection of the introduction.
The following proposition illustrates the fact that practitioners implicitly implement selection functions when writing programs.
Through its input-output correspondence each program of the form (3) induces a function which is an elementary selection. In other words
Algorithmic differentiation and a variational model
Algorithmic differentiation is based on the idea of propagating infinitesimal variations in a program through the chain rule, either forward or backward.
for which belong to . Algorithm 2 combines gradients dynamically along two modes: forward or backward. Let us describes these two forms.
which is a selection Jacobian thanks to the chain rule in (5). On the other hand the backward mode computes
This quantity turns out to be the same as the one computed by the forward mode thanks to:
Denote by the algorithmic-differentiation operator. This establishes the following fundamental fact which is at the root of this work. This result asserts that practitioners implicitly implement selection derivatives when writing numerical programs and calling forward or backward AD on these programs.
Algorithmic differentiation of a given program, i.e., , outputs a selection derivative of the underlying numerical function. In other words there exists a representation of the numerical function with elementary index such that:
Algorithmic differentiation at work
The asymptotic study of first-order optimization methods implies limiting processes and necessitates thus the introduction of graph closed operators. Given a representation for , we may construct such a convex-valued mapping pointwise as followsMinimality relates to the representation of the function, not the function itself. This is the minimal convex-valued operator, constructed pointwise and guaranteed to be graph-closed..
The role of is to capture all possible outputs of AD including all possible program branches. Of course, due to combinatorial explosion, this quantity is intractable in practice. Its introduction here is only instrumental, we do not use it in algorithms, we just need to access one of its element, for example using a selection derivatives, obtained from AD. A point satisfying is called a selection critical point. We will often drop the index and write .
The two following results highlight crucial properties of in terms of optimization, they again rely on the rigidity constraint of elementary functions.
The previous result generalizes Proposition 2 by allowing to integrate arbitrary selections along absolutely continuous curves. This connects our work to the general setting of , note that has a closed graph thanks to Proposition 6 in Appendix A.3.
Considering Definition 5 with instead of leads to the Clarke subgradient, which can also be defined as
where is the dense set of differentiability points of . While can be computed pointwise (check finitely many equalities), it might be very hard to check membership in without restrictive qualification conditions on programs .
where denotes the set of permutation matrices of size . Then is a conservative mapping for and it actually corresponds to the Clarke Jacobian.
2 Convergence of gradient type algorithm and criticality of limit points
Optimization processes in learning are supposed to provide at least a critical point of the loss, i.e. a point satisfying . When using AD one enlarges the definition of criticality into and artificial critical points appear, they satisfy and . Artificial critical points could possibly trap the optimization process in strongly non-optimal situations, we thus have to determine if they have an impact on learning phases.
Note that as discussed in Section 4 selection derivatives can be computed by AD if are given by the data of numerical programs as in (3), and could be far from usual notions of subgradients. Hence this algorithm models explicitly the training of a nonsmooth deep network using existing backpropagation implementations. Note that and that is a selection gradient for as stated in Proposition 4, denote by this quantity and the corresponding set valued field (Definition 5). The following result illustrates that selection critical points are the only attractors for the recursion and that generically such attractors are actually Clarke critical. The first result stands on the theory developed in . The second parallels developments in in the context of long run convergence. The spurious behaviour illustrated in Figure 1 does not affect asymptotics, for typical initialization.
Conclusion
The current work departs from existing approaches to nonsmooth algorithmic differentiation in a fundamental way. We propose to study the backward mode of AD, as implemented in machine learning, without any modification. Our theoretical results model thus AD “as is”, and our focus is precisely on its unpredictable behavior in a nonsmooth context, addressing an issue which is ubiquitous in machine learning. Our main contribution was to prove that, in a stochastic optimization context, this spurious behavior is essentially harmless from a theoretical point of view, providing justifications for the use of AD outside of its original domain of validity in machine learning.
We achieve our goal by modeling sharply common machine learning functions and their differentiation using selection derivatives, a known concept, which models the way AD differentiates nonsmooth programs. We restrict it to certain classes of elementary functions, opening the possibility to use powerful geometric techniques.
Further questions include convergence rates and complexity issues, hardly tackled at this day, let us mention the attempt of . Our theory is limited to continuous functions and an interesting venue is to extend it to discontinuous functions, in view of treating ranking operations ubiquitous in recommendation systems, or more generally differentiating through an argmax .
Broader impact
One of the goals of the paper is to raise awareness about an important issue of in the training of ML methods: the spuriousness of AD. To address adequately this issue, we think it is necessary to include algorithmic differentiation explicitly in the study of optimization algorithms, a point of view which is largely ignored by today’s machine learning community.
Acknowledgments and Disclosure of Funding
The authors acknowledge the support of ANR-3IA Artificial and Natural Intelligence Toulouse Institute, Air Force Office of Scientific Research, Air Force Material Command, USAF, under grant numbers FA9550-19-1-7026, FA9550-18-1-0226, and ANR MasDol. J. Bolte acknowledges the support of ANR Chess, grant ANR-17-EURE-0010 and ANR OMS. The authors would like to thank anonymous referees for careful reading of this work and useful suggestions. The authors would like to thank N. Asher and S. Gerchinovitz for useful discussions. We also warmly thank J. Malick who triggered this research.
References
Appendix A A more comprehensive discussion and auxiliary results
The use of backward mode of algorithmic differentiation (AD) for neural network training expanded in the 80’s, the most cited reference being . However the theory applies to much more optimization problems, see for example . Indeed, numerical libraries implementing the backward mode of AD were already available in the 90’s for FORTRAN code or C/C++ code , 30 years before the emergence of python libraries. These early implementation could differentiate virtually any code, but their domain of validity, i.e., the setting for which one could predict what the output would be, was restricted to differentiable functions evaluated on their (open) domain of differentiability.
This was well known to the AD community, see for example , and exploring further the domain of validity of AD, beyond mere differentiability, was already a vivid problem.
Let us mention who used notions such as finite selection, “isolated criticalities”, stable domain or regular arcs, and argued that “functions given by evaluation procedures are almost everywhere real analytic or stably undefined” where “undefined” meant that a nonsmooth elementary function is used in the evaluation process. For piecewise smooth functions which nonsmoothness can be described using the absolute value function (abs-normal form), developped a piecewis linearisation formalism and local approximation related to AD, proposed an AD based bundle type method. These developments are based on the notion of piecewise smooth functions which we use in this work. More recently, applied these techniques to single layer neural network training and proposed to avoid the usage of subgradient “oracles” in nonsmooth analysis as they are not available in practice. In a similar vein, let us mention study lexicographic derivatives, a notion of directional derivatives which satisfy a chain rule making them compatible by forward mode AD, and who use directional derivatives in the context of local sampling stochastic approximation algorithms for machine learning.
Constraint qualification is known in nonsmooth analysis to ensure favorable behavior of chain rules of differential calculus for nonsmooth objects (see ). These already appeared in the context of piecewise smooth functions of Scholtes with the notion of “essential selections”. Such an approach was used in to propose an AD algorithm for subgradient computation under constrant qualification. Similarly study first and second order optimality, in relation to AD using constraint qualification.
The current work departs from all these approaches in a fundamental way. We propose to study backward mode of AD, as implemented for nonsmooth functions by standard software (e.g. TensorFlow, PyTorch), without any modification, addition of operations or hypotheses. Our theoretical results model AD as implemented in current machine learning libraries. Contrary to previous works, our focus is precisely on the unpredictable behavior of AD in nonsmooth context. Our main contribution is to show that in a stochastic optimization context, this spurious behavior is essentially harmless from a theoretical point of view, providing justifications for the use of AD outside of its original domain of validity in machine learning.
At the time this paper was accepted, we learnt about a paper proposing an analysis close to ours . The authors show that AD applied to programs involving piecewise analytic continuous functions, under analytic partitions, compute gradients almost everywhere. This is the counterpart of Proposition 3, replacing log-exp elementary function in Definitions 1 and 2, by analytic functions.
The implementation of the function used in Figure 1 is given by the function tf.nn.relu in Tensorflow software library . This implementation corresponds to the selection function described in Section 2 and the same result may be obtained by an explicit implementation of this branching selection as illustrated in the following figure
One can imagine an equivalent implementation of with a slightly different branching involving a strict inequality, that would correspond to an equivalent implementation of the same function, but the computed derivative at is different due to the implementation
A.3 Auxiliary results and remarks
(a) The building blocks in the construction of in Definition 3 could be modified and adapted to other needs. Besides, the results we present in this article would remain true if we added real analytic functions restricted to compact sets. (b) Note also that in Definition 3, functions are actually real analytic on their (open) domain of definition. Yet their extension might not be analytic, as for instance the function extended by . (c) The construction of elementary piecewise functions in Definition 3, does not coincide in general with some natural minimal o-minimal, but are contained in a larger such structure. For instance, when the basic bricks are polynomial functions, we obtain the field of rational functions which differs from the set of semi-algebraic functions.
Appendix B Proofs
Proof of Theorem 1: Recall the operator is denoted by . Fix a function , by point (a), the operator should contain
Since is arbitrary, we have shown that is trivial.
There exists a finite set of numbers , such that for all , the function is constant.
Fix , and such that on . Since , it is and we have by the fundamental theorem of integral calculus
Hence, we may define by keeping only those sets with nonempty interior and take their closure. On each set , is identical to for some and the result follows.
For each and , consider the set
Proof of Proposition 4: The derivation formula follows from the proof argument of Proposition 1, for each pair , the function is the composition of two functions and its gradient is given by on . By construction of and definition of the selection derivative, this corresponds to (5) on and the result follows.
Proof of Lemma 1: We actually prove a slightly stronger result, namely for each
We argue by exhaustion from downward to , which is the result of interest. If , we indeed have
since . Now assume that (9) holds true for an index within , then we have
where step 1 is expanding the product, step 2 is because and , step 3 combines the fact that and (9) which we assumed to be true, the last step uses again the fact that and . Hence the result holds by exhaustion.
Appendix C o-minimal structures, definability and conservative fields
We recall here the results of geometry that we use in the present work. Some references on this topic are .
the elements of are exactly the finite unions of intervals.
According to , there is an o-minimal structure which contains all semialgebraic sets and the graph of the exponential function, we fix this o-minimal structure and call it . As a consequence, all functions which can be described by a finite compositional expression involving polynomials, quotients, exponential and logarithms are definable in . In particular any function is definable in , which opens the use of powerful geometric tools for functions in . From now on, we call an object definable if it is definable in .
Any first order formula (quantification on variables only) involving definable functions and definable sets describes a definable set.
Proof of Claim 1: The function is definable and has values in . For each , the set is definable, and by (v), it is a finite union of intervals. For each consider only the endpoints of those intervals with nonempty interior, this provides the desired partition.
C.2 Properties of definable sets
where the second limit is to be understood in the Grassmanian, i.e., “directional”, sense. In the sequel we shall use the term Whitney stratification to refer to a -stratification with the Whitney-() property. The following can be found for example in .
C.3 Variational stratification and projection formulas
The equations (10) are called projection formulas and are motivated by Corollary 9 in which states that Clarke subgradients of definable functions have projection formulas.
Let us recall the definition of conservative set-valued mappings from and one of its characterization.
The following combines other results from , where one implication is essentially due to based on .
For any , admit a variational stratification.
This result allows to prove the following
Appendix D Convergence to selection critical points
Proof of Theorem 4, first part: We use here the results on conservative fields developed in . To prove the theorem it suffices to establish that:
the number of critical values are finite.
The first point is Theorem 6 while the second one is the consequence of the latter and the definability of the couple , see Proposition 8 (ii). To conclude it suffices to apply the convergence results in [12, Theorem 9].
Proof of Theorem 4, second part: This result is a consequence of the more general Theorem 7 established in Section E. Let be the finite set given in Theorem 7, the set
Appendix E Artificial critical points
Critical values are defined accordingly as images of critical points.
is a singleton, and as a consequence the critical values of are in finite number,
If had full dimension it would contain a non trivial ball on which should be constant by the integral property. This would in turn imply that the points in the ball would also be local minimum and thus Clarke critical, which is impossible.
To see that the critical values are in finite number it suffices to evoke the fact that Clarke critical values are finite and use that artificial critical values are in finite number.
By definability the connected components are arcwise-connected with piecewise paths. Using the integral property this shows is constant on .
(iii) is obvious since local minimum or maximum are Clarke critical.
As explained in the introduction, artificial critical points are “computing artefacts”, whence their names. For algorithmic differentiation the “gradient” provided by a program is zero while the point might even be a smooth non critical point. We consider the setting of the mini-batch algorithm of the last section.
converges towards a Clarke critical value almost surely,
the cluster points of are Clarke critical point almost surely.
Proof : The proof is twofold. We first prove that the set of initial conditions leading to an artificial critical point or more generally to a non differentiability point within a finite time is “small”. We then use this fact to conclude.
where denotes the identity. There exists a finite set in such that,
Take a positive real and consider the set
By diagonalization, we see that the determinant of is for any , thence
For fixed, let us consider the finite set of definable mappings defined by
We now assume that so that each mapping in has the property and
These are initial conditions in leading to an artificial critical or a non-differentiability point within before time .
where is a random noise with zero mean. Using general results , we know that sequences, when bounded almost surely, have limit points which are Clarke critical.