Step size¶
This subpackage contains step size selection methods like Barzilai-Borwein and Polyak's step size.
See also¶
- Line search - line search methods.
- Trust region - trust region methods.
Classes:
-
AdGD
–AdGD and AdGD-2 (https://arxiv.org/abs/2308.02261)
-
BBStab
–Stabilized Barzilai-Borwein method (https://arxiv.org/abs/1907.06409).
-
BarzilaiBorwein
–Barzilai-Borwein step size method.
-
LR
–Learning rate. Adding this module also adds support for LR schedulers.
-
PolyakStepSize
–Polyak's subgradient method with known or unknown f*.
-
RandomStepSize
–Uses random global or layer-wise step size from
low
tohigh
. -
StepSize
–this is exactly the same as LR, except the
lr
parameter can be renamed to any other name to avoid clashes -
Warmup
–Learning rate warmup, linearly increases learning rate multiplier from :code:
start_lr
to :code:end_lr
over :code:steps
steps. -
WarmupNormClip
–Warmup via clipping of the update norm.
AdGD ¶
Bases: torchzero.core.transform.Transform
AdGD and AdGD-2 (https://arxiv.org/abs/2308.02261)
Source code in torchzero/modules/step_size/adaptive.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 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 |
|
BBStab ¶
Bases: torchzero.core.transform.Transform
Stabilized Barzilai-Borwein method (https://arxiv.org/abs/1907.06409).
This clips the norm of the Barzilai-Borwein update by delta
, where delta
can be adaptive if c
is specified.
Parameters:
-
c
(float
, default:0.2
) –adaptive delta parameter. If
delta
is set to None, firstinf_iters
updates are performed with non-stabilized Barzilai-Borwein step size. Then delta is set to norm of the update that had the smallest norm, and multiplied byc
. Defaults to 0.2. -
delta
(float | None
, default:None
) –Barzilai-Borwein update is clipped to this value. Set to
None
to use an adaptive choice. Defaults to None. -
type
(str
, default:'geom'
) –one of "short" with formula sᵀy/yᵀy, "long" with formula sᵀs/sᵀy, or "geom" to use geometric mean of short and long. Defaults to "geom". Note that "long" corresponds to BB1stab and "short" to BB2stab, however I found that "geom" works really well.
-
inner
(Chainable | None
, default:None
) –step size will be applied to outputs of this module. Defaults to None.
Source code in torchzero/modules/step_size/adaptive.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
BarzilaiBorwein ¶
Bases: torchzero.core.transform.Transform
Barzilai-Borwein step size method.
Parameters:
-
type
(str
, default:'geom'
) –one of "short" with formula sᵀy/yᵀy, "long" with formula sᵀs/sᵀy, or "geom" to use geometric mean of short and long. Defaults to "geom".
-
fallback
(float
) –step size when denominator is less than 0 (will happen on negative curvature). Defaults to 1e-3.
-
inner
(Chainable | None
, default:None
) –step size will be applied to outputs of this module. Defaults to None.
Source code in torchzero/modules/step_size/adaptive.py
LR ¶
Bases: torchzero.core.transform.Transform
Learning rate. Adding this module also adds support for LR schedulers.
Source code in torchzero/modules/step_size/lr.py
PolyakStepSize ¶
Bases: torchzero.core.transform.Transform
Polyak's subgradient method with known or unknown f*.
Parameters:
-
f_star
(float | Mone
, default:0
) –minimal possible value of the objective function. If not known, set to
None
. Defaults to 0. -
y
(float
, default:1
) –when
f_star
is set to None, it is calculated asf_best - y
. -
y_decay
(float
, default:0.001
) –y
is multiplied by(1 - y_decay)
after each step. Defaults to 1e-3. -
max
(float | None
, default:None
) –maximum possible step size. Defaults to None.
-
use_grad
(bool
, default:True
) –if True, uses dot product of update and gradient to compute the step size. Otherwise, dot product of update with itself is used.
-
alpha
(float
, default:1
) –multiplier to Polyak step-size. Defaults to 1.
Source code in torchzero/modules/step_size/adaptive.py
RandomStepSize ¶
Bases: torchzero.core.transform.Transform
Uses random global or layer-wise step size from low
to high
.
Parameters:
-
low
(float
, default:0
) –minimum learning rate. Defaults to 0.
-
high
(float
, default:1
) –maximum learning rate. Defaults to 1.
-
parameterwise
(bool
, default:False
) –if True, generate random step size for each parameter separately, if False generate one global random step size. Defaults to False.
Source code in torchzero/modules/step_size/lr.py
StepSize ¶
Bases: torchzero.core.transform.Transform
this is exactly the same as LR, except the lr
parameter can be renamed to any other name to avoid clashes
Source code in torchzero/modules/step_size/lr.py
Warmup ¶
Bases: torchzero.core.transform.Transform
Learning rate warmup, linearly increases learning rate multiplier from :code:start_lr
to :code:end_lr
over :code:steps
steps.
Parameters:
-
steps
(int
, default:100
) –number of steps to perform warmup for. Defaults to 100.
-
start_lr
(_type_
, default:1e-05
) –initial learning rate multiplier on first step. Defaults to 1e-5.
-
end_lr
(float
, default:1
) –learning rate multiplier at the end and after warmup. Defaults to 1.
Example
Adam with 1000 steps warmup
.. code-block:: python
opt = tz.Modular(
model.parameters(),
tz.m.Adam(),
tz.m.LR(1e-2),
tz.m.Warmup(steps=1000)
)
Source code in torchzero/modules/step_size/lr.py
WarmupNormClip ¶
Bases: torchzero.core.transform.Transform
Warmup via clipping of the update norm.
Parameters:
-
start_norm
(_type_
, default:1e-05
) –maximal norm on the first step. Defaults to 1e-5.
-
end_norm
(float
, default:1
) –maximal norm on the last step. After that, norm clipping is disabled. Defaults to 1.
-
steps
(int
, default:100
) –number of steps to perform warmup for. Defaults to 100.
Example
Adam with 1000 steps norm clip warmup
.. code-block:: python
opt = tz.Modular(
model.parameters(),
tz.m.Adam(),
tz.m.WarmupNormClip(steps=1000)
tz.m.LR(1e-2),
)