arXiv is now an independent nonprofit! Learn more
License: arXiv.org perpetual non-exclusive license
arXiv:2204.00523v1 [cs.LG] 01 Apr 2022

Estimating the Jacobian matrix of an unknown multivariate function from sample values by means of a neural network

Frédéric Latrémolière Affiliation: Department of Mathematics
University of Denver
Denver, CO 80208
Email: frederic@math.du.edu
   Sadananda Narayanappa Affiliation: Lockheed Martin Space
Littleton, CO 80127
Email: sadananda.narayanappa@lmco.com
   Petr Vojtěchovský Affiliation: Department of Mathematics
University of Denver
Denver, CO 80208
Email: petr@math.du.edu
Abstract

We describe, implement and test a novel method for training neural networks to estimate the Jacobian matrix JJ of an unknown multivariate function FF. The training set is constructed from finitely many pairs (x,F(x))(x,F(x)) and it contains no explicit information about JJ. The loss function for backpropagation is based on linear approximations and on a nearest neighbor search in the sample data. We formally establish an upper bound on the uniform norm of the error, in operator norm, between the estimated Jacobian matrix provided by the algorithm and the actual Jacobian matrix, under natural assumptions on the function, on the training set and on the loss of the neural network during training.

The Jacobian matrix of a multivariate function contains a wealth of information about the function and it has numerous applications in science and engineering. The method given here represents a step in moving from black-box approximations of functions by neural networks to approximations that provide some structural information about the function in question.

Index Terms: 
Jacobian matrix, Jacobian matrix estimator, neural network, nearest neighbor search.

I Introduction

I-A The problem

We propose to use neural networks to approximate the Jacobian matrix JJ of a multivariate function FF, where in the training of the neural network we use only a finite sample of input-output pairs (x,F(x))(x,F(x)). Crucially, no additional information about FF or JJ enters into the training and thus, in essence, the method proposed here differentiates a multivariate function FF solely based on a cloud of sample points.

The algorithm is based on two main ideas:

  • a loss function that utilizes a linear approximation of the sampled function FF in terms of the sought-after Jacobian matrix,

  • a nearest neighbor search in preparation of the training data from samples.

The neural network is not differentiated during or at the conclusion of the training.

In general, to estimate JJ from sample points of FF is a difficult mathematical problem. Since the Jacobian has numerous applications in mathematics, science and engineering, the ability to estimate it by any means, for instance by a neural network, is valuable in its own right.

Of particular importance here is the fact that the Jacobian can be invoked to detect relations, or lack thereof, between the input variables and output values of the unknown function FF. While neural networks are a powerful tool for nonlinear regression, the resulting interpolating function is notoriously a black-box, revealing little structural information about the sampled function FF. By taking advantage of the estimated Jacobian matrix of FF, we can start peering inside FF. We therefore expect that the ideas presented here will contribute to new approaches for the training of neural networks designed to reveal structural information about sampled data.

II Related results

II-A Differentiating trained neural networks that interpolate FF

A natural first approach to computing the Jacobian of a function FF by a neural network is to train a network to interpolate FF as usual and then differentiate the network itself (which, in the end, is a function).

One drawback of this approach is that it is possible to differentiate a neural network only if all its activation functions are differentiable, a situation that complicates the usage of some popular activation functions, such as ReLu [7]. (See [2] for a way of handling activation functions that are differentiable almost everywhere.)

A more serious obstacle is the well-known fact that even if a given function is a good approximation of another function—say in the sense of the uniform norm—their derivatives can be drastically different [14]. A simple but illustrative example is given in Figure 1. The sequence of sine waves converges uniformly on \mathbb{R} to the constant zero function since the amplitude of the waves decreases to 00. However, as the frequencies of the waves grow to infinity, the derivatives of the sine waves grow arbitrarily large and do not converge at all (not even pointwise) over \mathbb{R}.

xxyy
Fig. 1: Uniform convergence of functions does not imply converge of their derivatives.

In the context of neural networks, if a neural network is overfitted, then, in a manner similar to Runge’s phenomenon [16], the derivative of the overfitted neural network will likely have little resemblance to the derivative of the sampled function FF.

The difficulty with differentiating a neural network trained to interpolate a function is well demonstrated in [9], where, among other results, the authors train a neural network AA on a sampled function FF, differentiate the resulting neural network as a function, thus obtaining its Jacobian matrix JAJ_{A}, and then compare the interpolated values of FF produced by AA with linear approximations of FF based on the Jacobian matrix JAJ_{A}. They find that “the JJ estimation task fails” and conclude that neural networks “may be not suitable to handle derivative signal analysis.”

II-B Differentiating neural networks during training

In the highly influential paper [11], the authors proposed a method for training a neural network to solve (higher order) differential equations. The main idea of [11] is to differentiate the neural network during training and use this (higher order) derivative in the loss function. The training points are typically selected on a regular grid and hence it must be possible to evaluate all relevant functions at such points. The method of [11] is robust and it can be adopted to multivariate functions. We mention [13] as one of the many papers ultimately based on [11].

In the simplest case covered by [11], an approximate solution yy to the differential equation y(x)=F(x)y^{\prime}(x)=F(x) is obtained by comparing the neural network derivative AA^{\prime} to FF during training. Once AA is fully trained, AA^{\prime} should be a good approximation of FF, that is, AA itself should be a good approximation of an antiderivate of FF.

In order to obtain an approximation for the derivative of FF, one could now differentiate the trained network AA twice (running into the issues described in the previous subsection), or start over with the differential equation y(x)=F′′(x)y^{\prime}(x)=F^{\prime\prime}(x) (which presumably requires FF^{\prime} to be already known), or consider a variation of [11] in which the neural network AA is integrated rather than differentiated during training. Little seems to be known about integration of neural networks.

II-C Situations in which the Jacobian matrix can be computed by standard methods

One might work with data sampled from a function that satisfies a known differential equation, say for physical reasons. In the simplest case where the equation is first order and linear, the Jacobian matrix of the function can thus be computed directly from the differential equation. This essentially avoids the problem of estimating the Jacobian matrix by neural network altogether and relies on standard mathematical methods.

Similarly, the well-known automatic differentiation tool Autograd [12] (that is often used in connection with neural networks) conveniently differentiates a function whose code is written in NumPy [8], but it requires the function to be given explicitly (in NumPy code), not just by sample values.

II-D Gradient estimation

Gradient estimation is a vast topic, cf. [6]. In the context of neural networks, gradient estimation typically refers to an estimation of the gradient of the explicitly given loss function. Gradient estimation is critical for the stochastic gradient descent algorithm, which is in turn key in the learning algorithm of neural networks [15]. The backpropagation algorithm [3, 5, 10] is, in essence, an algorithm that efficiently computes the gradient of a loss function using a graph-directed implementation of the chain rule. However, backpropagation relies on symbolic differentiation or on a tool similar to Autograd, as well as on a specific form of the neural network.

Another meaning of the phrase “gradient estimation” arises in situations when differentiating a given function is slow or impossible and its Jacobian matrix is therefore merely approximated. For instance, in [17] the authors give an efficient approximation of the Jacobian matrix by fast Fourier transform in the context of MRI. Such methods typically rely on a prescribed model for the function whose gradient is being estimated.

In contrast with all these results, we do not differentiate a neural network that has been trained to interpolate FF, nor do we differentiate the neural network during training. Rather, we directly train a neural network to estimate the Jacobian matrix of an unknown function FF from given sample values of FF, regardless of the context. We prove rigorously that the Jacobian estimator approaches the Jacobian of FF in norm, under reasonable assumptions on FF, the sample set and the performance of the neural network used.

III Preliminaries

In this section we define the Jacobian matrix, recall the linear approximation formula for multivariate functions, and overview the general interpolation problem with a view toward neural networks. Readers familiar with these topics can skip forward to Section IV.

III-A Jacobian matrix and linear approximations

Suppose that cc, dd are positive integers, UU is an open, bounded subset of d\mathbb{R}^{d}, and F:UcF:U\to\mathbb{R}^{c} is a Fréchet differentiable function on UU [14, Ch. 9]. (Note that dd stands for the dimension of the domain d\mathbb{R}^{d} and cc for the dimension of the codomain c\mathbb{R}^{c}.)

Writing x=(x1,,xd)x=(x_{1},\ldots,x_{d}) for xdx\in\mathbb{R}^{d} and F=(F1,,Fc)F=(F_{1},\ldots,F_{c}) with Fj:dF_{j}:\mathbb{R}^{d}\rightarrow\mathbb{R} for each j{1,,c}j\in\{1,\ldots,c\}, the Jacobian matrix of FF is the matrix

J=JF=(F1x1F1xdFcx1Fcxd).J=J_{F}=\left(\begin{array}[]{ccc}\frac{\partial F_{1}}{\partial x_{1}}&\cdots&\frac{\partial F_{1}}{\partial x_{d}}\\ \vdots&&\vdots\\ \frac{\partial F_{c}}{\partial x_{1}}&\cdots&\frac{\partial F_{c}}{\partial x_{d}}\end{array}\right)\text{.}

In words, JFJ_{F} is the matrix whose rows are the gradients of F1,,FcF_{1},\dots,F_{c}.

Since the Jacobian JFJ_{F} is the matrix of the Fréchet derivative of FF with respect to the canonical basis, it satisfies by definition the following general linear approximation property (which is of course the very idea of the derivative):

limh0F(x+h)F(x)JF(x)hhd=0,\lim_{h\rightarrow 0}\frac{F(x+h)-F(x)-J_{F}(x)\cdot h}{\left\|{h}\right\|_{\mathbb{R}^{d}}}=0\text{,} (III.1)

where hd\left\|{h}\right\|_{\mathbb{R}^{d}} is the usual Euclidean norm

hd=(h1,,hd)d=(i=1dhi2)1/2.\left\|{h}\right\|_{\mathbb{R}^{d}}=\left\|{(h_{1},\dots,h_{d})}\right\|_{\mathbb{R}^{d}}=\left(\sum_{i=1}^{d}h_{i}^{2}\right)^{1/2}.

