The bayesian models module¶
This module contains the two Bayesian Models available in this library, namely
the bayesian version of the Wide and TabMlp models, referred as
BayesianWide and BayesianTabMlp. These models are very useful in
scenarios where getting a measure of uncertainty is important.
The models in this module are based on the publication: Weight Uncertainty in Neural Networks.
BayesianWide ¶
Bases: BaseBayesianModel
Defines a Wide model. This is a linear model where the
non-linearlities are captured via crossed-columns
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
size of the Embedding layer. |
required |
pred_dim
|
int
|
size of the ouput tensor containing the predictions |
1
|
prior_sigma_1
|
float
|
The prior weight distribution is a scaled mixture of two Gaussian densities: \[
\begin{aligned}
P(\mathbf{w}) = \prod_{i=j} \pi N (\mathbf{w}_j | 0, \sigma_{1}^{2}) + (1 - \pi) N (\mathbf{w}_j | 0, \sigma_{2}^{2})
\end{aligned}
\]
|
1.0
|
prior_sigma_2
|
float
|
Prior of the sigma parameter for the second of the two Gaussian distributions that will be mixed to produce the prior weight distribution |
0.002
|
prior_pi
|
float
|
Scaling factor that will be used to mix the Gaussians to produce the prior weight distribution |
0.8
|
posterior_mu_init
|
float
|
The posterior sample of the weights is defined as: \[
\begin{aligned}
\mathbf{w} &= \mu + log(1 + exp(\rho))
\end{aligned}
\]
where: \[
\begin{aligned}
\mathcal{N}(x\vert \mu, \sigma) &= \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(x-\mu)^2}{2\sigma^2}}\\
\log{\mathcal{N}(x\vert \mu, \sigma)} &= -\log{\sqrt{2\pi}} -\log{\sigma} -\frac{(x-\mu)^2}{2\sigma^2}\\
\end{aligned}
\]
\(\mu\) is initialised using a normal distributtion with mean
|
0.0
|
posterior_rho_init
|
float
|
As in the case of \(\mu\), \(\rho\) is initialised using a
normal distributtion with mean |
-7.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
bayesian_wide_linear |
Module
|
the linear layer that comprises the wide branch of the model |
Examples:
>>> import torch
>>> from pytorch_widedeep.bayesian_models import BayesianWide
>>> X = torch.empty(4, 4).random_(6)
>>> wide = BayesianWide(input_dim=int(X.max().item()), pred_dim=1)
>>> out = wide(X)
Source code in pytorch_widedeep/bayesian_models/tabular/bayesian_linear/bayesian_wide.py
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 | |
BayesianTabMlp ¶
Bases: BaseBayesianModel
Defines a BayesianTabMlp model.
This class combines embedding representations of the categorical features with numerical (aka continuous) features, embedded or not. These are then passed through a series of probabilistic dense layers (i.e. a MLP).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column_idx
|
Dict[str, int]
|
Dict containing the index of the columns that will be passed through
the |
required |
cat_embed_input
|
Optional[List[Tuple[str, int, int]]]
|
List of Tuples with the column name, number of unique values and embedding dimension. e.g. [(education, 11, 32), ...] |
None
|
cat_embed_activation
|
Optional[str]
|
Activation function for the categorical embeddings, if any. Currently 'tanh', 'relu', 'leaky_relu' and 'gelu' are supported |
None
|
continuous_cols
|
Optional[List[str]]
|
List with the name of the numeric (aka continuous) columns |
None
|
cont_norm_layer
|
Optional[Literal[batchnorm, layernorm]]
|
Type of normalization layer applied to the continuous features. Options are: 'layernorm', 'batchnorm' or None. |
None
|
embed_continuous
|
Optional[bool]
|
Boolean indicating if the continuous columns will be embedded (i.e. passed each through a linear layer with or without activation) |
None
|
cont_embed_dim
|
Optional[int]
|
Size of the continuous embeddings |
None
|
cont_embed_dropout
|
Optional[float]
|
Dropout for the continuous embeddings |
None
|
use_cont_bias
|
Optional[bool]
|
Boolean indicating if bias will be used for the continuous embeddings |
None
|
cont_embed_activation
|
Optional[str]
|
Activation function for the continuous embeddings if any. Currently 'tanh', 'relu', 'leaky_relu' and 'gelu' are supported |
None
|
mlp_hidden_dims
|
List[int]
|
List with the number of neurons per dense layer in the mlp. |
[200, 100]
|
mlp_activation
|
str
|
Activation function for the dense layers of the MLP. Currently 'tanh', 'relu', 'leaky_relu' and 'gelu' are supported |
'leaky_relu'
|
prior_sigma_1
|
float
|
The prior weight distribution is a scaled mixture of two Gaussian densities: \[
\begin{aligned}
P(\mathbf{w}) = \prod_{i=j} \pi N (\mathbf{w}_j | 0, \sigma_{1}^{2}) + (1 - \pi) N (\mathbf{w}_j | 0, \sigma_{2}^{2})
\end{aligned}
\]
|
1
|
prior_sigma_2
|
float
|
Prior of the sigma parameter for the second of the two Gaussian distributions that will be mixed to produce the prior weight distribution for each Bayesian linear and embedding layer |
0.002
|
prior_pi
|
float
|
Scaling factor that will be used to mix the Gaussians to produce the prior weight distribution ffor each Bayesian linear and embedding layer |
0.8
|
posterior_mu_init
|
float
|
The posterior sample of the weights is defined as: $$ \begin{aligned} \mathbf{w} &= \mu + log(1 + exp(\rho)) \end{aligned} $$ where: \[
\begin{aligned}
\mathcal{N}(x\vert \mu, \sigma) &= \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(x-\mu)^2}{2\sigma^2}}\\
\log{\mathcal{N}(x\vert \mu, \sigma)} &= -\log{\sqrt{2\pi}} -\log{\sigma} -\frac{(x-\mu)^2}{2\sigma^2}\\
\end{aligned}
\]
\(\mu\) is initialised using a normal distributtion with mean
|
0.0
|
posterior_rho_init
|
float
|
As in the case of \(\mu\), \(\rho\) is initialised using a
normal distributtion with mean |
-7.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
bayesian_cat_and_cont_embed |
Module
|
This is the module that processes the categorical and continuous columns |
bayesian_tab_mlp |
Sequential
|
mlp model that will receive the concatenation of the embeddings and the continuous columns |
Examples:
>>> import torch
>>> from pytorch_widedeep.bayesian_models import BayesianTabMlp
>>> X_tab = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1)
>>> colnames = ['a', 'b', 'c', 'd', 'e']
>>> cat_embed_input = [(u,i,j) for u,i,j in zip(colnames[:4], [4]*4, [8]*4)]
>>> column_idx = {k:v for v,k in enumerate(colnames)}
>>> model = BayesianTabMlp(mlp_hidden_dims=[8,4], column_idx=column_idx, cat_embed_input=cat_embed_input,
... continuous_cols = ['e'])
>>> out = model(X_tab)
Source code in pytorch_widedeep/bayesian_models/tabular/bayesian_mlp/bayesian_tab_mlp.py
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 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 | |