Skip to content

Commit

Permalink
mc_calibration does not crash with VariableArray objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Kucharssim committed Feb 21, 2025
1 parent ec2a0e2 commit 3744793
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions bayesflow/utils/comp_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from keras import ops

from sklearn.calibration import calibration_curve

Expand All @@ -16,9 +17,9 @@ def expected_calibration_error(m_true, m_pred, num_bins=10):
Parameters
----------
m_true : np.ndarray of shape (num_sim, num_models)
m_true : array of shape (num_sim, num_models)
The one-hot-encoded true model indices.
m_pred : tf.tensor of shape (num_sim, num_models)
m_pred : array of shape (num_sim, num_models)
The predicted posterior model probabilities.
num_bins : int, optional, default: 10
The number of bins to use for the calibration curves (and marginal histograms).
Expand All @@ -32,11 +33,9 @@ def expected_calibration_error(m_true, m_pred, num_bins=10):
Each list contains two arrays of length (num_bins) with the predicted and true probabilities for each bin.
"""

# Convert tf.Tensors to numpy, if passed
if type(m_true) is not np.ndarray:
m_true = m_true.numpy()
if type(m_pred) is not np.ndarray:
m_pred = m_pred.numpy()
# Convert tensors to numpy, if passed
m_true = ops.convert_to_numpy(m_true)
m_pred = ops.convert_to_numpy(m_pred)

# Extract number of models and prepare containers
n_models = m_true.shape[1]
Expand Down

0 comments on commit 3744793

Please sign in to comment.