Solvers
All solvers in nano-optax are pure functions. Each solver takes an
objective f(params, *data) and returns an OptResult with final parameters,
final objective value, and a per-epoch trace.
If you want to use a stateful schedule, pass a schedule function with
signature (step, state) -> (lr, new_state) and provide schedule_state.
Gradient Descent
gd
gd(
fun: Callable[..., Array],
init_params: PyTree,
data: tuple = (),
*,
lr: LearningRate = 0.001,
max_epochs: int = 100,
tol: float = 1e-06,
schedule_state: ScheduleState | None = None,
verbose: bool = False,
) -> OptResult
Run vanilla gradient descent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable[..., Array]
|
Objective function |
required |
init_params
|
PyTree
|
Initial parameters (PyTree). |
required |
data
|
tuple
|
Tuple of data arrays. |
()
|
lr
|
LearningRate
|
Learning rate (constant, schedule, or stateful schedule). |
0.001
|
max_epochs
|
int
|
Number of epochs to run. |
100
|
tol
|
float
|
Convergence tolerance on gradient norm. |
1e-06
|
schedule_state
|
ScheduleState | None
|
Optional initial state for a stateful schedule. |
None
|
verbose
|
bool
|
Print progress during optimization. |
False
|
Returns:
| Type | Description |
|---|---|
OptResult
|
OptResult with final parameters, value, and trace. |
Source code in src/nano_optax/gd.py
Stochastic Gradient Descent
sgd
sgd(
fun: Callable[..., Array],
init_params: PyTree,
data: tuple[Array, ...],
*,
lr: LearningRate = 0.001,
max_epochs: int = 100,
batch_size: int | None = 1,
key: Array | None = None,
schedule_state: ScheduleState | None = None,
verbose: bool = False,
) -> OptResult
Run stochastic gradient descent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable[..., Array]
|
Objective function |
required |
init_params
|
PyTree
|
Initial parameters (PyTree). |
required |
data
|
tuple[Array, ...]
|
Tuple of data arrays, sliced along axis 0. |
required |
lr
|
LearningRate
|
Learning rate (constant, schedule, or stateful schedule). |
0.001
|
max_epochs
|
int
|
Number of epochs to run. |
100
|
batch_size
|
int | None
|
Minibatch size (None uses full batch). |
1
|
key
|
Array | None
|
PRNGKey for shuffling (None disables shuffling). |
None
|
schedule_state
|
ScheduleState | None
|
Optional initial state for a stateful schedule. |
None
|
verbose
|
bool
|
Print progress during optimization. |
False
|
Returns:
| Type | Description |
|---|---|
OptResult
|
OptResult with final parameters, value, and trace. |
Source code in src/nano_optax/sgd.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
Proximal Gradient Descent
prox_gd
prox_gd(
fun: Callable[..., Array],
g: Callable[[PyTree], Array],
prox: Callable[[Array, Array], Array],
init_params: PyTree,
data: tuple = (),
*,
lr: LearningRate = 0.001,
max_epochs: int = 100,
tol: float = 1e-06,
schedule_state: ScheduleState | None = None,
verbose: bool = False,
) -> OptResult
Run proximal gradient descent for objectives of the form \(f\) + \(g\), where \(f\) is \(L\)-smooth and convex, and \(g\) is (possibly nonsmooth) proper, l.s.c., and convex. The proximal operator for \(g\) must be passed via the prox argument as an uncurried map with signature \((x,\eta)\mapsto \operatorname{prox}_{\eta g}(x)\). At iteration \(t\), the algorithm does a:
1. (Gradient step): \(y_{t} := x_{t-1} - \eta_{t}\nabla f(x_{t-1})\), and
2. (Proximal step) \(x_{t}:=\operatorname{prox}_{\eta_{t} g}(y_{t})\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable[..., Array]
|
Smooth function |
required |
g
|
Callable[[PyTree], Array]
|
Nonsmooth function |
required |
prox
|
Callable[[Array, Array], Array]
|
Proximal operator |
required |
init_params
|
PyTree
|
Initial parameters (PyTree). |
required |
data
|
tuple
|
Tuple of data arrays. |
()
|
lr
|
LearningRate
|
Learning rate (constant, schedule, or stateful schedule). |
0.001
|
max_epochs
|
int
|
Number of epochs to run. |
100
|
tol
|
float
|
Convergence tolerance on gradient mapping norm. |
1e-06
|
schedule_state
|
ScheduleState | None
|
Optional initial state for a stateful schedule. |
None
|
verbose
|
bool
|
Print progress during optimization. |
False
|
Returns:
| Type | Description |
|---|---|
OptResult
|
OptResult with final parameters, value, and trace. |
Source code in src/nano_optax/prox_gd.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
Proximal operators
prox_gd expects an uncurried prox operator \((x,\eta)\mapsto \operatorname{prox}_{\eta g}(x)\).
Two helpers are included:
prox_l1
Return the L1 proximal operator (soft-thresholding) as an uncurried map \((x,\eta)\mapsto \operatorname{prox}_{\eta \| \cdot \|_1}(x)\).
Source code in src/nano_optax/prox_gd.py
prox_l2
Return the squared-L2 norm's proximal operator as an uncurried map \((x,\eta)\mapsto \operatorname{prox}_{\eta \| \cdot \|_{2}^{2}}(x)\).
Source code in src/nano_optax/prox_gd.py
Accelerated Proximal Gradient Descent (FISTA)
apgd
apgd(
fun: Callable[..., Array],
g: Callable[[PyTree], Array],
prox: Callable[[Array, Array], Array],
init_params: PyTree,
data: tuple[Array, ...],
*,
lr: LearningRate = 0.001,
max_epochs: int = 100,
batch_size: int | None = None,
key: Array | None = None,
tol: float = 1e-06,
schedule_state: ScheduleState | None = None,
verbose: bool = False,
) -> OptResult
Run accelerated proximal gradient descent (FISTA).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable[..., Array]
|
Smooth function |
required |
g
|
Callable[[PyTree], Array]
|
Nonsmooth function |
required |
prox
|
Callable[[Array, Array], Array]
|
Proximal operator |
required |
init_params
|
PyTree
|
Initial parameters (PyTree). |
required |
data
|
tuple[Array, ...]
|
Tuple of data arrays, sliced along axis 0. |
required |
lr
|
LearningRate
|
Learning rate (constant, schedule, or stateful schedule). |
0.001
|
max_epochs
|
int
|
Number of epochs to run. |
100
|
batch_size
|
int | None
|
Minibatch size (None uses full batch). |
None
|
key
|
Array | None
|
PRNGKey for shuffling (None disables shuffling). |
None
|
tol
|
float
|
Convergence tolerance on gradient mapping norm. |
1e-06
|
schedule_state
|
ScheduleState | None
|
Optional initial state for a stateful schedule. |
None
|
verbose
|
bool
|
Print progress during optimization. |
False
|
Returns:
| Type | Description |
|---|---|
OptResult
|
OptResult with final parameters, value, and trace. |
Source code in src/nano_optax/apgd.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |