TransportMaps.DerivativesChecks
¶
Module Contents¶
Functions¶
|
Compute finite difference of |
|
Check the gradient |
|
Check the correctness of the action \(\langle \nabla^2 f, v \rangle\). |
- TransportMaps.DerivativesChecks.fd(f, x, dx, params=None, fshape=None, newdim=None)[source]¶
Compute finite difference of
f
The function
f
may be any scalar, vector, matrix, tensor etc. valued function \(f:\mathbb{R}^d\rightarrow \mathbb{R}^{n_1 \times \cdot \times n_k}\). If the function \(f\) has \(k\) output dimensions, the result offd()
will be of \(k+1\) dimensions, where the extra dimension will be appended at the position prescribed bynewdim
.- Parameters:
f (
function
) – the function \(f\)x (
ndarray
[\(m,d\)]) – evaluation points. If the function is not vectorized thenx
should be of dimension \(d\).dx (float) – finite difference perturbation
params (dict) – dictionary of parameters
fshape (tuple) – expected shape of the output of \(f\). It should correspond to \(n_1, \ldots, n_k\). If not provided, then it will be inferred by calling
f
.newdim (int) – dimension along which to add the gradient.
- Returns:
ndarray
[\(m,n_1,\ldots,n_{k+1}\)] – gradient of\(f\), where one of the \(n_i\) is \(d\).
- TransportMaps.DerivativesChecks.fd_gradient_check(f, gf, x, dx, rtol=None, atol=None, params=None, fshape=None, newdim=None, verbose=True, title='')[source]¶
Check the gradient
grad_f
using finite difference approximation off
.- Parameters:
f (
function
) – the function \(f\) or \((f, \nabla f)\)gf (
function
) – the funtion \(\nabla f\). IfNone
, then it is assumed thatf
returns the tuple \((f,\nabla f)\).x (
ndarray
[\(m,d\)]) – evaluation points. If the function is not vectorized thenx
should be of dimension \(d\).dx (float) – finite difference perturbation
rtol (float) – relative tolerance (default is
dx
)atol (float) – absolute tolerance (default is
dx
)params (dict) – dictionary of parameters
fshape (tuple) – expected shape of the output of \(f\). It should correspond to \(n_1, \ldots, n_k\). If not provided, then it will be inferred by calling
f
.newdim (int) – dimension along which to add the gradient.
verbose (bool) – whether to output the result of the test
title (str) – if
verbose==True
it is used to identify the gradient in the outputs
- Returns:
bool
– whether the gradient and its finite difference approximationare within the specified error tolerances.
- TransportMaps.DerivativesChecks.action_hess_check(ghf, ahf, x, v, fd_dx=None, rtol=None, atol=None, params=None, fshape=None, newdim=None, verbose=True, title='')[source]¶
Check the correctness of the action \(\langle \nabla^2 f, v \rangle\).
If the perturbation
fd_dx
is provided, thenghf
is assumed to be the gradient \(\nabla f\) and the Hessian \(\nabla^2 f\) is approximated using finite difference. Otherwiseghf
is considered to be the Hessian \(\nabla^2 f\) itself.- Parameters:
ghf (
function
) – the function \(\nabla^2 f\) or \(\nabla f\) of \((f, \nabla f)\). In the latter two cases the Hessian is approximated with finite difference andfd_dx
must be providedahf (
function
) – the funtion \(\langle \nabla^ f, v \rangle\)x (
ndarray
[\(m,d\)]) – evaluation points. If the function is not vectorized thenx
should be of dimension \(d\).v (
ndarray
[\(m,d\)]) – vector \(v\). If the action of the Hessian is not vectorized with respect to the perturbation, thenv
should be of dimension :maht:`d`fd_dx (float) – finite difference perturbation
rtol (float) – relative tolerance (default is
dx
)atol (float) – absolute tolerance (default is
dx
)params (dict) – dictionary of parameters
fshape (tuple) – expected shape of the output of \(f\). It should correspond to \(n_1, \ldots, n_k\). If not provided, then it will be inferred by calling
f
.newdim (int) – dimension along which to add the gradient.
verbose (bool) – whether to output the result of the test
title (str) – if
verbose==True
it is used to identify the gradient in the outputs
- Returns:
bool
– whether the action of the Hessianis within the specified error tolerances.