Equation (III.1) will play a crucial role in the loss function of our neural network.

III-B The interpolation problem in general

In a regression problem with noise, we are given a finite set S={(x,F(x)+ϵx):xX}S=\left\{(x,F(x)+\epsilon_{x}):x\in X\right\} of pairs of input-output values sampled from an unknown function F:UdcF:U\subseteq\mathbb{R}^{d}\rightarrow\mathbb{R}^{c}, where XUX\subseteq U and (ϵx)xX(\epsilon_{x})_{x\in X} is a family of independent random variables, all with mean 00, representing noise. The goal is to find an estimate for FF.

If we make the very strong assumption that FF is linear, i.e., that there exists a c×dc\times d matrix AMc×d()A\in M_{c\times d}(\mathbb{R}) such that F(x)=AxF(x)=Ax for all xUx\in U, then we may apply the standard linear, least square statistical method to derive an estimate for AA, by minimizing the error

BMc×d()minxXBxF(x)c2.B\in M_{c\times d}(\mathbb{R})\mapsto\min_{x\in X}\left\|{Bx-F(x)}\right\|_{\mathbb{R}^{c}}^{2}.

In this case, we note that the Jacobian matrix of FF is again AA and, of course, AA contains a lot of useful information about FF.

Without the assumption of linearity of FF, the problem becomes significantly more complicated and computationally intensive. A general method is to replace the algebra Mc×d()M_{c\times d}(\mathbb{R}) of matrices by a set of nonlinear functions which is parametrized by some points in the parameter space P\mathbb{R}^{P}, where PP is typically quite large. The main example of interest here is a class of functions called artificial neural networks, which, in their simplest form, can be described as a finite chain of alternating compositions of linear functions and functions called activation functions.

Formally, an artificial neural network AA with \ell layers, with n0n_{0} inputs, and with njn_{j} neurons and activation function fj:f_{j}:\mathbb{R}\rightarrow\mathbb{R} in layer jj for each j{1,,}j\in\{1,\ldots,\ell\}, is the function

A=f×nWf1×n1W1f1×n1W1,A=f_{\ell}^{\times n_{\ell}}\circ W_{\ell}\circ f_{\ell-1}^{\times n_{\ell-1}}\circ W_{\ell-1}\circ\ldots\circ f_{1}^{\times n_{1}}\circ W_{1},

where, for each j{1,,}j\in\{1,\ldots,\ell\}, the matrix of weight WjW_{j} (identified above with the linear map xWjxx\mapsto W_{j}x) has size nj×nj1n_{j}\times n_{j-1}, and fj×njf_{j}^{\times n_{j}} maps (x1,,xnj)nj(x_{1},\ldots,x_{n_{j}})\in\mathbb{R}^{n_{j}} to (fj(x1),,fj(xnj))(f_{j}(x_{1}),\ldots,f_{j}(x_{n_{j}})). A typical point of view is that the weight matrices WjW_{j} are the parameters of the neural network, while the number of layers, the number of neurons in individual layers and the activation functions are fixed for the given problem.

Once the class of neural networks is fixed, the regression problem is then to find a neural network AA as above which is a good approximation to a solution of the minimization problem

B neural networkminxXB(x)F(x)c2.B\text{ neural network}\mapsto\min_{x\in X}\left\|{B(x)-F(x)}\right\|_{\mathbb{R}^{c}}^{2}\text{.}

Solving this minimization problem is in general very difficult. A key observation is that an algorithm based on the gradient descent (or its variants, especially the stochastic gradient descent) called backpropagation has proven effective in practice. The process of (numerically, approximately) solving the minimization problem is referred to as the training of the neural network.

Under advantageous conditions, we can then interpolate between the values given in the sample set SS by means of a trained neural network AA, thus obtaining a (hopefully good) approximation to FF on its domain UU. However, unlike in the linear case, the obtained neural network AA is a black-box estimator in that it reveals no particular structure of the function FF. It is therefore nontrivial to use AA for anything more than interpolating the sample data.

III-C The difficulty in estimating the Jacobian matrix directly from sample points

Given a set of sample points {(x,F(x)):xX}\{(x,F(x)):x\in X\} with XUX\subseteq U finite (and well-distributed over UU, for instance, ε\varepsilon-dense for an ε\varepsilon that is small enough for the scale of the problem), estimating the partial derivatives of FF is a difficult problem. Here are some reasons why:

  • The sample set XX is random and thus, given x=(x1,,xd)Xx=(x_{1},\ldots,x_{d})\in X, we cannot assume that it contains points of the form (x1+Δx1,x2,,xd)(x_{1}+\Delta x_{1},x_{2},\ldots,x_{d}), etc, aligned with xx in one of the cardinal directions. Consequently, estimating partial derivatives involves a change of basis at each point, incurring a lot of computations and numerical errors.

  • Estimating a partial derivative is difficult in general. One way to see the statistical issue is as follows. If xXx\in X and x+hXx+h\in X then F(x+h)F(x)h\frac{F(x+h)-F(x)}{h} can be used as an approximation for J(x)J(x), but any error on F(x)F(x) and F(x+h)F(x+h), even if the error is small, is amplified upon dividing by the small quantity hh. More formally, since we really only know F(x)+ϵxF(x)+\epsilon_{x} and F(x+h)+ϵx+hF(x+h)+\epsilon_{x+h}, then ϵx+hϵxh\frac{\epsilon_{x+h}-\epsilon_{x}}{h} will typically be large for small hh. If the noise variables ϵx\epsilon_{x} and ϵx+h\epsilon_{x+h} have variance σ2\sigma^{2}, then the variance of ϵx+hϵxh\frac{\epsilon_{x+h}-\epsilon_{x}}{h} is 2σ2h\frac{2\sigma^{2}}{h}, which is large when hh is small.

  • Even if we could surmount the above two issues, we would still need some regression technique, such as neural networks, to interpolate the values of partial derivatives at inputs not contained in XX.

IV The algorithm

In this section we show in detail how to move one step beyond the regression problem for functions and present an algorithm that estimates the Jacobian matrix of a sampled function FF by means of a neural network. The only assumption on F:UdF:U\to\mathbb{R}^{d} is that it is differentiable on the bounded open set UdU\subseteq\mathbb{R}^{d}. The main idea is to use equation (III.1) to estimate JJ directly from pairs of points in the data cloud {(x,F(x)):xXU}\{(x,F(x)):x\in X\subseteq U\}.

IV-A Illustrating the algorithm

Let us first illustrate the main idea of the algorithm in the simplest case c=d=1c=d=1. Suppose that we wish to train a neural network J^\widehat{J} for approximating the derivative of F:F:\mathbb{R}\to\mathbb{R} from a finite sample set {(x,F(x)):xX}\{(x,F(x)):x\in X\}. Suppose that in the process of training J^\widehat{J} we encounter a sample point (a,F(a))(a,F(a)). Let (b,F(b))(b,F(b)) be another sample point. If bb is close enough to aa then F(b)F(b) is approximately equal to

F(a)+F(a)(ba).F(a)+F^{\prime}(a)(b-a).

Since J^\widehat{J} is being trained to approximate the unknown derivative FF^{\prime}, we naturally consider the known quantity

F(a)+J^(a)(ba)F(a)+\widehat{J}(a)(b-a)

instead and compare it to F(b)F(b), see Figure 2.

xxyyF(a)F(a)F(b)F(b)F(a)+F(a)(ba)F(a){+}F^{\prime}(a)(b{-}a)F(a)+J^(a)(ba)F(a){+}\widehat{J}(a)(b{-}a)aabbFFloss
Fig. 2: The loss (before normalization) for the Jacobian matrix estimator J^\widehat{J} resulting from the sample points (a,F(a))(a,F(a)), (b,F(b))(b,F(b)) of an unknown function F:F:\mathbb{R}\to\mathbb{R}. The secant line through (a,F(a))(a,F(a)) with slope J^(a)\widehat{J}(a) is in green. The unknown tangent line through (a,F(a))(a,F(a)) is in red.

We use the related normalized quantity

|F(b)(F(a)+J^(a)(ba))|2|ba|2.\frac{|F(b)-(F(a)+\widehat{J}(a)(b-a))|^{2}}{|b-a|^{2}}. (IV.1)

as a contribution to the loss function.

In the general case of estimating the Jacobian matrix of F:dcF:\mathbb{R}^{d}\to\mathbb{R}^{c}, the only differences from the \mathbb{R}\to\mathbb{R} case are:

  • The neural network J^\widehat{J} takes a point in d\mathbb{R}^{d} as an input and returns a real c×dc\times d matrix.

  • While training J^\widehat{J} at aXa\in X, we do not use a randomly chosen sample point bXb\in X close to bb but rather the kmaxk_{max} nearest neighbors of aa within radius rmaxr_{max}, where kmaxk_{max} and rmaxr_{max} are parameters of the algorithm. The nearest neighbors are precalculated and processed in batches.

  • The loss function (IV.1) is replaced with its multivariate analog, that is, for every data point (a,F(a))(a,F(a)) and its neighbor (b,F(b))(b,F(b)), the contribution to the loss is

    F(b)(F(a)+J^(a)(ba))c2bad2.\frac{\left\|{F(b)-(F(a)+\widehat{J}(a)(b-a))}\right\|_{\mathbb{R}^{c}}^{2}}{\left\|{b-a}\right\|_{\mathbb{R}^{d}}^{2}}\text{.} (IV.2)

IV-B The algorithm

A pseudo-code for the Jacobian matrix estimator can be found in Algorithm 1.

input : 
XX, an array of NN vectors in d\mathbb{R}^{d}
YY, an array of NN vectors in c\mathbb{R}^{c}
kmaxk_{max}, a positive integer
rmaxr_{max}, a positive real number
output : 
J^\widehat{J}, a trained neural network
/* initialize neural network */
J^initial neural network\widehat{J}\leftarrow\text{initial neural network}
/* data for nearest neighbors */
D[]D\leftarrow[\ ]
for i0i\leftarrow 0 to N1N-1 do
NiN_{i}\leftarrow NearestNeighbors(X,i,kmax,rmaxX,i,k_{max},r_{max})
 D[i]{(i,j):jNi}D[i]\leftarrow\{(i,j):j\in N_{i}\}
   end for
DUnion(D)D\leftarrow\textnormal{{Union(}}\textnormal{\emph{D}}\textnormal{{)}}
// list of pairs
/* loss function for a batch BDB{\subseteq}D */
ff\leftarrow function( BB )
s0s\leftarrow 0
 for dBd\in B do
 id[0]i\leftarrow d[0]
    jd[1]j\leftarrow d[1]
    ss+Y[j](Y[i]+J^(X[i])(X[j]X[i]))c2X[j]X[i]d2s\leftarrow s+\frac{\left\|{Y[j]-(Y[i]+\widehat{J}(X[i])(X[j]-X[i]))}\right\|_{\mathbb{R}^{c}}^{2}}{\left\|{X[j]-X[i]}\right\|_{\mathbb{R}^{d}}^{2}}
      end for
 return s|B|\frac{s}{|B|}
 
end
/* the training cycle */
J^\widehat{J}\leftarrow train J^\widehat{J} on DD using loss function ff
return J^\widehat{J}
Algorithm 1 JacobianEstimator

The algorithm uses standard methods of stochastic gradient descent with backpropagation but it relies on a novel loss function and data preparation which utilizes a nearest neighbor search. An implementation of the algorithm in TensorFlow 2.5.0 [1] can be found in the appendix.

TABLE I: The testing functions.
name function domain
F0:2F_{0}:\mathbb{R}^{2}\to\mathbb{R} (x,y)xexp(x2y2)(x,y)\mapsto x\exp(-x^{2}-y^{2}) (2,2)2(-2,2)^{2}
F1:2F_{1}:\mathbb{R}^{2}\to\mathbb{R} (x,y)xy(x,y)\mapsto xy (1,1)2(-1,1)^{2}
F2:2F_{2}:\mathbb{R}^{2}\to\mathbb{R} (x,y)x3+2xy2(x,y)\mapsto x^{3}+2xy^{2} (0,2)2(0,2)^{2}
F3:2F_{3}:\mathbb{R}^{2}\to\mathbb{R} (x,y)ln(1+x2y)(x,y)\mapsto\ln(1+x^{2}y) (0,2)2(0,2)^{2}
F4:2F_{4}:\mathbb{R}^{2}\to\mathbb{R} (x,y)(x+y)(x2+xy2+1)1(x,y)\mapsto(x+y)(x^{2}+xy^{2}+1)^{-1} (1,1)2(-1,1)^{2}
F5:2F_{5}:\mathbb{R}^{2}\to\mathbb{R} (x,y)cos(x2)+cos(y2)+3x(x,y)\mapsto\cos(x^{2})+\cos(y^{2})+3x (1,1)2(-1,1)^{2}
F6:2F_{6}:\mathbb{R}^{2}\to\mathbb{R} (x,y)1+x+x1+y(x,y)\mapsto\sqrt{1+x}+x\sqrt{1+y} (0,3)2(0,3)^{2}
F7:2F_{7}:\mathbb{R}^{2}\to\mathbb{R} (x,y)arctan(x+y2)(x,y)\mapsto\arctan(x+y^{2}) (3,3)2(-3,3)^{2}
F8:32F_{8}:\mathbb{R}^{3}\to\mathbb{R}^{2} (x,y,z)(x(x+y)+y2+zx,xyz)(x,y,z)\mapsto(x(x+y)+y^{2}+zx,xyz) (1,1)3(-1,1)^{3}
F9:33F_{9}:\mathbb{R}^{3}\to\mathbb{R}^{3} (x,y,z)(sin(xy)+sin(zy),cos(x+y)+cos(x+z),x+y+z)(x,y,z)\mapsto(\sin(xy)+\sin(zy),\cos(x+y)+\cos(x+z),x+y+z) (1,1)3(-1,1)^{3}
F10:44F_{10}:\mathbb{R}^{4}\to\mathbb{R}^{4} (x,y,z,t)(sin(xy),cos(xz)+cos(yt),110(x+y))(x,y,z,t)\mapsto(\sin(xy),\cos(xz)+\cos(yt),\frac{1}{10}(x+y)) (1,1)4(-1,1)^{4}
F11:52F_{11}:\mathbb{R}^{5}\to\mathbb{R}^{2} (x,y,z,t,w)(x(z+t)+yw,(x+y)exp(z2w2t))(x,y,z,t,w)\mapsto(x(z+t)+yw,(x+y)\exp(-z^{2}-w^{2}-t)) (1,1)5(-1,1)^{5}
F12:5F_{12}:\mathbb{R}^{5}\to\mathbb{R} (x,y,z,t,w)exp(x2xy23z22t+w)(x,y,z,t,w)\mapsto\exp(-x^{2}-\frac{xy}{2}-\frac{3z^{2}}{2}-t+w) (1,1)5(-1,1)^{5}

The sample set is represented as two lists XdX\subseteq\mathbb{R}^{d} and YcY\subseteq\mathbb{R}^{c} of the same length NN. For every index jj, we think of the pair (X[j],Y[j])(X[j],Y[j]) as a sample point (x,F(x))(x,F(x)), where FF is some unknown differentiable function F:UcF:U\to\mathbb{R}^{c}, UdU\subseteq\mathbb{R}^{d}.

In Algorithm 1, the initial neural network J^\widehat{J} has dd cells in its input layer and c×dc\times d cells in its output layer. The function

NearestNeighbors(X,i,kmax,rmax)\textsf{NearestNeighbors}(X,i,k_{max},r_{max})

returns all indices 0j<N0\leq j<N such that X[j]X[i]X[j]\neq X[i] is among the kmaxk_{max} nearest neighbors of X[i]X[i] in XX and X[i]X[j]d<rmax\left\|{X[i]-X[j]}\right\|_{\mathbb{R}_{d}}<r_{max}.

IV-C Notes on the algorithm

Let us point out additional features of Algorithm 1 that were used in our implementation and that are not necessarily captured in the pseudo-code.

  • In order not to include many nearby points in one batch, we randomly permute the entries of DD at the end of the data preparation stage.

  • Even if |X|=N=|Y||X|=N=|Y| and the parameters kmaxk_{max} and rmaxr_{max} are fixed, then number of training points in each run of the algorithm depends on XX (since we do not know in advance how many nearest neighbors of a given point will satisfy the constraint on rmaxr_{max}). After the training data DD is prepared, we adjust the batch size so that it divides the size of DD evenly. Note that the training set therefore has cardinality |D||D| satisfying N|D|NkmaxN\leq|D|\leq Nk_{max}.

  • We allow to set the value of rmaxr_{max} to infinity so that every nearest neighbor set has cardinality kmaxk_{max} (if kmax<Nk_{max}<N).

  • The effect of the parameter kmaxk_{max} can be suppressed by setting its value to at least NN.

Note that it is possible for both (i,j)(i,j) and (j,i)(j,i) to occur in the training set DD. If (i,j)D(i,j)\in D, which means that X[j]X[j] is one of the kmaxk_{max} nearest neighbors of X[i]X[i] and within radius rmaxr_{max} of X[i]X[i], it does not necessarily follow that (j,i)D(j,i)\in D, too. With each (i,j)D(i,j)\in D, the neural network J^\widehat{J} will be trained using a linear approximation centered at X[i]X[i].

IV-D Using the trained Jacobian neural network

Having trained the neural network J^\widehat{J} by Algorithm 1, we can obtain an approximate value of the Jacobian matrix JJ of the unknown function F:UcF:U\to\mathbb{R}^{c} by computing J^(x)\widehat{J}(x) for xUx\in U.

Note that we can also obtain an approximate value of the unknown function FF itself at xUx\in U by locating a nearby point yy in the sample set and computing F(y)+J^(y)(xy)F(y)+\widehat{J}(y)(x-y).

V Examples and results

We now test and validate Algorithm 1. The testing functions, their names and their domains are summarized in Table I, while the results (error estimates) can be found in Table II.

Remark V.1.

Most functions in Table I are scalar valued (as opposed to vector valued) but this is at little loss of generality. Indeed, a vector valued function F:dcF:\mathbb{R}^{d}\to\mathbb{R}^{c} can be represented by cc scalar valued functions Fi:dF_{i}:\mathbb{R}^{d}\to\mathbb{R}, 1ic1\leq i\leq c, a neural network can be trained on the same sample set to produce the estimated Jacobian matrix (gradient) J^i\widehat{J}_{i} for FiF_{i}, and the estimated Jacobian matrix J^\widehat{J} for FF is then obtained as J^=(J^1,,J^c)\widehat{J}=(\widehat{J}_{1},\dots,\widehat{J}_{c})^{\top}. However, since training a neural network on a vector valued function is not the same as training several neural networks on scalar valued functions, we have included a few vector valued functions in Table I to demonstrate that Algorithm 1 can cope with that situation as well.

In Subsection V-A we train J^\widehat{J} for the function F0F_{0} of Table I on a sample set consisting of N=106N=10^{6} points (resulting in a training set whose size is between NN and NkmaxNk_{max}), and we compare J^\widehat{J} and JJ visually as vector fields on a regular grid.

In Subsection V-B we describe in detail two validation methods, one for the situation when the function FF is not known to the training algorithm but is known to us, and another for the situation when the function FF is truly unknown. We then train Algorithm 1 on every function from Table I using random sample sets of various sizes. As expected, we observe that the error improves with the size of the training set and that it gets worse as the volume of the domain increases.

Refer to caption
(a) The graph of F0F_{0}.
Refer to caption
(b) The cloud of training points for J^\widehat{J}.
Refer to caption
(c) The gradient JJ (scaled).
Refer to caption
(d) The estimated gradient J^\widehat{J} (scaled).
Refer to caption
(e) The gradient JJ (to scale).
Refer to caption
(f) The estimated gradient J^\widehat{J} (to scale).
Fig. 3: A visual comparison of the Jacobian JJ of F0F_{0} and the trained Jacobian estimator J^\widehat{J}.

Finally, in Subsection V-C we discuss the effects of varying certain parameters of the algorithm (namely rmaxr_{max}, kmaxk_{max}, and the geometry of the neural network), as well as the effect of adding noise to the sampling data. We also comment on the limitations of Algorithm 1 and on computing time.

V-A A visual example

In all examples of Subsections V-A and V-B we set the parameters of the algorithm as

  • rmax=0.5r_{max}=0.5,

  • kmax=30k_{max}=30,

and of the neural network as

  • a simple forward ANN,

  • with 44 hidden layers consisting of 100100, 100100, 5050 and 2020 neurons,

  • using the swish activation function for all cells,

  • and trained with stochastic gradient descent in 5050 epochs and batch size of 5050.

Consider the function F0F_{0} of Table I. This function is not known to Algorithm 1 for the purposes of training the Jacobian matrix estimator J^\widehat{J}, nevertheless it is known to us and we can therefore visualize it, calculate its Jacobian matrix JJ by standard symbolic differentiation, and visualize the Jacobian matrix as well.

The graph of F0F_{0} is given in Figure 3(a). Its Jacobian matrix JJ is a function 22\mathbb{R}^{2}\to\mathbb{R}^{2} and we plot it as a vector field on a regular grid of 20×2020\times 20 points contained in (2,2)2(-2,2)^{2}. This is done in Figure 3(c), where the vectors are automatically scaled to improve legibility, and in Figure 3(e), where the vectors are to scale.

The input of the algorithm is a cloud of N=106N=10^{6} sample points {(x,F(x)):xX}\{(x,F(x)):x\in X\}, where the set X(2,2)2X\subseteq(-2,2)^{2} is generated randomly. Such a cloud is visualized in Figure 3(b), except that only 10310^{3} points are plotted in the figure to improve legibility.

The resulting Jacobian matrix estimator J^\widehat{J} is visualized as a vector field in Figure 3(d) automatically scaled, and in Figure 3(f) to scale.

Refer to caption
Fig. 4: The difference J^J\widehat{J}-J (to scale).

In order to compare the near-identical vector fields of JJ and J^\widehat{J}, we offer Figure 4 in which we plot the vector field J^J\widehat{J}-J to scale.

V-B Error statistics

It can be seen plainly from Figure 4 that J^\widehat{J} is a good estimator of JJ for the function F0F_{0}. In order to quantify this fact for F0F_{0} and for the other testing functions F:dcF:\mathbb{R}^{d}\to\mathbb{R}^{c} from Table I, we consider two validation methods.

For the first validation method, suppose that the Jacobian matrix estimator is trained for a function F:dcF:\mathbb{R}^{d}\to\mathbb{R}^{c} that is not known to the algorithm but that is known to us. We can therefore compare the estimated Jacobian matrix J^\widehat{J} with the actual Jacobian matrix JJ of FF calculated by standard symbolic differentiation.

In more detail, let SS be a very large, randomly generated subset of the domain of FF. (We used |S|=106|S|=10^{6} throughout.) For δ0\delta\geq 0, let

Sδ={xS:J(x)cd>δ},S_{\delta}=\{x\in S:\left\|{J(x)}\right\|_{\mathbb{R}^{cd}}>\delta\},

where J(x)cd\left\|{J(x)}\right\|_{\mathbb{R}^{cd}} is the Frobenius norm of the matrix J(x)J(x), that is, the Euclidean norm of the vector in cd\mathbb{R}^{cd} obtained by concatenating the rows of J(x)J(x). Consider the error estimate

Eδ=1|Sδ|xSδJ^(x)J(x)cdJ(x)cdE_{\delta}=\frac{1}{|S_{\delta}|}\sum_{x\in S_{\delta}}\frac{\left\|{\widehat{J}(x)-J(x)}\right\|_{\mathbb{R}^{cd}}}{\left\|{J(x)}\right\|_{\mathbb{R}^{cd}}} (V.1)

that averages pointwise over SδS_{\delta} the size of the error J^(x)J(x)\widehat{J}(x)-J(x) relative to the size of J(x)J(x). We will refer to EδE_{\delta} as the average relative error of J^\widehat{J} over SS and report it in percents.

Remark V.2.

The average relative error EδE_{\delta} is sensitive to the parameter δ\delta. By setting δ=0\delta=0, we exclude only those points xSx\in S with J(x)=0J(x)=0, that is, the points of SS where the relative error is not defined. By setting δ\delta to a small positive value, we also exclude the points xSx\in S with small J(x)J(x), hence in general improving the error estimate by ignoring the points where the relative error is greatly magnified by the small denominator.

For the second validation method, suppose that we would like to train the Jacobian matrix estimator on a sample set {(x,F(x)):xX}\{(x,F(x)):x\in X\} for a function F:dcF:\mathbb{R}^{d}\to\mathbb{R}^{c} that is not known to us. We split XX into two disjoint subsets XTX_{T} and XVX_{V}, a larger training set XTX_{T} and a smaller validation set XVX_{V}. (We used |XV|=104|X_{V}|=10^{4} except as otherwise noted.) We then train J^\widehat{J} on the training set {(x,F(x)):xXT}\{(x,F(x)):x\in X_{T}\} and we calculate the error on the validation set XVX_{V} as follows.

Let XVX^{*}_{V} be the set of all pairs (a,b)XV×XV(a,b)\in X_{V}\times X_{V} such that bb is a near neighbor of aa obtained by the same near neighbor routine that has been employed in Algorithm 1. For δ>0\delta>0, let

Sδ={(a,b)XV:F(b)c>δ}S^{*}_{\delta}=\{(a,b)\in X^{*}_{V}:\left\|{F(b)}\right\|_{\mathbb{R}^{c}}>\delta\}

and note that F(b)F(b) is known from the sample set. Instead of comparing J^\widehat{J} to the unknown Jacobian matrix JJ, consider the error estimate

Eδ=1|Sδ|(a,b)SδF(b)(F(a)+J^(a)(ba))cF(b)cE^{*}_{\delta}=\frac{1}{|S^{*}_{\delta}|}\sum_{(a,b)\in S^{*}_{\delta}}\frac{\left\|{F(b)-(F(a)+\widehat{J}(a)(b-a))}\right\|_{\mathbb{R}^{c}}}{\left\|{F(b)}\right\|_{\mathbb{R}^{c}}}

based on linear approximations. The purpose of the parameter δ\delta is the same as in the first error estimate (V.1). We again report EδE^{*}_{\delta} in percents.

Table II summarizes the error estimates for all testing functions from Table I.

TABLE II: The average relative error (in percents) of the Jacobian matrix estimator trained by Algorithm 1 for the functions from Table I.
function NN E0E_{0} E0.001E_{0.001} E0.01E_{0.01} E0.1E_{0.1} E0.01E^{*}_{0.01}
F0F_{0} 10310^{3} 24.924.9 24.924.9 21.221.2 11.411.4 4.424.42
10410^{4} 5.785.78 5.785.78 5.225.22 3.373.37 2.732.73
10510^{5} 3.113.11 3.103.10 2.832.83 1.941.94 2.692.69
10610^{6} 1.201.20 1.201.20 1.111.11 0.890.89 2.692.69
F1F_{1} 10410^{4} 0.570.57 0.560.56 0.560.56 0.550.55 0.700.70
10510^{5} 0.420.42 0.420.42 0.420.42 0.400.40 0.690.69
F2F_{2} 10410^{4} 6.336.33 2.722.72 1.821.82 1.331.33 0.550.55
10510^{5} 1.921.92 1.211.21 0.820.82 0.540.54 0.490.49
F3F_{3} 10410^{4} 21.921.9 7.257.25 3.663.66 1.651.65 0.700.70
10510^{5} 8.708.70 1.651.65 0.680.68 0.300.30 0.640.64
F4F_{4} 10410^{4} 1.191.19 1.191.19 1.191.19 1.191.19 0.430.43
10510^{5} 0.880.88 0.880.88 0.880.88 0.880.88 0.430.43
F5F_{5} 10410^{4} 0.790.79 0.790.79 0.790.79 0.790.79 0.290.29
10510^{5} 0.240.24 0.240.24 0.240.24 0.240.24 0.280.28
F6F_{6} 10410^{4} 1.001.00 1.001.00 1.001.00 1.001.00 0.040.04
10510^{5} 0.270.27 0.270.27 0.270.27 0.270.27 0.040.04
F7F_{7} 10410^{4} 6.846.84 6.846.84 6.846.84 6.116.11 1.441.44
10510^{5} 2.922.92 2.922.92 2.922.92 2.602.60 1.401.40
F8F_{8} 10410^{4} 4.614.61 4.614.61 4.604.60 4.604.60 3.613.61
10510^{5} 1.901.90 1.901.90 1.891.89 1.891.89 3.603.60
F9F_{9} 10410^{4} 1.991.99 1.991.99 1.991.99 1.991.99 0.280.28
10510^{5} 0.950.95 0.950.95 0.950.95 0.950.95 0.280.28
F10F_{10} 10410^{4} 7.757.75 7.757.75 7.747.74 7.747.74 3.913.91
10510^{5} 3.813.81 3.813.81 3.813.81 3.813.81 0.430.43
10610^{6} 1.991.99 1.991.99 1.991.99 1.991.99 0.430.43
F11F_{11} 10410^{4} 8.568.56 8.568.56 8.568.56 8.568.56 9.989.98
10510^{5} 4.334.33 4.334.33 4.334.33 4.334.33 9.929.92
10610^{6} 2.422.42 2.422.42 2.422.42 2.422.42 10.310.3
F12F_{12} 10510^{5} 7.727.72 7.727.72 7.727.72 7.527.52 8.878.87
10610^{6} 4.924.92 4.924.92 4.904.90 4.874.87 8.478.47

The effect of δ\delta is most visible for functions whose Jacobian matrix is frequently close to 00. Not surprisingly, the error estimate EδE^{*}_{\delta} tends to be better than EδE_{\delta} since both the training of J^\widehat{J} and the error estimate EδE^{*}_{\delta} are based on linear approximations, albeit at different pairs of points. Note that the error gets worse as the dimension dd of the domain increases.

