Skip to content

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
def bnn_load_predictions_(filepath: str) -> Tuple[np.ndarray, int, int]:
    """
    Load predictions generated by a Bayesian neural network from a CSV file.

    Parameters
    ----------
    filepath : str
        Path to the CSV file containing predictions.

    Returns
    -------
    predictions_array : np.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.
    """
    predictions_df = load_data(path=filepath, sep=",")
    predictions_array = predictions_df.to_numpy(dtype=np.float32)
    nb_intervals, nb_curves = predictions_array.shape
    return predictions_array, nb_intervals, nb_curves

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
def bnn_make_predictions_(
    bnn_model: keras.Model,
    X_test: Union[np.ndarray, tf.Tensor],
    iterations: int = 100,
    batch_size: int = None
) -> np.ndarray:
    """
    Generate predictions from a Bayesian neural network by repeated stochastic forward passes.

    Parameters
    ----------
    bnn_model : keras.Model
        Trained Bayesian neural network.
    X_test : np.ndarray or tf.Tensor
        Test input data.
    iterations : int, optional
        Number of stochastic forward passes to perform (default 100).
    batch_size : int, optional
        Batch size to use during prediction. If None, defaults to full batch.

    Returns
    -------
    np.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.
    """
    predicted = []
    for _ in range(iterations):
        # !!! TO DO : investiger les différences de comportement entre 
        # model et model.predict avec batch_size != None !!!
        # predicted.append(bnn_model.predict(X_test, batch_size=batch_size, verbose=0))
        predicted.append(bnn_model(X_test))
    predicted = np.concatenate(predicted, axis=1)

    return predicted

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
def gaussian_prior(
    kernel_size: int,
    bias_size: int,
    dtype: tf.dtypes.DType = None,
    sigma: float = 1.0
) -> keras.Sequential:
    """
    Define a Gaussian prior distribution over weights and biases of a Bayesian neural network.

    Parameters
    ----------
    kernel_size : int
        Number of kernel weights in the layer.
    bias_size : int
        Number of bias terms in the layer.
    dtype : tf.dtypes.DType, optional
        TensorFlow data type for the distribution (default None).
    sigma : float, optional
        Standard deviation of the Gaussian prior (default 1.0).

    Returns
    -------
    keras.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.
    """
    n = kernel_size + bias_size
    var_mat_diag = sigma * tf.ones(n, dtype=dtype)
    prior_model = keras.Sequential(
        [
            tfp.layers.DistributionLambda(
                lambda t: tfp.distributions.MultivariateNormalDiag(
                    loc=tf.zeros(n, dtype=dtype), scale_diag=var_mat_diag
                )
            )
        ]
    )
    return prior_model

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
def independent_gaussian_posterior(
    kernel_size: int,
    bias_size: int,
    dtype: tf.dtypes.DType = None
) -> keras.Sequential:
    """
    Define an independent Gaussian posterior distribution for variational inference
    in Bayesian neural networks.

    Parameters
    ----------
    kernel_size : int
        Number of kernel weights in the layer.
    bias_size : int
        Number of bias terms in the layer.
    dtype : tf.dtypes.DType, optional
        TensorFlow data type for the distribution (default None).

    Returns
    -------
    keras.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**.
    """
    n = kernel_size + bias_size
    posterior_model = keras.Sequential(
        [
            tfp.layers.VariableLayer(
                tfp.layers.IndependentNormal.params_size(n), dtype=dtype
            ),
            tfp.layers.IndependentNormal(n),
        ]
    )
    return posterior_model

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
def negative_loglikelihood(
    targets: Union[np.ndarray, tf.Tensor],
    estimated_distribution: tfp.distributions.Distribution
) -> tf.Tensor:
    """
    Compute negative log-likelihood for a probabilistic prediction.

    Parameters
    ----------
    targets : np.ndarray or tf.Tensor
        True target values.
    estimated_distribution : tfp.distributions.Distribution
        Estimated (predicted) distribution output from the Bayesian network.

    Returns
    -------
    tf.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.
    """
    return -estimated_distribution.log_prob(targets)