otf.optim
Optimization utilities and implementations used by the package.
This package exposes optimizers, learning-rate schedulers and gradient computers
used for parameter estimation when assimilating data into BaseSystem
instances.
Modules:
| Name | Description |
|---|---|
base |
Optimization utilities for parameter estimation during data assimilation. |
gradient |
|
lr_scheduler |
Learning-rate schedulers for optimizers. |
optimizer |
Optimization algorithms for estimating unknown system parameters. |
Classes:
| Name | Description |
|---|---|
AdjointGradient |
Compute parameter gradients using adjoint-based methods. |
DummyLRScheduler |
No-op scheduler for testing and compatibility. |
DummyOptimizer |
Optimizer that performs no parameter updates (useful for testing). |
ExponentialLR |
Multiply an optimizer's learning rate by a constant factor on each |
GradientDescent |
Simple gradient-descent optimizer. |
LevenbergMarquardt |
Levenberg–Marquardt optimizer using sensitivity-based gradients. |
MultiStepLR |
Reduce learning rate at specified step milestones. |
OptaxWrapper |
Adapter that wraps an Optax optimizer as a |
OptimizerChain |
Combine several optimizers by summing their weighted updates. |
PartialOptimizer |
Wrap an optimizer to update only a subset of parameters. |
Regularizer |
Produce parameter-space penalties used as a regularization term. |
SensitivityGradient |
Compute gradients using sensitivity (forward) equations. |
WeightedLevenbergMarquardt |
Weighted Levenberg–Marquardt optimizer (Gauss–Newton variant). |
Functions:
| Name | Description |
|---|---|
pruned_factory |
Return a 'pruned' variant of |
AdjointGradient
AdjointGradient(
system: BaseSystem,
update_option: UpdateOption = UpdateOption.asymptotic,
solver: tuple[type[SinglestepSolver | MultistepSolver]]
| type[SinglestepSolver | MultistepSolver]
| None = None,
dt: float | None = None,
interval_fraction: float = 1,
)
Bases: GradientComputer
Compute parameter gradients using adjoint-based methods.
Supports different update strategies controlled by UpdateOption.
Initialize an AdjointGradient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
BaseSystem
|
|
required |
update_option
|
UpdateOption
|
Which adjoint/update method to use ( |
asymptotic
|
solver
|
tuple[type[SinglestepSolver | MultistepSolver]] | type[SinglestepSolver | MultistepSolver] | None
|
Solver class or tuple of solver classes used when simulation-based
adjoint computation is selected ( |
None
|
dt
|
float | None
|
Time-step used with the solver (required when |
None
|
interval_fraction
|
float
|
Fraction of the input time series to use for gradient computation (value in (0, 1]). |
1
|
Source code in src/otf/optim/gradient/adjoint.py
DummyLRScheduler
Bases: LRScheduler
No-op scheduler for testing and compatibility.
Initialize a dummy scheduler (accepts arbitrary args).
This scheduler performs no action when step is called.
Source code in src/otf/optim/lr_scheduler.py
DummyOptimizer
Bases: BaseOptimizer
Optimizer that performs no parameter updates (useful for testing).
Initialize the dummy optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
BaseSystem
|
Target |
required |
gradient_computer
|
GradientComputer | None
|
Optional |
None
|
Source code in src/otf/optim/optimizer.py
ExponentialLR
Bases: LRScheduler
Multiply an optimizer's learning rate by a constant factor on each
step() call.
Initialize the exponential scheduler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
BaseOptimizer
|
An instance of |
required |
gamma
|
float
|
Factor to multiply the learning rate by on each |
0.99
|
Source code in src/otf/optim/lr_scheduler.py
GradientDescent
GradientDescent(
system: BaseSystem,
learning_rate: float = 0.0001,
gradient_computer: GradientComputer | None = None,
)
Bases: BaseOptimizer
Simple gradient-descent optimizer.
Create a gradient-descent optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Scalar learning rate used to scale the negative gradient. |
0.0001
|
Source code in src/otf/optim/optimizer.py
LevenbergMarquardt
LevenbergMarquardt(
system: BaseSystem,
learning_rate: float = 0.001,
lam: float = 0.01,
gradient_computer: SensitivityGradient | None = None,
)
Bases: BaseOptimizer
Levenberg–Marquardt optimizer using sensitivity-based gradients.
Levenberg–Marquardt optimizer using sensitivity-based gradients.
This implementation requires a SensitivityGradient instance and is
currently implemented only for the UpdateOption.last_state update
method of the gradient computer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Scalar multiplier applied to the computed step. |
0.001
|
lam
|
float
|
Levenberg–Marquardt damping parameter. |
0.01
|
Source code in src/otf/optim/optimizer.py
MultiStepLR
Bases: LRScheduler
Reduce learning rate at specified step milestones.
Initialize the multi-step scheduler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
BaseOptimizer
|
An instance of |
required |
milestones
|
list[int] | tuple[int]
|
For each milestone, update the learning rate after that many calls
to |
required |
gamma
|
float
|
Factor by which to multiply the learning rate at each milestone. |
0.5
|
Source code in src/otf/optim/lr_scheduler.py
OptaxWrapper
OptaxWrapper(
system: BaseSystem,
optimizer: GradientTransformationExtraArgs,
gradient_computer: GradientComputer | None = None,
)
Bases: BaseOptimizer
Adapter that wraps an Optax optimizer as a BaseOptimizer.
Wrap a given Optax optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
GradientTransformationExtraArgs
|
Instance of |
required |
Source code in src/otf/optim/optimizer.py
OptimizerChain
OptimizerChain(
system: BaseSystem,
learning_rate: float,
optimizers: list[BaseOptimizer],
weights: list[float],
)
Bases: BaseOptimizer
Combine several optimizers by summing their weighted updates.
Initialize an OptimizerChain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Scalar applied to the total weighted update (controls overall step size). |
required |
optimizers
|
list[BaseOptimizer]
|
Sequence of |
required |
weights
|
list[float]
|
Sequence of relative weights (one per optimizer). These are normalized internally so their sum is one. |
required |
Notes
It can be convenient to set each individual optimizer's internal
learning rate to 1.0 and control the combined step size with
learning_rate supplied here.
Source code in src/otf/optim/base.py
PartialOptimizer
Bases: BaseOptimizer
Wrap an optimizer to update only a subset of parameters.
The wrapped optimizer computes a full update vector; PartialOptimizer
masks that update so only selected indices in system.cs are changed.
Initialize the wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
BaseOptimizer
|
Base optimizer used to compute the full parameter update. |
required |
param_idx
|
jndarray | None
|
Indices of |
None
|
Methods:
| Name | Description |
|---|---|
__getattr__ |
For attributes that aren't defined in this class, route access to the |
__setattr__ |
For attributes that aren't defined in this class, route access to the |
Source code in src/otf/optim/base.py
__getattr__
For attributes that aren't defined in this class, route access to the wrapped optimizer.
__setattr__
For attributes that aren't defined in this class, route access to the wrapped optimizer.
Source code in src/otf/optim/base.py
Regularizer
Regularizer(
system: BaseSystem,
ord: int | float | Callable,
prior: jndarray | None = None,
callable_is_derivative: bool | None = None,
)
Bases: BaseOptimizer
Produce parameter-space penalties used as a regularization term.
The step method returns the negative derivative of the regularization
penalty (same shape as system.cs) and can be combined with a
gradient-based optimizer.
Initialize a Regularizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ord
|
int | float | Callable
|
If an int/float, interpreted as the |
required |
prior
|
jndarray | None
|
Optional prior parameter vector; penalty is applied to |
None
|
callable_is_derivative
|
bool | None
|
If |
None
|
Methods:
| Name | Description |
|---|---|
step |
Return the negative derivative of the regularization penalty. |
Source code in src/otf/optim/base.py
step
Return the negative derivative of the regularization penalty.
The returned array has the same shape as system.cs and can be added to
a gradient-based update.
Source code in src/otf/optim/base.py
SensitivityGradient
SensitivityGradient(
system: BaseSystem,
update_option: UpdateOption = UpdateOption.last_state,
solver: tuple[type[SinglestepSolver | MultistepSolver]]
| type[SinglestepSolver | MultistepSolver]
| None = None,
dt: float | None = None,
use_unobserved_asymptotics: bool = False,
)
Bases: GradientComputer
Compute gradients using sensitivity (forward) equations.
Different UpdateOptions select how the sensitivity information is
assembled (last state, mean state, mean gradient, or complete simulation).
Initialize a SensitivityGradient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
BaseSystem
|
|
required |
update_option
|
UpdateOption
|
Strategy for forming the gradient ( |
last_state
|
solver
|
tuple[type[SinglestepSolver | MultistepSolver]] | type[SinglestepSolver | MultistepSolver] | None
|
Solver class or tuple of solver classes used when the |
None
|
dt
|
float | None
|
Time-step used with the solver (required when |
None
|
use_unobserved_asymptotics
|
bool
|
When True attempt to use asymptotic information from unobserved state components (experimental). |
False
|
Source code in src/otf/optim/gradient/sensitivity.py
WeightedLevenbergMarquardt
WeightedLevenbergMarquardt(
system: BaseSystem,
learning_rate: float = 0.001,
lam: float = 0.01,
gradient_computer: GradientComputer | None = None,
)
Bases: BaseOptimizer
Weighted Levenberg–Marquardt optimizer (Gauss–Newton variant).
Perform a weighted version of the Levenberg–Marquardt modification of Gauss–Newton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
The learning rate (scalar by which to multiply the step) |
0.001
|
lam
|
float
|
Levenberg–Marquardt parameter |
0.01
|
Source code in src/otf/optim/optimizer.py
pruned_factory
Return a 'pruned' variant of system_type.
If a parameter in cs of the system is to be set below its corresponding
threshold (in absolute value), it will be set to zero permanently.
Optionally require that this occur at least a specified number of times
consecutively before setting a parameter to zero permanently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_type
|
type[BaseSystem]
|
The type of |
required |
Source code in src/otf/optim/base.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |