Skip to content

scirex.training

Optimisers that handle complex-valued weights.

from scirex.training import adamw_cx

Use adamw_cx instead of optax.adamw when the model has complex weights (FNO and GINO do; WNO has real weights only). Plain AdamW treats a complex weight incorrectly. The training loop is experimental: see scirex.experimental.training.

Optimisers

scirex.training.optim also exports scale_by_adam_cx, the gradient transformation adamw_cx is built from.

adamw_cx

adamw_cx(
    learning_rate: float,
    weight_decay: float = 0.0,
    b1: float = 0.9,
    b2: float = 0.999,
    eps: float = 1e-06,
) -> optax.GradientTransformation

AdamW that handles complex-valued parameters, as an Optax transformation.

Compatible with optax.inject_hyperparams. For complex parameters the second moment uses |g|**2 (real), as PyTorch does; see scale_by_adam_cx. Weight decay is decoupled from the Adam update.

Args:

Name Type Description Default
learning_rate float

Step size.

weight_decay float

Decoupled weight-decay coefficient, by default 0.0.

0.0
b1 float

Exponential decay rate of the first moment, by default 0.9.

0.9
b2 float

Exponential decay rate of the second moment, by default 0.999.

0.999
eps float

Term added to the denominator for numerical stability, by default 1e-6.

1e-06

Returns:

Type Description
GradientTransformation

The chained Adam scaling, weight decay and learning-rate scaling.

Examples:

>>> import jax.numpy as jnp
>>> import optax
>>> params = {"w": jnp.ones(3, dtype=jnp.complex64)}
>>> opt = adamw_cx(learning_rate=1e-3)
>>> state = opt.init(params)
>>> grads = {"w": jnp.ones(3, dtype=jnp.complex64)}
>>> updates, state = opt.update(grads, state, params)
>>> params = optax.apply_updates(params, updates)
>>> params["w"].dtype
dtype('complex64')

scale_by_adam_cx

scale_by_adam_cx(
    b1: float = 0.9, b2: float = 0.999, eps: float = 1e-06
) -> optax.GradientTransformation

Adam scale transform that uses |g|² for complex params (matches PyTorch).

JAX jax.grad returns conj(PyTorch_grad) for complex parameters (Wirtinger convention difference). We conjugate complex gradients here so that the first moment accumulates in the same direction as PyTorch's optimizer, and weight decay shrinks (rather than grows) the imaginary component. |g|² is unchanged by conjugation so nu is not affected.