The rather large error E0.01E^{*}_{0.01} for F11F_{11} and F12F_{12} is a consequence of the large volume of the domains and small validation set XVX_{V}. Increasing the size of XVX_{V} from 10410^{4} (the default) to 10510^{5} for F11F_{11} (resp. F12F_{12}) with N=106N=10^{6} improves E0.01E^{*}_{0.01} from 10.310.3 (resp. 8.478.47) to 3.923.92 (resp. 3.533.53).

V-C Parameters and limitations of the algorithm

We conclude this section with somewhat informal comments on the effects of varying the parameters and on the limitations of Algorithm 1. A more thorough discussion will be reported elsewhere.

V-C1 Varying kmaxk_{max} and rmaxr_{max}

Generally speaking, the error improves with smaller values of rmaxr_{max}, provided that the training set is sufficiently dense so that enough neighbors can be found within radius rmaxr_{max} of sample points. The effect of the parameter kmaxk_{max} is more delicate. If the sample set is very dense, larger values of kmaxk_{max} improve the error since more quality training pairs become available. If the sample set is sparse, larger values of kmaxk_{max} (in conjunction with large values of rmaxr_{max}) make the error worse.

Table III lists observed error rates for the function F0F_{0} with a sample set of size N=104N=10^{4} for various values of kmaxk_{max} and rmaxr_{max}. The first line with kmax=30k_{max}=30 and rmax=0.5r_{max}=0.5 is repeated from Table II as a baseline; it turns out that in this particular example the pairs of nearby points are more restricted by kmax=30k_{max}=30 than by rmax=0.5r_{max}=0.5. In the second line, when kmaxk_{max} is relaxed to 100100, the algorithm picks up too many distant pairs of points and the error rate gets worse. In the third line, with kmax=100k_{max}=100 and rmax=0.1r_{max}=0.1, the number of kept nearby pairs of points drops by approximately 80%80\% compared to the second line, only about 1919 nearby points are retained on average for every sample point (well below kmax=100k_{max}=100), and the error rate improves slightly on the baseline. Finally, with kmax=10k_{max}=10 and rmax=0.5r_{max}=0.5, the number of kept training pairs drops by approximately 90%90\% compared with the second line and the error rate improves further.

TABLE III: The effect of kmaxk_{max} and rmaxr_{max} for F0F_{0} and N=104N=10^{4}.
kmaxk_{max} rmaxr_{max} E0E_{0} E0.001E_{0.001} E0.01E_{0.01} E0.1E_{0.1} E0.01E^{*}_{0.01}
3030 0.50.5 5.785.78 5.785.78 5.225.22 3.373.37 2.732.73
100100 0.50.5 10.410.4 9.549.54 8.778.77 5.355.35 5.525.52
100100 0.10.1 5.305.30 5.305.30 4.734.73 2.922.92 1.141.14
1010 0.50.5 4.974.97 4.964.96 4.454.45 2.922.92 0.610.61

V-C2 Varying the geometry of the neural network

We do not understand well the effect of the geometry of the neural network on Algorithm 1. Table IV reports the error rate E0.1E_{0.1} for the function F0F_{0}, sample set of size N=104N=10^{4} and the default parameters kmax=30k_{max}=30 and rmax=0.5r_{max}=0.5. The first line of Table IV is again taken from Table II as a baseline.

TABLE IV: The effect of neural net geometry for F0F_{0} and N=104N=10^{4}.
layers and their size E0.1E_{0.1}
100, 100, 50, 20 3.373.37
1000, 100, 50, 20 4.484.48
1000, 1000, 50, 20 3.623.62
100, 1000, 100 2.832.83
50, 50 2.232.23
8 layers of 20 neurons 3.273.27
16 layers of 20 neurons 5.505.50

V-C3 Non-differentiable functions and singularities

Although we have assumed throughout that the function FF is differentiable for the purposes of being able to compare its Jacobian matrix JJ with the Jacobian estimator J^\widehat{J}, Algorithm 1 will happily train J^\widehat{J} from a sample set {(x,F(x)):xX}\{(x,F(x)):x\in X\} for any function FF, differentiable or not. In fact, FF need not be even defined outside of XX.

It is to be expected that J^\widehat{J} will not be a good approximation of JJ if JJ oscillates wildly relative to the density of the sample set or if it attains a very large range of values, for example due to the presence of a singularity inside or just outside of the considered domain.

Concerning singularities, we observed the following results for J^\widehat{J} trained on a sample set with N=104N=10^{4} points. For F(x,y)=(x+y)1/2F(x,y)=(x+y)^{1/2} on (0,1)2(0,1)^{2}, the Jacobian matrix has a singularity at (0,0)(0,0), that is, at a “corner” of the domain of FF, nevertheless the error estimates are very satisfactory: E0=0.94E_{0}=0.94 and E0.01=0.02E^{*}_{0.01}=0.02. For F(x,y)=x2+y2F(x,y)=\sqrt{x^{2}+y^{2}} on (1,1)2(-1,1)^{2}, there is a JJ-singularity at (0,0)(0,0), that is, inside the domain of FF, and the error estimates are E0.1=23.6E_{0.1}=23.6 and E0.01=0.3E^{*}_{0.01}=0.3. Finally, the function F(x,y)=|x|+|y|F(x,y)=|x|+|y| is not differentiable along the coordinate axes, but on (1,1)2(-1,1)^{2} we still get E0=3.9E_{0}=3.9 and E0.01=0.4E^{*}_{0.01}=0.4.

The low values of EδE^{*}_{\delta} indicate that J^\widehat{J} does not seem to be capable of detecting/suggesting singularities for truly unknown functions (for which the EδE_{\delta} error estimates are not available).

V-C4 Convexity

If the function FF is scalar valued, then convexity of the function FF has an effect on the error estimate. If the function FF is convex (resp. concave) near aa, the estimator J^\widehat{J} tends to return a value J^(a)\widehat{J}(a) that is smaller (resp. larger) in norm than it should be. To see this, consider the concave function depicted in Figure 2 and suppose that J^(a)\widehat{J}(a) is such that the green secant line intersects the line x=bx=b somewhere between the red tangent line and the blue graph of FF. Then the value J^(a)\widehat{J}(a) should be increased in order to get closer to the tangent line, but the still positive loss function will have the opposite effect.

V-C5 Noisy sampling data

Algorithm 1 may be used on noisy training data. By training J^\widehat{J} on the sample set {(x,F(x)+ϵx):xX}\{(x,F(x)+\epsilon_{x}):x\in X\}, where {ϵx:xX}\{\epsilon_{x}:x\in X\} is a family of independent Gaussian random variables with mean 00 and standard deviation 0.010.01, we observed that J^\widehat{J} still approximates JJ rather well, but certainly not as well as in noiseless situations. For instance, for the function F1F_{1} of Table I and with N=105N=10^{5} sample points we observed the average relative error of E0.01=8.06E_{0.01}=8.06 percent, for F4F_{4} and N=104N=10^{4} we observed E0.01=9.74E_{0.01}=9.74, and for F8F_{8} and N=105N=10^{5} we observed E0.01=10.5E_{0.01}=10.5. The negative effect of noise on regression in general was discussed in the introduction and it persists in the context of Algorithm 1.

V-C6 The running time

The running time of the algorithm increases with the size NN of the sample set and with the parameter kmaxk_{max}. Using kmax=50k_{max}=50, the observed running time of Algorithm 1 on a PC with Intel Core i7 9th generation 3GHz processor was 0.60.6 sec/epoch for N=103N=10^{3}, 44 sec/epoch for N=104N=10^{4}, 4040 sec/epoch for N=105N=10^{5} and 250250 sec/epoch for N=106N=10^{6}.

VI Convergence of the Jacobian matrix estimator: A formal proof

In this section we prove that under reasonable assumptions on the function FF, the neural network, the training set and the outcome of the training in Algorihtm 1, the resulting Jacobian matrix estimator J^\widehat{J} converges in norm to the Jacobian matrix JJ of FF.

We will be more careful with vectors from now on and write them as column vectors. As above, the Euclidean norm of x=(x1,,xd)dx=(x_{1},\dots,x_{d})^{\top}\in\mathbb{R}^{d} will be denoted by

xd=(i=1dxi2)1/2.\left\|{x}\right\|_{\mathbb{R}^{d}}=\left(\sum_{i=1}^{d}x_{i}^{2}\right)^{1/2}.

The dot product of xx, ydy\in\mathbb{R}^{d} will be denoted by

x,y=i=1dxiyi,\langle x,y\rangle=\sum_{i=1}^{d}x_{i}y_{i},

so that xd=x,x1/2\left\|{x}\right\|_{\mathbb{R}^{d}}=\langle x,x\rangle^{1/2}. Finally, for an n×mn\times m matrix AA, the operator norm will be denoted by

An×m=sup{Axn:xm,xm1}.\left\|{A}\right\|_{n\times m}=\mathrm{sup}\{\left\|{Ax}\right\|_{\mathbb{R}_{n}}:x\in\mathbb{R}^{m},\,\left\|{x}\right\|_{\mathbb{R}^{m}}\leq 1\}.

VI-A The assumptions

Let us fix ε>0\varepsilon>0 throughout. The first assumption states that the second partial derivatives of FF are bounded:

Assumption 1.

Let UU be an open bounded subset of d\mathbb{R}^{d}. Let F=(F1,,Fc):UcF=(F_{1},\dots,F_{c}):U\to\mathbb{R}^{c} be a twice differentiable function and let H=(Hi)i=1cH=(H_{i})_{i=1}^{c},

Hi=(2Fixjxk)j,k.H_{i}=\left(\frac{\partial^{2}F_{i}}{\partial x_{j}\partial x_{k}}\right)_{j,k}.

There is a constant L>0L>0 such that Hid×dL\left\|{H_{i}}\right\|_{d\times d}\leq L for every 1ic1\leq i\leq c.

