laplace.marglik_training
#
marglik_training
#
marglik_training(model: Module, train_loader: DataLoader, likelihood: Likelihood | str = Likelihood.CLASSIFICATION, hessian_structure: HessianStructure | str = HessianStructure.KRON, backend: Type[CurvatureInterface] = AsdlGGN, optimizer_cls: Type[Optimizer] = Adam, optimizer_kwargs: dict | None = None, scheduler_cls: Type[LRScheduler] | None = None, scheduler_kwargs: dict | None = None, n_epochs: int = 300, lr_hyp: float = 0.1, prior_structure: PriorStructure | str = PriorStructure.LAYERWISE, n_epochs_burnin: int = 0, n_hypersteps: int = 10, marglik_frequency: int = 1, prior_prec_init: float = 1.0, sigma_noise_init: float = 1.0, temperature: float = 1.0, fix_sigma_noise: bool = False, progress_bar: bool = False, enable_backprop: bool = False, dict_key_x: str = 'input_ids', dict_key_y: str = 'labels') -> tuple[BaseLaplace, Module, list[Number], list[Number]]
Marginal-likelihood based training (Algorithm 1 in [1]). Optimize model parameters and hyperparameters jointly. Model parameters are optimized to minimize negative log joint (train loss) while hyperparameters minimize negative log marginal likelihood.
This method replaces standard neural network training and adds hyperparameter optimization to the procedure.
The settings of standard training can be controlled by passing train_loader
,
optimizer_cls
, optimizer_kwargs
, scheduler_cls
, scheduler_kwargs
, and n_epochs
.
The model
should return logits, i.e., no softmax should be applied.
With likelihood=Likelihood.CLASSIFICATION
or Likelihood.REGRESSION
, one can choose between
categorical likelihood (CrossEntropyLoss) and Gaussian likelihood (MSELoss).
As in [1], we optimize prior precision and, for regression, observation noise
using the marginal likelihood. The prior precision structure can be chosen
as 'scalar'
, 'layerwise'
, or 'diagonal'
. 'layerwise'
is a good default
and available to all Laplace approximations. lr_hyp
is the step size of the
Adam hyperparameter optimizer, n_hypersteps
controls the number of steps
for each estimated marginal likelihood, n_epochs_burnin
controls how many
epochs to skip marginal likelihood estimation, marglik_frequency
controls
how often to estimate the marginal likelihood (default of 1 re-estimates
after every epoch, 5 would estimate every 5-th epoch).
References
[1] Immer, A., Bauer, M., Fortuin, V., Rätsch, G., Khan, EM. Scalable Marginal Likelihood Estimation for Model Selection in Deep Learning. ICML 2021.
Parameters:
-
model
(Module
) –torch neural network model (needs to comply with Backend choice)
-
train_loader
(DataLoader
) –pytorch dataloader that implements
len(train_loader.dataset)
to obtain number of data points -
likelihood
(str
, default:Likelihood.CLASSIFICATION
) –Likelihood.CLASSIFICATION or Likelihood.REGRESSION
-
hessian_structure
(('diag', 'kron', 'full')
, default:'diag'
) –structure of the Hessian approximation
-
backend
(Backend
, default:AsdlGGN
) –Curvature subclass, e.g. AsdlGGN/AsdlEF or BackPackGGN/BackPackEF
-
optimizer_cls
(Optimizer
, default:Adam
) –optimizer to use for optimizing the neural network parameters togeth with
train_loader
-
optimizer_kwargs
(dict
, default:None
) –keyword arguments for
optimizer_cls
, for example to change learning rate or momentum -
scheduler_cls
(_LRScheduler
, default:None
) –optionally, a scheduler to use on the learning rate of the optimizer.
scheduler.step()
is called after every batch of the standard training. -
scheduler_kwargs
(dict
, default:None
) –keyword arguments for
scheduler_cls
, e.g.lr_min
for CosineAnnealingLR -
n_epochs
(int
, default:300
) –number of epochs to train for
-
lr_hyp
(float
, default:0.1
) –Adam learning rate for hyperparameters
-
prior_structure
(str
, default:'layerwise'
) –structure of the prior. one of
['scalar', 'layerwise', 'diag']
-
n_epochs_burnin
(int default=0
, default:0
) –how many epochs to train without estimating and differentiating marglik
-
n_hypersteps
(int
, default:10
) –how many steps to take on the hyperparameters when marglik is estimated
-
marglik_frequency
(int
, default:1
) –how often to estimate (and differentiate) the marginal likelihood
marglik_frequency=1
would be every epoch,marglik_frequency=5
would be every 5 epochs. -
prior_prec_init
(float
, default:1.0
) –initial prior precision
-
sigma_noise_init
(float
, default:1.0
) –initial observation noise (for regression only)
-
temperature
(float
, default:1.0
) –factor for the likelihood for 'overcounting' data. Might be required for data augmentation.
-
fix_sigma_noise
(bool
, default:False
) –if False, optimize observation noise via marglik otherwise use
sigma_noise_init
throughout. Only works for regression. -
progress_bar
(bool
, default:False
) –whether to show a progress bar (updated per epoch) or not
-
enable_backprop
(bool
, default:False
) –make the returned Laplace instance backpropable---useful for e.g. Bayesian optimization.
-
dict_key_x
(str
, default:'input_ids'
) –The dictionary key under which the input tensor
x
is stored. Only has effect when the model takes aMutableMapping
as the input. Useful for Huggingface LLM models. -
dict_key_y
(str
, default:'labels'
) –The dictionary key under which the target tensor
y
is stored. Only has effect when the model takes aMutableMapping
as the input. Useful for Huggingface LLM models.
Returns:
-
lap
(laplace
) –fit Laplace approximation with the best obtained marginal likelihood during training
-
model
(Module
) –corresponding model with the MAP parameters
-
margliks
(list
) –list of marginal likelihoods obtained during training (to monitor convergence)
-
losses
(list
) –list of losses (log joints) obtained during training (to monitor convergence)
Source code in laplace/marglik_training.py
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 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 |
|