Laplax API¶
GGN
Create a GGN matrix-vector product function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_fn
|
ModelFn
|
Neural network forward pass. |
required |
params
|
Params
|
Network parameters. |
required |
data
|
Data | Iterable
|
Training data. |
required |
loss_fn
|
LossFn
|
Loss function to use. |
required |
factor
|
float
|
Scaling factor for GGN. |
1.0
|
vmap_over_data
|
bool
|
Whether model expects batch dimension. |
True
|
verbose_logging
|
bool
|
Whether to enable verbose logging. |
True
|
transform
|
Callable | None
|
Transform to apply to data. |
None
|
Returns:
Type | Description |
---|---|
Callable[[Params], Params]
|
GGN matrix-vector product function. |
Raises:
Type | Description |
---|---|
ValueError
|
If input/output shapes don't match. |
Source code in laplax/api.py
laplace
Estimate curvature & obtain a Gaussian weight-space posterior.
This function computes a Laplace approximation to the posterior distribution over neural network weights. It estimates the curvature of the loss landscape and constructs a Gaussian approximation centered at the MAP estimate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_fn
|
ModelFn
|
The neural network forward pass function that takes input and parameters. |
required |
params
|
Params
|
The MAP estimate of the network parameters. |
required |
data
|
Data | Iterable
|
Either a single batch (tuple/dict) or a DataLoader-like iterable containing the training data. |
required |
loss_fn
|
LossFn
|
The supervised loss function to use (e.g., "mse" for regression). |
required |
curv_type
|
CurvApprox
|
Type of curvature approximation to use (e.g., "ggn", "diag-ggn"). |
required |
num_curv_samples
|
Int
|
Number of Monte Carlo samples used to estimate the GGN, by default 1. |
1
|
num_total_samples
|
Int
|
Total number of samples in the dataset, by default 1. |
1
|
vmap_over_data
|
bool
|
Whether the model expects a leading batch axis, by default True. |
True
|
curv_mv_jit
|
bool
|
Whether to jit the curvature matrix-vector product, by default False. |
False
|
**curv_kwargs
|
Kwargs
|
Additional arguments forwarded to the curvature estimation function. |
{}
|
Returns:
Type | Description |
---|---|
tuple[Callable[[PriorArguments, Float], Posterior], PyTree]
|
A tuple containing:
|
Notes
The function supports different curvature approximations:
- Full GGN: Computes the full Generalized Gauss-Newton matrix
- Diagonal GGN: Approximates the GGN with its diagonal
- Low-rank GGN: Uses Lanczos or LOBPCG for efficient approximation
Source code in laplax/api.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 |
|
calibration
Calibrate hyperparameters of the Laplace approximation.
This function tunes the prior precision (or similar hyperparameters) of the Laplace approximation by optimizing a specified objective function. It supports different calibration objectives and methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
posterior_fn
|
Callable[[PriorArguments, Float], Posterior]
|
Function that generates samples from the posterior. |
required |
model_fn
|
ModelFn
|
The neural network forward pass function. |
required |
params
|
Params
|
The MAP estimate of the network parameters. |
required |
data
|
Data
|
The validation data used for calibration. |
required |
loss_fn
|
LossFn
|
The supervised loss function used for training. |
required |
curv_estimate
|
PyTree
|
The estimated curvature from the Laplace approximation. |
required |
curv_type
|
CurvApprox
|
Type of curvature approximation used. |
required |
predictive_type
|
Predictive | str
|
Type of predictive distribution to use, by default Predictive.NONE. |
NONE
|
pushforward_type
|
Pushforward | str
|
Type of pushforward approximation to use, by default Pushforward.LINEAR. |
LINEAR
|
pushforward_fns
|
list[Callable] | None
|
Custom pushforward functions to use, by default None. |
None
|
sample_key
|
KeyType
|
PRNG key. |
DEFAULT_KEY
|
num_samples
|
int
|
Number of MC samples for the predictive. |
30
|
calibration_objective
|
CalibrationObjective | str
|
Objective function to optimize during calibration, by default CalibrationObjective.NLL. |
NLL
|
calibration_method
|
CalibrationMethod | str
|
Method to use for calibration, by default CalibrationMethod.GRID_SEARCH. |
GRID_SEARCH
|
vmap_over_data
|
bool
|
Whether the model expects a leading batch axis, by default True. |
True
|
objective_jit
|
bool
|
Whether to jit the calibration objective, by default True. |
True
|
**calibration_kwargs
|
Kwargs
|
Additional arguments for the calibration method. |
{}
|
Returns:
Type | Description |
---|---|
tuple[PriorArguments, Callable[[InputArray], dict[str, Array]]]
|
A tuple containing:
|
Raises:
Type | Description |
---|---|
ValueError
|
When an unknown calibration method is provided. |
Notes
Supported calibration objectives:
- NLL: Negative log-likelihood
- CHI_SQUARED: Chi-squared statistic
- MARGINAL_LOG_LIKELIHOOD: Marginal log-likelihood
- ECE: Expected Calibration Error
Supported calibration methods:
- GRID_SEARCH: Grid search over prior precision
Source code in laplax/api.py
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 |
|
evaluation
Evaluate the calibrated Laplace approximation.
This function assesses the performance of the calibrated Laplace approximation by computing various metrics on the test data. It supports both regression and classification tasks with different predictive distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
posterior_fn
|
Callable[[PriorArguments, Float], Posterior]
|
Function that generates samples from the posterior. |
required |
model_fn
|
ModelFn
|
The neural network forward pass function. |
required |
params
|
Params
|
The MAP estimate of the network parameters. |
required |
arguments
|
PriorArguments
|
The calibrated prior arguments. |
required |
data
|
Data | Iterator[Data]
|
The test data for evaluation. |
required |
metrics
|
DefaultMetrics | list[Callable] | Callable | str
|
Metrics to compute during evaluation, by default DefaultMetrics.REGRESSION. |
REGRESSION
|
predictive_type
|
Predictive | str
|
Type of predictive distribution to use, by default Predictive.NONE. |
NONE
|
pushforward_type
|
Pushforward | str
|
Type of pushforward approximation to use, by default Pushforward.LINEAR. |
LINEAR
|
pushforward_fns
|
list[Callable] | None
|
Custom pushforward functions to use, by default None. |
None
|
reduce
|
Callable
|
Function to reduce metrics across batches, by default identity. |
identity
|
sample_key
|
KeyType
|
Random key for sampling, by default jax.random.key(0). |
DEFAULT_KEY
|
num_samples
|
int
|
Number of samples for Monte Carlo predictions, by default 10. |
10
|
predictive_jit
|
bool
|
Whether to jit the predictive distribution, by default True. |
True
|
Returns:
Type | Description |
---|---|
tuple[dict[str, Array], Callable[[InputArray], dict[str, Array]]]
|
A tuple containing:
|
Notes
Supported metrics:
- REGRESSION: Default metrics for regression tasks
- CLASSIFICATION: Default metrics for classification tasks
- Custom metrics can be provided as a list of callables
The function supports both linearized and Monte Carlo predictions through different pushforward types.
Source code in laplax/api.py
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 |
|