laplax.curv.fisher
Fisher Matrix Vector Product.
create_empirical_fisher_mv_without_data ¶
create_empirical_fisher_mv_without_data(model_fn: ModelFn, params: Params, loss_fn: LossFn | str | Callable | None, factor: Float, *, vmap_over_data: bool = True, loss_grad_fn: Callable | None = None) -> Callable[[Params, Data], Params]
Create empirical Fisher matrix-vector product without fixed data.
The resulting matrix vector product computes: $$ \text{factor} \cdot \sum_n J_n^\top \left(\nabla_{f_n} c(y=y_n,\hat{y}=f_n)\right) \left(\nabla_{f_n} c(y=y_n,\hat{y}=f_n)\right)^\top J_n \cdot v $$
where \(J_n\) is the Jacobian of the model w.r.t the parameters
evaluated at data point \(n\), \(c(y=y_n,\hat{y}=f_n)\) is the
loss function evaluated at data point \(n\), and \(v\) is the vector.
The factor is a scaling factor that is used to scale the Fisher matrix.
This function computes the above expression efficiently without hardcoding the dataset, making it suitable for distributed or batched computations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_fn
|
ModelFn
|
The model's forward pass function. |
required |
params
|
Params
|
Model parameters. |
required |
loss_fn
|
LossFn | str | Callable | None
|
Loss function to use for the Fisher computation. |
required |
factor
|
Float
|
Scaling factor for the Fisher computation. |
required |
vmap_over_data
|
bool
|
Whether to vmap over the data. Defaults to True. |
True
|
loss_grad_fn
|
Callable | None
|
The loss gradient function. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[Params, Data], Params]
|
A function that takes a vector and a batch of data, |
Callable[[Params, Data], Params]
|
and computes the empirical Fisher matrix-vector product. |
Note
If 'vmap_over_data'=True (default), the computation is vmapped over the batch of data. If the computation should be performed for a singe datum (or a singleton batch dimension), pass 'vmap_over_data'=False. If 'vmap_over_data'=False and a non-singleton batch dimension is present, the batch dimension is handled by vectorization. Compared to 'vmap_over_data'=True, this can be faster especially for large batches, but the passed 'model_fn' and (optional) 'loss_grad_fn' must accept batches of data.
Source code in laplax/curv/fisher.py
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 | |
create_empirical_fisher_mv ¶
create_empirical_fisher_mv(model_fn: ModelFn, params: Params, data: Data, loss_fn: LossFn | str | Callable | None = None, *, num_curv_samples: Int | None = None, num_total_samples: Int | None = None, vmap_over_data: bool = True, loss_grad_fn: Callable | None = None) -> CurvatureMV
Creates the empirical Fisher matrix-vector product with data.
The resulting matrix vector product computes: $$ \frac{\text{num_total_samples}}{\text{num_curv_samples}} \cdot \sum_n J_n^\top \left(\nabla_{f_n} c(y=y_n,\hat{y}=f_n)\right) \left(\nabla_{f_n} c(y=y_n,\hat{y}=f_n)\right)^\top J_n \cdot v $$
where \(J_n\) is the Jacobian of the model w.r.t the parameters evaluated at data point \(n\), \(c(y=y_n,\hat{y}=f_n)\) is the loss function evaluated at data point \(n\), and \(v\) is the vector.
This function hardcodes the dataset, making it ideal for scenarios where the dataset remains fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_fn
|
ModelFn
|
The model's forward pass function. |
required |
params
|
Params
|
Model parameters. |
required |
data
|
Data
|
A batch of input and target data. |
required |
loss_fn
|
LossFn | str | Callable | None
|
Loss function to use for the Fisher computation. |
None
|
num_curv_samples
|
Int | None
|
Number of samples used to calculate the Fisher.
Defaults to None, in which case it is inferred from |
None
|
num_total_samples
|
Int | None
|
Number of total samples the model was trained on. See the
remark in |
None
|
vmap_over_data
|
bool
|
Whether to vmap over the data. Defaults to True. |
True
|
loss_grad_fn
|
Callable | None
|
The loss gradient function. If not provided, it is computed using the 'loss_fn'. |
None
|
Returns:
| Type | Description |
|---|---|
CurvatureMV
|
A function that takes a vector and computes |
CurvatureMV
|
the empirical Fisher matrix-vector product for the given data. |
Note
If 'vmap_over_data'=True (default), the computation is vmapped over the batch of data. If the computation should be performed for a singe datum (or a singleton batch dimension), pass 'vmap_over_data'=False. If 'vmap_over_data'=False and a non-singleton batch dimension is present, the batch dimension is handled by vectorization. Compared to 'vmap_over_data'=True, this can be faster especially for large batches, but the passed 'model_fn' and (optional) 'loss_grad_fn' must accept batches of data.
Source code in laplax/curv/fisher.py
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 | |
create_MC_fisher_mv_without_data ¶
create_MC_fisher_mv_without_data(model_fn: ModelFn, params: Params, loss_fn: LossFn | str, key: KeyType, factor: Float, *, vmap_over_data: bool = True, mc_samples: Int | None = 1) -> Callable[[Params, Data], Params]
Create Monte-Carlo approximated Fisher matrix-vector product without fixed data.
The resulting matrix vector product computes: $$ \frac{factor}{\text{mc_samples}}\sum_{n,m} J_n^\top \left(\nabla_{f_n} c(y=\tilde{y}{n,m},\hat{y}=f_n)\right) \left(\nabla{f_n} c(y=\tilde{y}_{n,m},\hat{y}=f_n)\right)^\top J_n \cdot v $$
where \(J_n\) is the Jacobian of the model w.r.t the parameters evaluated at
data point \(n\), \(c(y,\hat{y})\) is the loss function, and \(v\) is the vector.
\(\tilde{y}_{n,m}\) is the m-th Monte Carlo sample of the label under the likelihood
induced by the loss function: \(r(y|f_n) = \exp(-c(y,\hat{y}=f_n))\) at
data point \(n\).
The factor is a scaling factor that is used to scale the Fisher matrix.
This function computes the above expression efficiently without hardcoding the dataset, making it suitable for distributed or batched computations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_fn
|
ModelFn
|
The model's forward pass function. |
required |
params
|
Params
|
Model parameters. |
required |
loss_fn
|
LossFn | str
|
Loss function to use for the Fisher computation. |
required |
key
|
KeyType
|
Random key to use for sampling. |
required |
factor
|
Float
|
Scaling factor for the Fisher computation. |
required |
vmap_over_data
|
bool
|
Whether to vmap over the data. Defaults to True. |
True
|
mc_samples
|
Int | None
|
Number of MC samples to use. Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
Callable[[Params, Data], Params]
|
A function that takes a vector and a batch of data |
Callable[[Params, Data], Params]
|
and computes the Monte-Carlo Fisher matrix-vector product. |
Note
If vmap_over_data is true (default), the computation is vmapped over the batch of data. If the computation should be performed for a singe datum (or a singleton batch dimension), pass 'vmap_over_data'=False. If 'vmap_over_data'=False and a non-singleton batch dimension is passed, the batch dimension is handled explicitly. In this case, the passed model_fn must accept batches of data.
Source code in laplax/curv/fisher.py
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 | |
create_MC_fisher_mv ¶
create_MC_fisher_mv(model_fn: ModelFn, params: Params, data: Data, loss_fn: LossFn | str, key: KeyType, *, num_curv_samples: Int | None = None, num_total_samples: Int | None = None, vmap_over_data: bool = True, mc_samples: Int | None = 1) -> CurvatureMV
Create Monte-Carlo approximated Fisher matrix-vector product.
The resulting matrix vector product computes: $$ \text{factor} \cdot \frac{1}{\text{mc_samples}}\sum_{n,m} J_n^\top \left(\nabla_{f_n} c(y=\tilde{y}{n,m},\hat{y}=f_n)\right) \left(\nabla{f_n} c(y=\tilde{y}_{n,m},\hat{y}=f_n)\right)^\top J_n \cdot v $$
where \(J_n\) is the Jacobian of the model w.r.t the parameters
evaluated at data point \(n\), \(c(y,\hat{y})\) is the
loss function, and \(v\) is the vector.
\(\tilde{y}_{n,m}\) is the m-th Monte Carlo sample of the label under the likelihood
induced by the loss function: \(r(y|f_n) =\exp(-c(y,\hat{y}=f_n))\) at data point \(n\).
The factor is a scaling factor that is used to scale the Fisher matrix.
This function hardcodes the dataset, making it ideal for scenarios where the dataset remains fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_fn
|
ModelFn
|
The model's forward pass function. |
required |
params
|
Params
|
Model parameters. |
required |
data
|
Data
|
A batch of input and target data. |
required |
loss_fn
|
LossFn | str
|
Loss function to use for the Fisher computation. |
required |
key
|
KeyType
|
Random key to use for sampling. |
required |
num_curv_samples
|
Int | None
|
Number of samples used to calculate the Fisher.
Defaults to None, in which case it is inferred from |
None
|
num_total_samples
|
Int | None
|
Number of total samples the model was trained on. See the
remark in |
None
|
vmap_over_data
|
bool
|
Whether to vmap over the data. Defaults to True. |
True
|
mc_samples
|
Int | None
|
Number of MC samples to use. Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
CurvatureMV
|
A function that takes a vector and computes |
CurvatureMV
|
the Monte-Carlo Fisher matrix-vector product. |
Note
If vmap_over_data is true (default), the computation is vmapped over the batch of data. If the computation should be performed for a singe datum (or a singleton batch dimension), pass 'vmap_over_data'=False. If 'vmap_over_data'=False and a non-singleton batch dimension is passed, the batch dimension is handled explicitly. In this case, the passed model_fn must accept batches of data.
Source code in laplax/curv/fisher.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |