Optimizer¶
Optimizer Factory¶
mindcv.optim.optim_factory.create_optimizer(model_or_params, opt='adam', lr=0.001, weight_decay=0, momentum=0.9, nesterov=False, weight_decay_filter='disable', layer_decay=None, loss_scale=1.0, schedule_decay=0.004, checkpoint_path='', eps=1e-10, **kwargs)
¶
Creates optimizer by name.
PARAMETER | DESCRIPTION |
---|---|
model_or_params |
network or network parameters. Union[list[Parameter],list[dict], nn.Cell], which must be the list of parameters or list of dicts or nn.Cell. When the list element is a dictionary, the key of the dictionary can be "params", "lr", "weight_decay","grad_centralization" and "order_params".
|
opt |
wrapped optimizer. You could choose like 'sgd', 'nesterov', 'momentum', 'adam', 'adamw', 'lion', 'rmsprop', 'adagrad', 'lamb'. 'adam' is the default choose for convolution-based networks. 'adamw' is recommended for ViT-based networks. Default: 'adam'.
TYPE:
|
lr |
learning rate: float or lr scheduler. Fixed and dynamic learning rate are supported. Default: 1e-3.
TYPE:
|
weight_decay |
weight decay factor. It should be noted that weight decay can be a constant value or a Cell. It is a Cell only when dynamic weight decay is applied. Dynamic weight decay is similar to dynamic learning rate, users need to customize a weight decay schedule only with global step as input, and during training, the optimizer calls the instance of WeightDecaySchedule to get the weight decay value of current step. Default: 0.
TYPE:
|
momentum |
momentum if the optimizer supports. Default: 0.9.
TYPE:
|
nesterov |
Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. Default: False.
TYPE:
|
weight_decay_filter |
filters to filter parameters from weight_decay. - "disable": No parameters to filter. - "auto": We do not apply weight decay filtering to any parameters. However, MindSpore currently automatically filters the parameters of Norm layer from weight decay. - "norm_and_bias": Filter the paramters of Norm layer and Bias from weight decay.
TYPE:
|
layer_decay |
for apply layer-wise learning rate decay.
TYPE:
|
loss_scale |
A floating point value for the loss scale, which must be larger than 0.0. Default: 1.0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optimizer object |
Source code in mindcv/optim/optim_factory.py
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 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 302 303 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 |
|
AdamW¶
mindcv.optim.adamw.AdamW
¶
Bases: Optimizer
Implements the gradient clipping by norm for a AdamWeightDecay optimizer.
Source code in mindcv/optim/adamw.py
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 225 226 227 228 229 230 231 232 233 |
|
mindcv.optim.adamw.AdamW.get_lr()
¶
The optimizer calls this interface to get the learning rate for the current step. User-defined optimizers based
on :class:mindspore.nn.Optimizer
can also call this interface before updating the parameters.
RETURNS | DESCRIPTION |
---|---|
float, the learning rate of current step. |
Source code in mindcv/optim/adamw.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
Adan¶
mindcv.optim.adan.Adan
¶
Bases: Optimizer
The Adan (ADAptive Nesterov momentum algorithm) Optimizer from https://arxiv.org/abs/2208.06677
Note: it is an experimental version.
Source code in mindcv/optim/adan.py
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 |
|
mindcv.optim.adan.Adan.get_lr()
¶
The optimizer calls this interface to get the learning rate for the current step. User-defined optimizers based
on :class:mindspore.nn.Optimizer
can also call this interface before updating the parameters.
RETURNS | DESCRIPTION |
---|---|
float, the learning rate of current step. |
Source code in mindcv/optim/adan.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
mindcv.optim.adan.Adan.target(value)
¶
If the input value is set to "CPU", the parameters will be updated on the host using the Fused optimizer operation.
Source code in mindcv/optim/adan.py
199 200 201 202 203 204 205 |
|
Lion¶
mindcv.optim.lion.Lion
¶
Bases: Optimizer
Implementation of Lion optimizer from paper 'https://arxiv.org/abs/2302.06675'. Additionally, this implementation is with gradient clipping.
Notes: lr is usually 3-10x smaller than adamw. weight decay is usually 3-10x larger than adamw.
Source code in mindcv/optim/lion.py
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 |
|
mindcv.optim.lion.Lion.get_lr()
¶
The optimizer calls this interface to get the learning rate for the current step. User-defined optimizers based
on :class:mindspore.nn.Optimizer
can also call this interface before updating the parameters.
RETURNS | DESCRIPTION |
---|---|
float, the learning rate of current step. |
Source code in mindcv/optim/lion.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
NAdam¶
mindcv.optim.nadam.NAdam
¶
Bases: Optimizer
Implements NAdam algorithm (a variant of Adam based on Nesterov momentum).
Source code in mindcv/optim/nadam.py
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 |
|
mindcv.optim.nadam.NAdam.get_lr()
¶
The optimizer calls this interface to get the learning rate for the current step. User-defined optimizers based
on :class:mindspore.nn.Optimizer
can also call this interface before updating the parameters.
RETURNS | DESCRIPTION |
---|---|
float, the learning rate of current step. |
Source code in mindcv/optim/nadam.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|