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
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
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
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 | |
is_valid_parameters(parameters)
Check where the parameters are valid.
Source code in pytorch_optimizer/optimizer/utils.py
160 161 162 | |
has_overflow(grad_norm)
Detect inf and NaN in grad_norm.
Source code in pytorch_optimizer/optimizer/utils.py
165 166 167 | |
to_real(x)
Return real value of tensor.
Source code in pytorch_optimizer/optimizer/utils.py
170 171 172 | |
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
175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
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
|
PARAMETERS
|
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
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 225 226 227 228 229 | |
unit_norm(x, norm=2.0)
Get norm of unit.
Source code in pytorch_optimizer/optimizer/utils.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
disable_running_stats(model)
Disable running stats (momentum) of BatchNorm.
Source code in pytorch_optimizer/optimizer/utils.py
250 251 252 253 254 255 256 257 258 | |
enable_running_stats(model)
Enable running stats (momentum) of BatchNorm.
Source code in pytorch_optimizer/optimizer/utils.py
261 262 263 264 265 266 267 268 | |
get_global_gradient_norm(param_groups)
Get global gradient norm.
Source code in pytorch_optimizer/optimizer/utils.py
271 272 273 274 275 276 277 278 279 280 281 | |
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
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
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
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |