bnn_models_built_in_utils¶
Description of helper functions for Bayesian modeling implemented in the module bnn_for_14C_calibration.bnn_models_built_in_utils:
bnn_for_14C_calibration.bnn_models_built_in_utils
¶
bnn_load_predictions_(filepath)
¶
Load predictions generated by a Bayesian neural network from a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the CSV file containing predictions. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
predictions_array |
ndarray
|
Predictions as a float32 numpy array. |
nb_intervals |
int
|
Number of intervals (rows) in the predictions. |
nb_curves |
int
|
Number of curves (columns) in the predictions. |
Notes
- Intended for predictions generated by
bnn_make_predictions_for calibration purposes.
Source code in src/bnn_for_14C_calibration/bnn_models_built_in_utils.py
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 | |
bnn_make_predictions_(bnn_model, X_test, iterations=100, batch_size=None)
¶
Generate predictions from a Bayesian neural network by repeated stochastic forward passes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bnn_model
|
Model
|
Trained Bayesian neural network. |
required |
X_test
|
ndarray or Tensor
|
Test input data. |
required |
iterations
|
int
|
Number of stochastic forward passes to perform (default 100). |
100
|
batch_size
|
int
|
Batch size to use during prediction. If None, defaults to full batch. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Concatenated predictions from all iterations. Shape: (n_samples, n_outputs, iterations) |
Notes
- Each call to the model produces a stochastic output due to the variational posterior.
Source code in src/bnn_for_14C_calibration/bnn_models_built_in_utils.py
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 | |
gaussian_prior(kernel_size, bias_size, dtype=None, sigma=1.0)
¶
Define a Gaussian prior distribution over weights and biases of a Bayesian neural network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel_size
|
int
|
Number of kernel weights in the layer. |
required |
bias_size
|
int
|
Number of bias terms in the layer. |
required |
dtype
|
DType
|
TensorFlow data type for the distribution (default None). |
None
|
sigma
|
float
|
Standard deviation of the Gaussian prior (default 1.0). |
1.0
|
Returns:
| Type | Description |
|---|---|
Sequential
|
Non-trainable prior distribution model. Each weight and bias is assumed independent and distributed according to a normal distribution N(0, sigma^2). |
Notes
- The prior distribution is non-trainable; its parameters are fixed.
Source code in src/bnn_for_14C_calibration/bnn_models_built_in_utils.py
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 | |
independent_gaussian_posterior(kernel_size, bias_size, dtype=None)
¶
Define an independent Gaussian posterior distribution for variational inference in Bayesian neural networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel_size
|
int
|
Number of kernel weights in the layer. |
required |
bias_size
|
int
|
Number of bias terms in the layer. |
required |
dtype
|
DType
|
TensorFlow data type for the distribution (default None). |
None
|
Returns:
| Type | Description |
|---|---|
Sequential
|
Trainable posterior distribution model. Each weight and bias has a learnable mean and diagonal variance. Off-diagonal covariances are zero, implying independence among weights. |
Notes
- The parameters of the distribution (mean and diagonal variance) are trainable.
Source code in src/bnn_for_14C_calibration/bnn_models_built_in_utils.py
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 | |
negative_loglikelihood(targets, estimated_distribution)
¶
Compute negative log-likelihood for a probabilistic prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
ndarray or Tensor
|
True target values. |
required |
estimated_distribution
|
Distribution
|
Estimated (predicted) distribution output from the Bayesian network. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Negative log-likelihood value for use as a loss function in Bayesian neural networks. |
Notes
- Used with stochastic outputs of probabilistic networks.
- Supports variational inference for posterior estimation.
Source code in src/bnn_for_14C_calibration/bnn_models_built_in_utils.py
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 | |