The second assumption states that the neural network trained by Algorithm 1 is Lipschitz. This can be achieved by fixing the geometry of the neural network (number of layers and neurons in each layer), by using activation functions that are Lipschitz, and by bounding above the weights of the neural network, for instance.

Assumption 2.

There is a constant L>0L^{\prime}>0 such that every neural network J^\widehat{J} trained by Algorithm 1 satisfies

J^(y)J^(x)c×dLyxd\left\|{\widehat{J}(y)-\widehat{J}(x)}\right\|_{c\times d}\leq L^{\prime}\left\|{y-x}\right\|_{\mathbb{R}^{d}}

for every x,yUx,y\in U.

The third assumption states, roughly speaking, that the (domain of) the sample set is sufficiently dense and that near every sample point we can find dd additional sample points such that any two of the dd resulting vectors are close to being orthogonal.

Assumption 3.

Let {(x,F(x)):xX}\{(x,F(x)):x\in X\} be the sample set. Then:

  1. (i)

    XX is ε\varepsilon-dense in UU, that is, for every yUy\in U there is xXx\in X such that xydε\left\|{x-y}\right\|_{\mathbb{R}^{d}}\leq\varepsilon,

  2. (ii)

    there exist a constant 0<α<10<\alpha<1 and a constant R>0R>0 such that for all xXx\in X, if UxU_{x} is the set of the kmaxk_{max} nearest neighbors of xx within distance RεR\varepsilon of xx, then there are points ux,1,,ux,dUxu_{x,1},\dots,u_{x,d}\in U_{x} such that

    |ux,ix,ux,jx|ux,ixdux,jxdαd\frac{\left|{\left<{u_{x,i}-x},{u_{x,j}-x}\right>}\right|}{\left\|{u_{x,i}-x}\right\|_{\mathbb{R}^{d}}\left\|{u_{x,j}-x}\right\|_{\mathbb{R}^{d}}}\leqslant\frac{\alpha}{d}

    for every 1i<jd1\leq i<j\leq d.

Remark VI.1.

The constant RR in Assumption 3 can be shown to always exist, for any α(0,1)\alpha\in(0,1) and dd, as long as UU has large enough diameter and kmax=|X|k_{max}=|X|. However, in general, kmaxk_{max} is much smaller than |X||X|, so Assumption 3(ii) is not implied by (i) in general.

The final assumption states that Algorithm 1 produces a neural network for which the loss (used in training) is under control. This should be seen as a relative assumption on the capability of neural networks.

Assumption 4.

After training J^\widehat{J} by Algorithm 1 with rmaxRεr_{max}\geqslant R\varepsilon, we have

F(y)F(x)J^(x)(yx)cyxdε\frac{\left\|{F(y)-F(x)-\widehat{J}(x)(y-x)}\right\|_{\mathbb{R}^{c}}}{\left\|{y-x}\right\|_{\mathbb{R}^{d}}}\leq\varepsilon

for every xXx\in X and yUxy\in U_{x}, where XX and UxU_{x} are as in Assumption 3.

VI-B A proof of convergence

We will need the following result in the proof of the main theorem.

Lemma VI.2.

Let 0<α<10<\alpha<1 and let B={b1,,bd}B=\{b_{1},\dots,b_{d}\} be a set of unit vectors in d\mathbb{R}^{d} such that |bi,bj|α/d|\langle b_{i},b_{j}\rangle|\leq\alpha/d for every 1i<jd1\leq i<j\leq d. Then BB is a basis of d\mathbb{R}^{d} and if y=i=1dyibiy=\sum_{i=1}^{d}y_{i}b_{i} is any vector with yd1\left\|{y}\right\|_{\mathbb{R}_{d}}\leq 1 then

|yi|(1α)1/2|y_{i}|\leq(1-\alpha)^{-1/2} (VI.1)

for every 1id1\leq i\leq d.

Proof.

Let us view BB as a matrix with columns b1,,bdb_{1},\dots,b_{d} and let G=BBG=B^{\top}B be the (symmetric) Gram matrix of BB. Since all the vectors bib_{i} are of unit length and |bi,bj|α/d|\langle b_{i},b_{j}\rangle|\leq\alpha/d for every 1i<jd1\leq i<j\leq d, we have G=I+MG=I+M for some matrix MM such that the absolute value of every entry of MM is at most α/d\alpha/d. Hence Md×dα<1\left\|{M}\right\|_{d\times d}\leq\alpha<1, GG is invertible and thus also BB is invertible.

Let yy be a vector in d\mathbb{R}^{d} such that yd1\left\|{y}\right\|_{\mathbb{R}^{d}}\leq 1. Since BB is a basis, we can write y=i=1dyibiy=\sum_{i=1}^{d}y_{i}b_{i}, i.e., y=B(y1,,yd)y=B(y_{1},\dots,y_{d})^{\top}.

Recall the Neumann series [4, VII, Corollary 2.3]

(IT)1=k=1Tk(I-T)^{-1}=\sum_{k=1}^{\infty}T^{k}

for a bounded linear operator TT. Since G=I+M=I(M)G=I+M=I-(-M) and Md×d=Md×d\left\|{M}\right\|_{d\times d}=\left\|{-M}\right\|_{d\times d}, we deduce

G1d×dk=0(Md×d)kk=0αk=(1α)1.\left\|{G^{-1}}\right\|_{d\times d}\leqslant\sum_{k=0}^{\infty}(\left\|{M}\right\|_{d\times d})^{k}\leq\sum_{k=0}^{\infty}\alpha^{k}=(1-\alpha)^{-1}.

Then

G1/2d×d(1α)1/2\left\|{G^{-1/2}}\right\|_{d\times d}\leq(1-\alpha)^{-1/2} (VI.2)

by the spectral mapping theorem [4], as GG is positive. (This can also be seen by noting that G1G^{-1} is symmetric, so its operator norm is the absolute value of its largest eigenvalue, whose square root is then the largest eigenvalue, hence the norm, of the symmetric matrix G1/2G^{-1/2}.)

Let E=BG1/2E=BG^{-1/2} and note that we have EE=(BG1/2)BG1/2=G1/2BBG1/2=G1/2GG1/2=IE^{\top}E=(BG^{-1/2})^{\top}BG^{-1/2}=G^{-1/2}B^{\top}BG^{-1/2}=G^{-1/2}GG^{-1/2}=I. Hence the columns e1,,ede_{1},\ldots,e_{d} of EE form an orthonormal basis for d\mathbb{R}^{d}.

Consider the vector

z=(e1,y,,ed,y).z=({\left<{e_{1}},{y}\right>},\dots,{\left<{e_{d}},{y}\right>})^{\top}.

Since EE is an orthonormal basis, we have zd=yd1\left\|{z}\right\|_{\mathbb{R}^{d}}=\left\|{y}\right\|_{\mathbb{R}^{d}}\leq 1 and y=Ez=BG1/2zy=Ez=BG^{-1/2}z. Recalling y=B(y1,,yd)y=B(y_{1},\dots,y_{d})^{\top}, we deduce (y1,,yd)=G1/2z(y_{1},\dots,y_{d})^{\top}=G^{-1/2}z. Therefore

(y1,,yd)dG1/2d×dzd(1α)1/2\left\|{(y_{1},\dots,y_{d})}\right\|_{\mathbb{R}^{d}}\leq\left\|{G^{-1/2}}\right\|_{d\times d}\left\|{z}\right\|_{\mathbb{R}^{d}}\leq(1-\alpha)^{-1/2}

thanks to (VI.2) and zd1\left\|{z}\right\|_{\mathbb{R}_{d}}\leq 1. This means that

i=1dyi2(1α)1\sum_{i=1}^{d}y_{i}^{2}\leq(1-\alpha)^{-1}

and (VI.1) follows. ∎

We are now ready to state an prove the main result.

Theorem VI.3.

Let ε>0\varepsilon>0, UdU\subseteq\mathbb{R}^{d}, F:UcF:U\to\mathbb{R}^{c} and let JJ be the Jacobian matrix of FF. Suppose that Assumptions 14 are satisfied for FF and for the trained Jacobian matrix estimator J^\widehat{J}. Then

supxUJ^(x)J(x)c×dCε,\sup_{x\in U}\left\|{\widehat{J}(x)-J(x)}\right\|_{c\times d}\leq C\varepsilon,

where

C=(L+L)+d(1α)1/2(1+LR2).C=(L+L^{\prime})+\frac{d}{(1-\alpha)^{1/2}}\left(1+\frac{LR}{2}\right).
Proof.

Since the Hessian of FF is bounded above by LL by Assumption 1, a higher order Taylor expansion for FF yields

F(y)F(x)J(x)(yx)cL2xyd2\left\|{F(y)-F(x)-J(x)(y-x)}\right\|_{\mathbb{R}^{c}}\leqslant\frac{L}{2}\left\|{x-y}\right\|_{\mathbb{R}^{d}}^{2}

for all x,yUx,y\in U. Using this inequality, Assumption 4 and the triangular inequality, we have

(J^(x)J(x))(yx)c\displaystyle\left\|{(\widehat{J}(x)-J(x))(y-x)}\right\|_{\mathbb{R}^{c}}
J^(x)(yx)(F(y)F(x))c\displaystyle\quad\leqslant\left\|{\widehat{J}(x)(y-x)-(F(y)-F(x))}\right\|_{\mathbb{R}^{c}}
+F(y)F(x)J(x)(yx)c\displaystyle\quad\quad+\left\|{F(y)-F(x)-J(x)(y-x)}\right\|_{\mathbb{R}^{c}} (VI.3)
εyxd+L2yxd2.\displaystyle\quad\leqslant\varepsilon\left\|{y-x}\right\|_{\mathbb{R}^{d}}+\frac{L}{2}\left\|{y-x}\right\|_{\mathbb{R}^{d}}^{2}.

Assumption 1 implies that JJ is LL-Lipschitz, i.e.,

J(x)J(y)c×dLxyd\left\|{J(x)-J(y)}\right\|_{c\times d}\leqslant L\left\|{x-y}\right\|_{\mathbb{R}^{d}}

for every x,yUx,y\in U. By Assumption 2, J^\widehat{J} is LL^{\prime}-Lipschitz. Therefore

