optimizer
optimizer_handler(optimizer_name, params, lr_mult=1.0, **kwargs)
¶
Returns an instance of the specified optimizer. See Notes below for supported optimizers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimizer_name |
str
|
The name of the optimizer to use. |
required |
params |
list or Tensor
|
The parameters to optimize. |
required |
lr_mult |
float
|
A multiplier for the learning rate. Defaults to 1.0. |
1.0
|
**kwargs |
Any
|
Additional keyword arguments for the optimizer. |
{}
|
Notes
The following optimizers are supported (see also: https://pytorch.org/docs/stable/optim.html#base-class):
* Adadelta
* Adagrad
* Adam
* AdamW
* SparseAdam
* ASGD
* LBFGS
* NAdam
* RAdam
* RMSprop
* Rprop
* SGD
Returns:
Type | Description |
---|---|
Optimizer
|
An instance of the specified optimizer. |
Examples:
>>> from torch.utils.data import DataLoader
from spotpython.data.diabetes import Diabetes
from spotpython.light.netlightregression import NetLightRegression
from torch import nn
import lightning as L
BATCH_SIZE = 8
lr_mult=0.1
dataset = Diabetes()
train_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
test_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
val_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
# First example: Adam
net_light_base = NetLightRegression(l1=128, epochs=10, batch_size=BATCH_SIZE,
initialization='xavier', act_fn=nn.ReLU(),
optimizer='Adam', dropout_prob=0.1, lr_mult=lr_mult,
patience=5, _L_in=10, _L_out=1)
trainer = L.Trainer(max_epochs=2, enable_progress_bar=False)
trainer.fit(net_light_base, train_loader)
# Adam uses a lr which is calculated as lr=lr_mult * 0.001, so this value
# should be 0.1 * 0.001 = 0.0001
trainer.optimizers[0].param_groups[0]["lr"] == lr_mult*0.001
# Second example: Adadelta
net_light_base = NetLightRegression(l1=128, epochs=10, batch_size=BATCH_SIZE,
initialization='xavier', act_fn=nn.ReLU(),
optimizer='Adadelta', dropout_prob=0.1, lr_mult=lr_mult,
patience=5, _L_in=10, _L_out=1)
trainer = L.Trainer(max_epochs=2, enable_progress_bar=False)
trainer.fit(net_light_base, train_loader)
# Adadelta uses a lr which is calculated as lr=lr_mult * 1.0, so this value
# should be 1.0 * 0.1 = 0.1
trainer.optimizers[0].param_groups[0]["lr"] == lr_mult*1.0
Source code in spotpython/hyperparameters/optimizer.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 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 |
|