symforce.opt.numeric_factor module¶
- class NumericFactor(keys, optimized_keys, linearization_function)[source]¶
Bases:
object
A class used to wrap linearization functions such that they can be used by the optimizer.
- Parameters:
keys (T.Sequence[str]) – The set of keys that are inputs to the linearization function.
optimized_keys (T.Sequence[str]) – A subset of
keys
representing the keys which the given linearization function computes the jacobian with respect to.linearization_function (T.Callable[..., T.Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]]) – A function that returns the residual, jacobian, hessian approximation, and right-hand-side used with the levenberg marquardt optimizer.
- classmethod from_file_python(keys, optimized_keys, output_dir, namespace, name)[source]¶
Returns a NumericFactor constructed from the python function
name
from the module located atoutput_dir / "python" / "symforce" / namespace / f"{name}.py"
(this matches the directory structure created byfactor.Factor.generate()
). This can be used after generating a linearization function from a symbolic factor as follows:Create a symbolic factor and generate the linearization function:
output_dir = "my_output_dir" namespace = "my_namespace" name = "my_custom_factor" sym_factor = Factor( keys=my_keys, residual=my_func, name=name, ) sym_factor.generate(my_optimized_keys, output_dir, namespace)
Load the generated linearization function:
num_factor = NumericFactor.from_file_python( my_keys, my_optimized_keys, output_dir, namespace, name )
- Parameters:
keys (Sequence[str]) – The set of keys that are inputs to the linearization function.
optimized_keys (Sequence[str]) – A subset of
keys
representing the keys which the given linearization function computes the jacobian with respect to.output_dir (str | PathLike) – The top-level output directory of the linearization function.
namespace (str) – The namespace of the linearization function.
name (str) – The name of the linearization function.
- Return type:
- linearize(inputs)[source]¶
Evaluates the linearization function for the given inputs. Returns the residual, jacobian, hessian approximation, and right hand side used with the levenberg marquardt optimizer.
- cc_factor(cc_key_map)[source]¶
Create a C++ Factor from this symbolic Factor, for use with the C++ Optimizer
Note that while this is a C++ Factor object, the linearization function may be a compiled C++ function or a Python function passed into C++ through pybind, depending on the language the linearization function was generated in.
- Parameters:
cc_key_map (Mapping[str, Key]) – Mapping from Python keys (strings, like returned by
Values.keys_recursive
) to C++ keys- Returns:
A C++ wrapped Factor object
- Return type: