Utilization
get_supported_optimizers(filters=None)
Return list of available optimizer names, sorted alphabetically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
Optional[Union[str, List[str]]]
|
Optional[Union[str, List[str]]]. wildcard filter string that works with fmatch. if None, it will return the whole list. |
None
|
Source code in pytorch_optimizer/optimizer/__init__.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | |
get_supported_lr_schedulers(filters=None)
Return list of available lr scheduler names, sorted alphabetically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
Optional[Union[str, List[str]]]
|
Optional[Union[str, List[str]]]. wildcard filter string that works with fmatch. if None, it will return the whole list. |
None
|
Source code in pytorch_optimizer/lr_scheduler/__init__.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
get_supported_loss_functions(filters=None)
Return list of available loss function names, sorted alphabetically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
Optional[Union[str, List[str]]]
|
Optional[Union[str, List[str]]]. wildcard filter string that works with fmatch. if None, it will return the whole list. |
None
|
Source code in pytorch_optimizer/loss/__init__.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
CPUOffloadOptimizer
Offload optimizer to CPU for single-GPU training. This will reduce GPU memory by the size of optimizer state.
Reference: https://github.com/pytorch/ao/blob/main/torchao/prototype/low_bit_optim/cpu_offload.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. a list of parameters or parameter groups. |
required |
optimizer_class
|
Type[Optimizer]
|
Type[torch.optim.Optimizer]. constructor of the base optimizer. Defaults to :class: |
AdamW
|
offload_gradients
|
bool
|
bool. free GPU gradients once they are moved to CPU. Not compatible with gradient accumulation. |
False
|
kwargs
|
other keyword arguments to be passed to the base optimizer e.g. |
{}
|
Source code in pytorch_optimizer/optimizer/utils.py
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 | |
is_valid_parameters(parameters)
Check where the parameters are valid.
Source code in pytorch_optimizer/optimizer/utils.py
213 214 215 | |
has_overflow(grad_norm)
Detect inf and NaN in grad_norm.
Source code in pytorch_optimizer/optimizer/utils.py
218 219 220 | |
to_real(x)
Return real value of tensor.
Source code in pytorch_optimizer/optimizer/utils.py
223 224 225 | |
normalize_gradient(x, use_channels=False, epsilon=1e-08)
Normalize gradient with stddev.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
torch.Tensor. gradient. |
required |
use_channels
|
bool
|
bool. channel-wise normalization. |
False
|
epsilon
|
float
|
float. eps. |
1e-08
|
Source code in pytorch_optimizer/optimizer/utils.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
clip_grad_norm(parameters, max_norm=0.0, sync=False)
Clip gradient norms.
During combination with FSDP, will also ensure that grad norms are aggregated across all workers,
since each worker only stores their shard of the gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Union[PARAMETERS, Tensor]
|
PARAMETERS. Parameters whose gradients we wish to clip. |
required |
max_norm
|
float
|
float. Maximum norm we wish the gradients to have. If non-positive, then we will not perform clipping. |
0.0
|
sync
|
bool
|
bool. Boolean indicating whether we should aggregate across the distributed group. Used only in combination with FSDP. |
False
|
Returns:
| Type | Description |
|---|---|
Union[Tensor, float]
|
The gradient norm across all parameters, before clipping. |
Source code in pytorch_optimizer/optimizer/utils.py
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 | |
unit_norm(x, norm=2.0)
Get norm of unit.
Source code in pytorch_optimizer/optimizer/utils.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
disable_running_stats(model)
Disable running stats (momentum) of BatchNorm.
Source code in pytorch_optimizer/optimizer/utils.py
304 305 306 307 308 309 310 311 312 | |
enable_running_stats(model)
Enable running stats (momentum) of BatchNorm.
Source code in pytorch_optimizer/optimizer/utils.py
315 316 317 318 319 320 321 322 | |
get_global_gradient_norm(param_groups)
Get global gradient norm.
Source code in pytorch_optimizer/optimizer/utils.py
325 326 327 328 329 330 331 332 333 334 335 | |
reg_noise(network1, network2, num_data, lr, eta=0.008, temperature=0.0001)
Entropy-MCMC: Sampling from flat basins with ease.
usage: https://github.com/lblaoke/EMCMC/blob/master/exp/cifar10_emcmc.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network1
|
Module
|
nn.Module. network. |
required |
network2
|
Module
|
nn.Module. network. |
required |
num_data
|
int
|
int. number of training data. |
required |
lr
|
float
|
float. learning rate. |
required |
eta
|
float
|
float. eta. |
0.008
|
temperature
|
float
|
float. temperature. |
0.0001
|
Source code in pytorch_optimizer/optimizer/utils.py
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 | |
copy_stochastic(target, source)
Copy stochastic.
reference: https://github.com/pytorch/pytorch/issues/120376#issuecomment-1974828905
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tensor
|
torch.Tensor. bfloat16 tensor. |
required |
source
|
Tensor
|
torch.Tensor. float32 tensor. |
required |
Source code in pytorch_optimizer/optimizer/utils.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |