symnum.diffops.numpy module#
Autograd-style functional differential operators for NumPy functions.
- symnum.diffops.numpy.grad(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate a function to evaluate the gradient of a scalar-valued function.
The passed function should take as arguments NumPy arrays and return a NumPy scalar, and likewise the returned function will take NumPy array arguments.
- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
gradient(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated gradient function returns both the gradient and value of
func
as a 2-tuple (True
) or just the gradient (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated gradient function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ScalarLike]]]
- symnum.diffops.numpy.gradient(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate a function to evaluate the gradient of a scalar-valued function.
The passed function should take as arguments NumPy arrays and return a NumPy scalar, and likewise the returned function will take NumPy array arguments.
- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
gradient(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated gradient function returns both the gradient and value of
func
as a 2-tuple (True
) or just the gradient (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated gradient function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ScalarLike]]]
- symnum.diffops.numpy.hessian(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate a function to evaluate the Hessian of a scalar-valued function.
The passed function should take as arguments NumPy arrays and return a NumPy scalar, and likewise the returned function will take NumPy array arguments.
- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
hessian(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated Hessian function returns the Hessian, gradient and value of
func
as a 3-tuple (True
) or just the Hessian (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated Hessian function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ArrayLike, ScalarLike]]]
- symnum.diffops.numpy.hessian_vector_product(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate an operator to evaluate Hessian-vector-products for a function.
The passed function should take as arguments NumPy arrays and return a NumPy scalar, and likewise the returned operator will take NumPy array arguments.
For a single argument function
func
, n-dimensional input arrayx
and n-dimensional ‘vector’ arrayv
of the same shape asx
then we have the following equivalencehessian_vector_product(func)(x)(v) == tensordot(hessian(func)(x), v, n)
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
hessian_vector_product(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the Hessian-vector-product function, gradient and value of
func
as a 3-tuple (True
) or just the Hessian-vector-product function (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated Hessian-vector-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike, ScalarLike]]]
- symnum.diffops.numpy.jacobian(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate a function to evaluate the Jacobian of a function.
The passed function should take as arguments and return NumPy array(s), and likewise the returned function will take NumPy array arguments.
- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
jacobian(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated Jacobian function returns both the Jacobian and value of
func
as a 2-tuple (True
) or just the Jacobian (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated Jacobian function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ArrayLike]]]
- symnum.diffops.numpy.jacobian_vector_product(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate an operator to evaluate Jacobian-vector-products for a function.
The passed function should take as arguments and return NumPy array(s), and likewise the returned operator will take NumPy array arguments.
For a single argument function
func
, n-dimensional input arrayx
and n-dimensional ‘vector’ arrayv
of the same shape asx
then we have the following equivalencejacobian_vector_product(func)(x)(v) == tensordot(jacobian(func)(x), v, n)
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
jacobian_vector_product(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the Jacobian-vector-product function and value of
func
as a 2-tuple (True
) or just the Jacobian-vector-product function (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated Jacobian-vector-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike]]]
- symnum.diffops.numpy.matrix_hessian_product(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate an operator to evaluate matrix-Hessian-products for a function.
The passed function should take as arguments and return NumPy array(s), and likewise the returned function will act on NumPy arrays.
For a single argument function
func
, n-dimensional input arrayx
and k + n-dimensional ‘matrix’ arraym
with shapefunc(x).shape + x.shape
then we have the following equivalencematrix_hessian_product(func)(x)(m) == ( tensordot(m, jacobian(jacobian(func))(x), k + n) )
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
matrix_hessian_product(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator return the matrix-Hessian-product function, Jacobian and value of
func
as a 3-tuple (True
) or just the matrix-Hessian-product function (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated matrix-Hessian-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike, ArrayLike]]]
- symnum.diffops.numpy.matrix_tressian_product(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate an operator to evaluate matrix-Tressian-products for a function.
The passed function should take as arguments NumPy arrays and return a NumPy scalar, and likewise the returned operator will take NumPy array arguments.
For a single argument function
func
, n-dimensional input arrayx
and (2 * n)-dimensional ‘matrix’ arraym
of shapex.shape + x.shape
then we have the following equivalencematrix_tressian_product(func)(x)(m) == ( tensordot(jacobian(hessian(func))(x), 2 * n) )
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
matrix_tressian_product(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the matrix-Tressian-product function, Hessian, gradient and value of func as a 4-tuple (
True
) or just the matrix-Tressian-product function (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated matrix-Tressian-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike, ArrayLike, ScalarLike]]]
- symnum.diffops.numpy.vector_jacobian_product(func, *arg_shapes, wrt=0, return_aux=False, **kwargs)#
Generate an operator to evaluate vector-Jacobian-products for a function.
The passed function should take as arguments and return NumPy array(s), and likewise the returned function will act on NumPy arrays.
For a single argument function
func
, input arrayx
and n-dimensional ‘vector’ array v of the same shape asfunc(x)
then we have the following equivalencevector_jacobian_product(func)(x)(v) == tensordot(v, jacobian(func)(x), n)
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
*arg_shapes (tuple[int, ...]) – Variable length list of tuples defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form
vector_jacobian_product(func, (2, 2), (2, 4, 3), ...)
.wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the vector-Jacobian-product function and value of
func
as a 2-tuple (True
) or just the vector-Jacobian-product function (False
).**kwargs – Any keyword arguments to the NumPy code generation function
symnum.codegen.generate_func()
.
- Returns:
Generated vector-Jacobian-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike]]]