Optimization (periodic)¶
vertax.opt
Optimization methods for VertAX.
OptimizationTarget ¶
Bases: Enum
What the minimize function will try to optimize.
recording_t1_statistics ¶
recording_t1_statistics()
Collect the per-iteration T1 counts of every relaxation run inside the block.
Yields a list to which one record is appended per relaxation, in call order: a dict with
counts, an integer array of shape (iterations, 2) holding, per iteration, the edges presented
to the T1 detector (no longer than min_dist_T1, neither cell a triangle) and the flips kept, and
source, the library function that ran the relaxation. With T1 transitions enabled, rejected
flips are presented minus kept, provided the flips of one iteration share no cell (see
minimize, count_t1). Relaxations traced inside a caller's own jit/vmap/grad
cannot be recorded this way: call minimize(..., count_t1=True) there and return the counts.
minimize ¶
minimize(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, L_in: InnerLossFunction, solver: GradientTransformation, min_dist_T1: float, iterations_max: int = 1000, tolerance: float = 0.0001, patience: int = 5, selected_verts: Array | None = None, selected_hes: Array | None = None, selected_faces: Array | None = None, optimization_target: OptimizationTarget = OptimizationTarget.VERTICES, update_t1_func: UpdateT1Func = update_T1, perform_energy_check: bool = False, cap_vertex_step: bool = False, reset_solver_on_t1: bool = False, count_t1: bool = False) -> tuple[tuple[Array, Array, Array, Array, Array, Array], tuple[Array, ...]]
General minimization function.
Supported solvers: any optax.GradientTransformation. Fixed-step solvers (optax.sgd,
optax.adam) use the gradient only. Line-search solvers -- vertax.cg.cg_linesearch
(nonlinear conjugate gradient), vertax.cg.gd_linesearch (steepest descent with a Wolfe line
search) and optax.lbfgs() -- also need the energy and a callable to re-evaluate it, which
this loop passes to solver.update as value, grad and value_fn at every step. Use
cap_vertex_step=True with them, and reset_solver_on_t1=True with optax.lbfgs(),
whose curvature pairs otherwise outlive the topology they were measured on. A solver whose state
holds integers needs a fixed integer dtype under jax_enable_x64 (cg_linesearch and
gd_linesearch do; optax.lbfgs() does).
Build the solver once and pass the same object to every call. The relaxation loop is compiled
for each solver object (the solver is a static argument of the compiled loop, compared by
identity), so a new solver at every call, e.g. minimize(..., optax.sgd(0.01), ...) in a loop,
recompiles at every call and keeps every compiled copy in the jit cache. The same holds for
L_in and update_t1_func, and for the solvers given to the bilevel optimizers. Measured on
a 20-cell periodic mesh, 50 iterations, CPU: 5.7 s for the first call, then 0.15 s per call with
the same solver; 4.7 to 5.0 s per call with a new optax.sgd each time, the cache growing by
one entry per call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cap_vertex_step
|
bool
|
Clip each per-step vertex displacement to MAX_VERTEX_STEP_FACTOR * min_dist_T1. Intended for line-search solvers (cg_linesearch, gd_linesearch, lbfgs), which choose their own step size and can otherwise move a vertex across whole cells in one iteration. Off by default, so fixed-step solvers (adam, sgd) are untouched. |
False
|
reset_solver_on_t1
|
bool
|
Re-initialise the solver state at the new vertex positions after every iteration whose T1 step changed the topology (vertex optimisation only). Off by default. |
False
|
count_t1
|
bool
|
Also return, as a third element of the history, an integer array of shape
|
False
|
inner_opt ¶
inner_opt(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, L_in: InnerLossFunction, solver: GradientTransformation, min_dist_T1: float, iterations_max: int = 1000, tolerance: float = 0.0001, patience: int = 5, selected_verts: Array | None = None, selected_hes: Array | None = None, selected_faces: Array | None = None, update_t1_func: UpdateT1Func = update_T1, perform_energy_check: bool = False, cap_vertex_step: bool = False) -> tuple[tuple[Array, Array, Array], Array]
Inner optimization function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cap_vertex_step
|
bool
|
Clip each per-step vertex displacement to
MAX_VERTEX_STEP_FACTOR * min_dist_T1, as in |
False
|
cost_ad ¶
cost_ad(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, solver_inner: GradientTransformation, min_dist_T1: float, iterations_max: int = 1000, tolerance: float = 0.0001, patience: int = 5, selected_verts: Array | None = None, selected_hes: Array | None = None, selected_faces: Array | None = None, image_target: Array | None = None, update_t1_func: UpdateT1Func = update_T1, return_t1_counts: bool = False, cap_vertex_step: bool = False) -> Array
Automatic differentiation cost function.
With return_t1_counts it returns (cost, (counts, steps)), the T1 counts of the unrolled
relaxation (see minimize(count_t1=True)) as an auxiliary output for grad(..., has_aux=True);
the relaxation is the same as inner_opt's.
outer_opt ¶
outer_opt(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, solver_inner: GradientTransformation, solver_outer: GradientTransformation, min_dist_T1: float, iterations_max: int, tolerance: float, patience: int, selected_verts: Array | None, selected_hes: Array | None, selected_faces: Array | None, image_target: Array | None, update_t1_func: UpdateT1Func = update_T1, params_regularizer: Callable | None = None, cap_vertex_step: bool = False) -> tuple[Array, Array, Array]
Outer optimization for Automatic differentiation method.
inner_eq_prop ¶
inner_eq_prop(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, loss_ep_static: LossEPFunction, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, selected_verts: Array | None, selected_hes: Array | None, selected_faces: Array | None, image_target: Array | None, beta: float, solver: GradientTransformation, min_dist_T1: float, iterations_max: int = 1000, tolerance: float = 0.001, patience: int = 5, update_t1_func: UpdateT1Func = update_T1, perform_energy_check: bool = False, cap_vertex_step: bool = False) -> tuple[tuple[Array, Array, Array], Array]
Wrapper to call the JIT-compiled minimize function and handle post-processing (slicing history).
outer_eq_prop ¶
outer_eq_prop(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, solver_inner: GradientTransformation, solver_outer: GradientTransformation, min_dist_T1: float, iterations_max: int, tolerance: float, patience: int, selected_verts: Array | None, selected_hes: Array | None, selected_faces: Array | None, image_target: Array | None, beta: float, update_t1_func: UpdateT1Func = update_T1, params_regularizer: Callable | None = None, ep_variant: EPVariant = EPVariant.CENTERED, cap_vertex_step: bool = False) -> tuple[Array, Array, Array]
Outer optimization for equilibrium propagation method.
params_regularizer(vert_params, he_params, face_params) -> scalar, if given, has its
gradient added to the equilibrium-propagation gradients before the outer solver's update, so
it is optimized jointly with the outer loss.
The outer-optimizer state is re-initialised every call (current / default behaviour).
For persistent state across epochs use outer_eq_prop_with_state.
outer_eq_prop_with_state ¶
outer_eq_prop_with_state(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, solver_inner: GradientTransformation, solver_outer: GradientTransformation, min_dist_T1: float, iterations_max: int, tolerance: float, patience: int, selected_verts: Array | None, selected_hes: Array | None, selected_faces: Array | None, image_target: Array | None, beta: float, opt_state: OptState | None, update_t1_func: UpdateT1Func = update_T1, params_regularizer: Callable | None = None, ep_variant: EPVariant = EPVariant.CENTERED, cap_vertex_step: bool = False) -> tuple[Array, Array, Array, optax.OptState]
Outer optimization for equilibrium propagation with persistent optimizer state.
Identical to outer_eq_prop except:
- Accepts opt_state: if None, initialises the state (first call semantics);
otherwise uses the supplied state (persistent-Adam / any stateful optax transform).
- Returns (vert_params, he_params, face_params, updated_opt_state) so the caller
can thread the state across epochs.
This function is intended to be called from PbcBilevelOptimizer._outer_opt when
persist_outer_state=True. All other callers should use outer_eq_prop.
outer_implicit ¶
outer_implicit(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, solver_inner: GradientTransformation, solver_outer: GradientTransformation, min_dist_T1: float, iterations_max: int, tolerance: float, patience: int, selected_verts: Array | None, selected_hes: Array | None, selected_faces: Array | None, image_target: Array | None, update_t1_func: UpdateT1Func = update_T1, params_regularizer: Callable | None = None, cap_vertex_step: bool = False) -> tuple[Array, Array, Array]
Outer optimization for implicit differentiation method.
outer_adjoint_state ¶
outer_adjoint_state(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, solver_inner: GradientTransformation, solver_outer: GradientTransformation, min_dist_T1: float, iterations_max: int, tolerance: float, patience: int, selected_verts: Array | None, selected_hes: Array | None, selected_faces: Array | None, image_target: Array | None, update_t1_func: UpdateT1Func = update_T1, params_regularizer: Callable | None = None, cap_vertex_step: bool = False) -> tuple[Array, Array, Array]
Outer optimization for adjoint state method.
bilevel_opt ¶
bilevel_opt(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vert_params: Array, he_params: Array, face_params: Array, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, L_in: InnerLossFunction, L_out: OuterLossFunction, solver_inner: GradientTransformation, solver_outer: GradientTransformation, min_dist_T1: float, iterations_max: int, tolerance: float, patience: int, selected_verts: Array | None, selected_hes: Array | None, selected_faces: Array | None, image_target: Array | None = None, beta: float = 0.01, method: BilevelOptimizationMethod = BilevelOptimizationMethod.AUTOMATIC_DIFFERENTIATION, update_t1_func: UpdateT1Func = update_T1) -> tuple[tuple[Array, Array, Array, Array, Array, Array], Array]
Bilevel optimization for PBC meshes.