(J^(x)J(x))yc\displaystyle\left\|{(\widehat{J}(x)-J(x))y}\right\|_{\mathbb{R}^{c}}
(J^(x0)J(x0))yc\displaystyle\quad\leqslant\left\|{(\widehat{J}(x_{0})-J(x_{0}))y}\right\|_{\mathbb{R}^{c}} (VI.4)
+(L+L)x0xdyd\displaystyle\quad\quad+(L^{\prime}+L)\left\|{x_{0}-x}\right\|_{\mathbb{R}^{d}}\left\|{y}\right\|_{\mathbb{R}^{d}}

for every x,y,x0Ux,y,x_{0}\in U.

For the rest of the proof, fix xUx\in U and let yy be any vector with yd1\left\|{y}\right\|_{\mathbb{R}^{d}}\leq 1. By Assumption 4(i), there exists x0Xx_{0}\in X such that xx0dε\left\|{x-x_{0}}\right\|_{\mathbb{R}^{d}}\leq\varepsilon. By Assumption 4(ii), there exist points ui=ux0,iXu_{i}=u_{x_{0},i}\in X within RεR\varepsilon of x0x_{0} such that the unit vectors

bi=uix0uix0db_{i}=\frac{u_{i}-x_{0}}{\left\|{u_{i}-x_{0}}\right\|_{\mathbb{R}^{d}}}

satisfy |bi,bj|α/d|{\left<{b_{i}},{b_{j}}\right>}|\leq\alpha/d for every 1i<jd1\leq i<j\leq d. By Lemma VI.2, B={b1,,bd}B=\{b_{1},\dots,b_{d}\} is a basis of d\mathbb{R}^{d} and y=i=1dyibiy=\sum_{i=1}^{d}y_{i}b_{i} for some y1,,ydy_{1},\dots,y_{d}\in\mathbb{R} such that |yi|(1α)1/2|y_{i}|\leq(1-\alpha)^{-1/2} for every 1id1\leq i\leq d.

Using (VI.3) in the first inequality below,

(J^(x0)J(x0))bic\displaystyle\left\|{(\widehat{J}(x_{0})-J(x_{0}))b_{i}}\right\|_{\mathbb{R}^{c}}
=(J^(x0)J(x0))(uix0)cuix0d\displaystyle\quad=\frac{\left\|{(\widehat{J}(x_{0})-J(x_{0}))(u_{i}-x_{0})}\right\|_{\mathbb{R}^{c}}}{\left\|{u_{i}-x_{0}}\right\|_{\mathbb{R}^{d}}}
εuix0d+L2uix0d2uix0d\displaystyle\quad\leq\frac{\varepsilon\left\|{u_{i}-x_{0}}\right\|_{\mathbb{R}^{d}}+\frac{L}{2}\left\|{u_{i}-x_{0}}\right\|_{\mathbb{R}^{d}}^{2}}{\left\|{u_{i}-x_{0}}\right\|_{\mathbb{R}^{d}}}
=ε+L2uix0cε+LRε2=ε(1+LR2).\displaystyle\quad=\varepsilon+\frac{L}{2}\left\|{u_{i}-x_{0}}\right\|_{\mathbb{R}^{c}}\leq\varepsilon+\frac{LR\varepsilon}{2}=\varepsilon\left(1+\frac{LR}{2}\right).

We therefore have

(J^(x0)J(x0))yc\displaystyle\left\|{(\widehat{J}(x_{0})-J(x_{0}))y}\right\|_{\mathbb{R}^{c}}
=(J^(x0)J(x0))i=1dyibic\displaystyle\quad=\left\|{(\widehat{J}(x_{0})-J(x_{0}))\sum_{i=1}^{d}y_{i}b_{i}}\right\|_{\mathbb{R}^{c}}
i=1d|yi|(J^(x0)J(x0))bic\displaystyle\quad\leq\sum_{i=1}^{d}|y_{i}|\left\|{(\widehat{J}(x_{0})-J(x_{0}))b_{i}}\right\|_{\mathbb{R}^{c}}
i=1d(1α)1/2(J^(x0)J(x0))bic\displaystyle\quad\leq\sum_{i=1}^{d}(1-\alpha)^{-1/2}\left\|{(\widehat{J}(x_{0})-J(x_{0}))b_{i}}\right\|_{\mathbb{R}^{c}}
εd(1α)1/2(1+LR2).\displaystyle\quad\leq\varepsilon\frac{d}{(1-\alpha)^{1/2}}\left(1+\frac{LR}{2}\right).

The inequality (VI.4) then yields

(J^(x)J(x))yc\displaystyle\left\|{(\widehat{J}(x)-J(x))y}\right\|_{\mathbb{R}^{c}}
(J^(x0)J(x0))yc+ε(L+L)\displaystyle\quad\leq\left\|{(\widehat{J}(x_{0})-J(x_{0}))y}\right\|_{\mathbb{R}^{c}}+\varepsilon(L^{\prime}+L)
ε((L+L)+d(1α)1/2(1+LR2)),\displaystyle\quad\leq\varepsilon\left((L+L^{\prime})+\frac{d}{(1-\alpha)^{1/2}}\left(1+\frac{LR}{2}\right)\right),

finishing the proof. ∎

The estimate in Theorem VI.3 does not depend on the dimension cc of the codomain of FF, which reflects the fact that computing the gradient of each coordinate of FF does not affect the computation of the gradient of the other coordinates. Of course, the estimate gets worse as the dimension dd of the domain grows larger.

VII Conclusion and future work

We introduced a novel algorithm for the estimation of the Jacobian matrix of an unknown, sampled multivariable function by means of neural networks. The main ideas of the algorithm are a loss function based on a linear approximation and a nearest neighbor search in the sample data. The algorithm was tested on a variety of functions and for various sizes of sample sets. The typical average relative error is on the order of single percents, using both an error estimate for functions with a known Jacobian matrix and an error estimate for unknown functions based on linear approximations. We proved that the estimated Jacobian matrix converges to the Jacobian matrix under reasonable assumptions on the function, the sampling set and the loss function.

In future work, we will apply Algorithm 1 for validation of physics-informed models, anomaly detection and time series analysis. For physics-informed models, a typical restriction is of the form F/x>0\partial F/\partial x>0, which can be verified or refuted by the Jacobian matrix estimator J^\widehat{J} trained by Algorithm 1. In anomaly detection, the Jacobian estimator can be retrained periodically on a window of data and deviations in the values of J^\widehat{J} can be statistically detected (and, in addition, the reason for the anomaly can be narrowed down by focusing on anomalous values Fi/xj\partial F_{i}/\partial x_{j}). In a time series, the time parameter can be treated as another variable (preferably modulo a fixed period of time to allow for nearby points in the sample set) or the time parameter can be suppressed by turning the time series S(t1)=(a1,,an)S(t_{1})=(a_{1},\dots,a_{n}), S(t2)=(b1,,bn)S(t_{2})=(b_{1},\dots,b_{n}), etc, into a dynamical system F(a1,,an)=(b1,,bn)F(a_{1},\dots,a_{n})=(b_{1},\dots,b_{n}), etc.

Acknowledgement

The authors acknowledge support from a Lockheed Martin Space Engineering and Technology grant “Time series analysis.”

References

  • [1] Martín Abadi, Ashish Agarwal, Paul Barham, Eugene Brevdo, Zhifeng Chen, Craig Citro, Greg S. Corrado, Andy Davis, Jeffrey Dean, Matthieu Devin, Sanjay Ghemawat, Ian Goodfellow, Andrew Harp, Geoffrey Irving, Michael Isard, Rafal Jozefowicz, Yangqing Jia, Lukasz Kaiser, Manjunath Kudlur, Josh Levenberg, Dan Mané, Mike Schuster, Rajat Monga, Sherry Moore, Derek Murray, Chris Olah, Jonathon Shlens, Benoit Steiner, Ilya Sutskever, Kunal Talwar, Paul Tucker, Vincent Vanhoucke, Vijay Vasudevan, Fernanda Viégas, Oriol Vinyals, Pete Warden, Martin Wattenberg, Martin Wicke, Yuan Yu and Xiaoqiang Zheng, TensorFlow: Large-scale machine learning on heterogeneous systems, 2015. Software available from tensorflow.org.
  • [2] J. Berner, D. Elbrächter, P. Grohs and A. Jentzen, Towards a regularity theory for ReLU networks – chain rule and global error estimates, 2019 13th International conference on Sampling Theory and Applications (SampTA), 2019, pp. 1–5, doi: 10.1109/SampTA45681.2019.9031005.
  • [3] Arthur Bryson, A gradient method for optimizing multi-stage allocation processes, Proceedings of the Harvard Univ. Symposium on digital computers and their applications, pp. 3–6, April 1961. Harvard University Press.
  • [4] John Conway, A course in functional analysis, 2nd edition. Graduate Texts in Mathematics, 96. Springer-Verlag, New York, 1990, xvi+399.
  • [5] Stuart Dreyfus, The numerical solution of variational problems, Journal of Mathematical Analysis and Applications, 5 (1), pp. 30–45. doi:10.1016/0022-247x(62)90004-5
  • [6] Michael C. Fu, Gradient Estimation, in Handbooks in Operations Research and Management Science, vol. 13 “Simulation”, pp. 575–616, North-Holland, 2006.
  • [7] Kunihiko Fukushima, Visual feature extraction by a multilayered network of analog threshold elements, IEEE Transactions on Systems Science and Cybernetics 5 (4) (1969), pp. 322–333. doi:10.1109/TSSC.1969.300225.
  • [8] C.R. Harris, K.J. Millman, S.J. van der Walt et al, Array programming with NumPy, Nature 585 (2020), pp. 357–362. DOI: 10.1038/s41586-020-2649-2.
  • [9] Xing He, Lei Chu, Robert Qiu, Qian Ai and Wentao Huang, Data-driven Estimation of the Power Flow Jacobian Matrix in High Dimensional Space, arxiv.org/abs/1902.06211
  • [10] Henry Kelley, Gradient theory of optimal flight paths, ARS Journal. 30 (10), pp. 947–954. doi:10.2514/8.5282
  • [11] I.E. Lagaris, A. Likas and D.I. Fotiadis, Artificial neural networks for solving ordinary and partial differential equations, in IEEE Transactions on Neural Networks, vol. 9, no. 5, pp. 987–1000, Sept. 1998, doi: 10.1109/72.712178.
  • [12] Dougal Maclaurin, David Duvenaud and Ryan P. Adams, Autograd: Effortless gradients in numpy, in ICML 2015 AutoML Workshop 238 (2015), 5 pages.
  • [13] K. Rudd, G.D. Muro and S. Ferrari, A Constrained Backpropagation Approach for the Adaptive Solution of Partial Differential Equations, in IEEE Transactions on Neural Networks and Learning Systems, vol. 25, no.3, pp. 571–584, March 2014, doi: 10.1109/TNNLS.2013.2277601.
  • [14] Walter Rudin, Principles of mathematical analysis, 3rd edition, International Series in Pure and Applied Mathematics. McGraw-Hill Book Co., New York-Auckland-Düsseldorf, 1976, x+342 pp.
  • [15] David Rumelhart, Geoffrey Hinton, Ronald Williams, 8. Learning Internal Representations by Error Propagation in Rumelhart, David E.; McClelland, James L. (eds.). Parallel Distributed Processing : Explorations in the Microstructure of Cognition. Vol. 1 : Foundations. MIT Press. ISBN 0-262-18120-7.
  • [16] Carl Runge, Über empirische Funktionen und die Interpolation zwischen äquidistanten Ordinaten, Zeitschrift für Mathematik und Physik 46 (1901), pp. 224–243.
  • [17] Guanhua Wang and Jeffrey A. Fessler, Efficient approximation of Jacobian matrices involving a non-uniform fast Fourier transform (NUFFT), arxiv.org/abs/2111.02912.

Appendix: The code for JacobianEstimator

# Imported modules
import math
import numpy as np
from sklearn.neighbors import NearestNeighbors
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import adam_v2
from tensorflow.keras.constraints import max_norm
################################
## Definition of the class Jhat
##
## Purpose:
## Estimate the Jacobian of F : I -> O from a finite sample of values (x,F(x)).
## The function F is considered unknown outside of the sample set.
## I is a subset of R^domdim and O is a subset of R^codomdim.
##
## Usage:
## 1. Create a Jhat object. This process does not involve the data set,
## but it does involve the dimensions of the data set. This process creates
## a simple ANN using Dense layers.
## 2. Fit the Jhat object to the sample data. The data comes as an array of
## input values (x) and a corresponding array of output values (fx).
## Formally, x and fx have respective ”shapes” (N,domdim) and (N,codomdim),
## where N is the number of sample points. We understand df[j] as F(x[j]).
## 3. Use the functions predict1 (for one input value) or predict (for a list
## of input values) to obtain the estimate for the Jacobian of F.
##
## Syntax:
##
## constructor:
## Jhat(layers,domdim,codomdim,nbr,r_max,batch_size,epochs,learning_rate,verbose)
## - layers (opt): an array of integers with the number of neurons per layer.
## The resulting ANN will have two extra layers (input and output).
## Default value is [100,100,50,20].
## - domdim (opt): dimension of the domain of F. Default is 2.
## - codomdim (opt): dimension of the codomain of F. Default to 1.
## - nbr (opt): max number of nearest neighbors to use for the estimation.
## Default is twice domdim. Should be at least domdim.
## - r_max: if not 0, only keeps neighbors within r_max. Default is 0.
## - batch_size (opt): batch size for the gradient method. Default to 50.
## - epochs (opt): number of iterations over training set. Default is 50.
## - learning_rate (opt): learning rate for the gradient method.
## - max_w (opt): maximum norm of the weights (no constraint if 0)
## - verbose (opt): text notification during training. Default is True.
##
## fit: fit(self,x,fx)
## - x: array of N input values, each an array of domdim numbers
## - fx: array of N output values, each an array of codomdim numbers
## returns self
##
## predict1: predict1(self,x)
## - x: an array of domdim numbers
## returns the predicted value of the Jacobian at x
##
## predict: predict(self,x)
## - x: an array of N input values, each an array of domdim numbers
## returns array of predicted values of the Jacobian at each input value of x
class Jhat:
# constructor
def __init__(self, domdim = 2, codomdim = 2, layers = [100,100,50,20], batch_size = 50, epochs = 50, nbr = 0, r_max = 0.0, learning_rate = 0.0001, max_w=0,verbose=True):
# store parameters
self.domdim = domdim
self.codomdim = codomdim
self.layers = layers
self.batch_size = batch_size if batch_size > 0 else 50
self.epochs = epochs if epochs > 0 else 50
self.nbr = nbr if nbr >= domdim else 2*domdim
self.r_max = r_max
self.max_norm = max_w
self.verbose = verbose
# create and store ANN
self.model = Sequential()
# input layer
self.model.add(Dense(layers[0],input_shape=(domdim,),activation=’swish’,kernel_constraint=max_norm(max_w) if max_w>0 else None))
# hidden layers
for n_neurons in layers[1:]:
self.model.add(Dense(n_neurons,activation=’swish’,kernel_constraint=max_norm(max_w) if max_w>0 else None))
# output layer
self.model.add(Dense(domdim * codomdim,kernel_constraint=max_norm(100.) if max_w>0 else None))
self.model.compile(loss=self.createLoss(),optimizer=adam_v2.Adam(learning_rate=learning_rate))
# internal: loss function generator
# The core of the entire process, this produces a closure used as the loss
# function to train the network from the data cloud to estimate the Jacobian
def createLoss(self):
def loss(real,predict):
dx=real[:,0:self.domdim]
df=real[:,self.domdim:]
return tf.math.reduce_mean(tf.math.square(tf.math.subtract(df,tf.linalg.matvec(tf.reshape(predict,(self.batch_size,self.codomdim,self.domdim)),dx))))
return loss
# internal: prepare the data set for training.
# For a given pair (x,F(x)) in the submitted sample:
# 1. find self.nbr closest neighbors x_1,..,x_nbr to x.
# 2. compute and store (x-x_1, F(x)-F(x_1)), (x-x_2,F(x)-F(x_2)),
# 3. return the array obtained by processing each sample point.
def prepareData(self,x,fx,train_mode=True,zero=0):
if self.verbose :
print(”Preparingdatafromsample”)
print(”Inputshape,x.shape)
print(”Outputshape,fx.shape)
nbrs = NearestNeighbors(n_neighbors=self.nbr,algorithm=’ball_tree’).fit(x)
dist, indices = nbrs.kneighbors(x)
if self.verbose:
print(”Minimaldistance:”,np.amin(dist))
print(”Averagedistance:”,np.average(dist))
print(”Maximaldistance:”,np.amax(dist))
if self.r_max==0:
self.r_max = np.amax(dist)+1.0
N = x.shape[0] * self.nbr
pos = np.empty( (N,self.domdim) )
delta = np.empty( (N,self.domdim+self.codomdim) )
reservedx = np.empty( (N,self.domdim) )
reservefx = np.empty( (N,self.codomdim) )
k = 0
if train_mode:
for idx in indices[:,0]:
for idx2 in indices[idx,1:]:
dx = np.subtract(x[idx2],x[idx])
dxnorm = np.linalg.norm(dx)
if dxnorm < self.r_max and dxnorm>zero:
pos[k] = x[idx]
delta[k,0:self.domdim] = dx/dxnorm
delta[k,self.domdim:] = np.subtract(fx[idx2],fx[idx])/dxnorm
k = k + 1
else:
for idx in indices[:,0]:
for idx2 in indices[idx,1:]:
dx = np.subtract(x[idx2],x[idx])
dxnorm = np.linalg.norm(dx)
div = np.linalg.norm(fx[idx2])
if dxnorm < self.r_max and dxnorm>0 and div>zero:
pos[k] = x[idx]
delta[k,0:self.domdim] = dx/div
delta[k,self.domdim:] = np.subtract(fx[idx2],fx[idx])/div
k = k + 1
if k < self.batch_size:
self.batch_size = k
else:
r = k % self.batch_size
if r != 0:
d = self.batch_size - r
for j in range(d):
pos[k+j] = pos[0]
delta[k+j] = delta[0]
k += d
if self.verbose:
print(”Numberoftrainingdatapoints:, k)
print(”Finalizedbatchsize:, self.batch_size)
# shuffle
randind = np.arange(k)
np.random.shuffle(randind)
pos = pos[:k]
delta = delta[:k]
pos = [ pos[j] for j in randind ]
delta = [ delta[j] for j in randind ]
return (tf.convert_to_tensor(pos[:k]), tf.convert_to_tensor(delta[:k]))
# fit: train the ANN with a sample set, passed as a parameter.
def fit(self,x,fx):
F = self.prepareData(x,fx)
self.model.fit(F[0],F[1], epochs=self.epochs, batch_size=self.batch_size,verbose=2 if self.verbose else 0)
return self
# predict: compute the estimate of the Jacobian of F at each entry of x.
def predict(self,x):
N = x.shape[0]
return self.model.predict(x).reshape(N,self.codomdim,self.domdim)
# predict1: compute the estimate of the Jacobian of F at a single input x.
def predict1(self,x):
return self.model.predict([x])
# predictflat: same as predict, but does not reshape output
def predictflat(self,x):
return self.model.predict(x)
# validation functions
def tangentl(self,j,dx,N):
return tf.linalg.matvec(tf.reshape(j,(N,self.codomdim,self.domdim)),dx)
def validate(self,x,fx,zero=0):
F = self.prepareData(x,fx,train_mode=False,zero=zero)
dfhat = tf.convert_to_tensor(self.predict(F[0]))
dx=tf.cast(F[1][:,0:self.domdim],dtype=np.float32)
df=tf.cast(F[1][:,self.domdim:],dtype=np.float32)
N=df.shape[0]
return tf.subtract(df,self.tangentl(dfhat,dx,N))