Skip to content

Commit c6e6c09

Browse files
Include training, testing and uncertainty quantification snippet in README.md
1 parent ebd4bff commit c6e6c09

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

README.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Bayesian-Torch: Bayesian neural network layers for uncertainty estimation
2-
**[Get started](#Installation)** | **[Example usage](#example-usage)** | **[Documentation](doc/bayesian_torch.layers.md)** | **[License](LICENSE)** | **[Citing](#citing)**
2+
**[Get started](#installation)** | **[Example usage](#example-usage-training-and-evaluation-of-models)** | **[Documentation](doc/bayesian_torch.layers.md)** | **[License](LICENSE)** | **[Citing](#citing)**
33

44
### Bayesian layers and utilities to perform stochastic variational inference in PyTorch
55

@@ -38,9 +38,9 @@ The repository has implementations for the following Bayesian layers:
3838
Please refer to [documentation](doc/bayesian_torch.layers.md#layers) of Bayesian layers for details.
3939

4040
Other features include:
41+
- [x] [dnn_to_bnn()](https://github.com/IntelLabs/bayesian-torch/blob/main/bayesian_torch/models/dnn_to_bnn.py#L127): An API to convert deterministic deep neural network (dnn) model of any architecture to Bayesian deep neural network (bnn) model, simplifying the model definition i.e. drop-in replacements of Convolutional, Linear and LSTM layers to corresponding Bayesian layers. This will enable seamless conversion of existing topology of larger models to Bayesian deep neural network models for extending towards uncertainty-aware applications.
42+
- [x] [MOPED](https://github.com/IntelLabs/bayesian-torch/blob/main/bayesian_torch/utils/util.py#L72): Specifying weight priors and variational posteriors in Bayesian neural networks with Empirical Bayes [[Krishnan et al. 2020](https://ojs.aaai.org/index.php/AAAI/article/view/5875)]
4143
- [x] [AvUC](https://github.com/IntelLabs/bayesian-torch/blob/main/bayesian_torch/utils/avuc_loss.py): Accuracy versus Uncertainty Calibration loss [[Krishnan and Tickoo 2020](https://proceedings.neurips.cc/paper/2020/file/d3d9446802a44259755d38e6d163e820-Paper.pdf)]
42-
- [x] [MOPED](https://github.com/IntelLabs/bayesian-torch/blob/main/bayesian_torch/utils/util.py#L72): Specifying weight priors and variational posteriors with Empirical Bayes [[Krishnan et al. 2020](https://ojs.aaai.org/index.php/AAAI/article/view/5875)]
43-
- [x] [dnn_to_bnn](https://github.com/IntelLabs/bayesian-torch/blob/main/bayesian_torch/models/dnn_to_bnn.py#L127): An API to convert deterministic deep neural network (dnn) model of any architecture to Bayesian deep neural network (bnn) model, simplifying the model definition i.e. drop-in replacements of Convolutional, Linear and LSTM layers to corresponding Bayesian layers. This will enable seamless conversion of existing topology of larger models to Bayesian deep neural network models for extending towards uncertainty-aware applications.
4444

4545
## Installation
4646
<!--
@@ -55,6 +55,7 @@ git clone https://github.com/IntelLabs/bayesian-torch
5555
cd bayesian-torch
5656
pip install .
5757
```
58+
<!--
5859
This code has been tested on PyTorch v1.8.1 LTS.
5960
6061
Dependencies:
@@ -64,16 +65,17 @@ Dependencies:
6465
- conda install -c conda-forge accimage
6566
- pip install tensorboard
6667
- pip install scikit-learn
67-
68+
-->
6869
## Usage
6970
There are two ways to build Bayesian deep neural networks using Bayesian-Torch:
7071
1. Convert an existing deterministic deep neural network (dnn) model to Bayesian deep neural network (bnn) model with dnn_to_bnn()
7172
2. Define your custom model using the Bayesian layers ([Flipout](https://github.com/IntelLabs/bayesian-torch/tree/main/bayesian_torch/layers/flipout_layers) or [Reparameterization](https://github.com/IntelLabs/bayesian-torch/tree/main/bayesian_torch/layers/variational_layers))
7273

73-
(1) For instance to build Bayesian-ResNet18 from torchvision deterministic ResNet18 model:
74+
(1) For instance, building Bayesian-ResNet18 from torchvision deterministic ResNet18 model is as simple as:
7475
```
76+
import torch
7577
import torchvision
76-
from bayesian_torch.models.dnn_to_bnn import dnn_to_bnn
78+
from bayesian_torch.models.dnn_to_bnn import dnn_to_bnn, get_kl_loss
7779
7880
const_bnn_prior_parameters = {
7981
"prior_mu": 0.0,
@@ -82,13 +84,13 @@ const_bnn_prior_parameters = {
8284
"posterior_rho_init": -3.0,
8385
"type": "Reparameterization", # Flipout or Reparameterization
8486
"moped_enable": False, # True to initialize mu/sigma from the pretrained dnn weights
85-
"moped_delta": 0.2,
87+
"moped_delta": 0.5,
8688
}
8789
8890
model = torchvision.models.resnet18()
8991
dnn_to_bnn(model, const_bnn_prior_parameters)
9092
```
91-
To use MOPED method, setting the prior and initializing variational parameters from a pretrained determined model (helps training convergence of larger models):
93+
To use MOPED method, setting the prior and initializing variational parameters from a pretrained deterministic model (helps training convergence of larger models):
9294
```
9395
const_bnn_prior_parameters = {
9496
"prior_mu": 0.0,
@@ -97,12 +99,47 @@ const_bnn_prior_parameters = {
9799
"posterior_rho_init": -3.0,
98100
"type": "Reparameterization", # Flipout or Reparameterization
99101
"moped_enable": True, # True to initialize mu/sigma from the pretrained dnn weights
100-
"moped_delta": 0.2,
102+
"moped_delta": 0.5,
101103
}
102104
103105
model = torchvision.models.resnet18(pretrained=True)
104106
dnn_to_bnn(model, const_bnn_prior_parameters)
105107
```
108+
Training snippet:
109+
```
110+
criterion = torch.nn.CrossEntropyLoss()
111+
optimizer = torch.optim.Adam(model.parameters(), args.learning_rate)
112+
113+
output = model(x_train)
114+
kl = get_kl_loss(model)
115+
ce_loss = criterion(output, y_train)
116+
loss = ce_loss + kl / args.batch_size
117+
118+
loss.backward()
119+
optimizer.step()
120+
```
121+
Testing snippet:
122+
```
123+
model.eval()
124+
with torch.no_grad():
125+
output_mc = []
126+
for mc_run in range(args.num_monte_carlo):
127+
logits = model(x_test)
128+
probs = torch.nn.functional.softmax(logits, dim=-1)
129+
output_mc.append(probs)
130+
output = torch.stack(output_mc)
131+
pred_mean = output.mean(dim=0)
132+
y_pred = torch.argmax(pred_mean, axis=-1)
133+
test_acc = (y_pred.data.cpu().numpy() == y_test.data.cpu().numpy()).mean()
134+
```
135+
Uncertainty Quantification:
136+
```
137+
from utils.util import predictive_entropy, mutual_information
138+
139+
predictive_uncertainty = predictive_entropy(output.data.cpu().numpy())
140+
model_uncertainty = mutual_information(output.data.cpu().numpy())
141+
```
142+
106143
(2) For building custom models, we have provided [example model implementations](bayesian_torch/models/bayesian) using the Bayesian layers.
107144

108145
## Example usage (training and evaluation of models)

0 commit comments

Comments
 (0)