Source code for bayesflow.utils.keras_utils

import keras
import numpy as np

from bayesflow.types import Tensor


[docs] def resolve_seed(seed): """Convert an integer seed to a SeedGenerator; pass a SeedGenerator or None through unchanged.""" if isinstance(seed, int): return keras.random.SeedGenerator(seed) return seed
[docs] def inverse_shifted_softplus(x: Tensor, shift: float = np.log(np.e - 1), beta: float = 1.0, threshold: float = 20.0): """Inverse of the shifted softplus function.""" return inverse_softplus(x, beta=beta, threshold=threshold) - shift
[docs] def inverse_softplus(x: Tensor, beta: float = 1.0, threshold: float = 20.0) -> Tensor: """Numerically stabilized inverse softplus function.""" return keras.ops.where(beta * x > threshold, x, keras.ops.log(keras.ops.expm1(beta * x)) / beta)
[docs] def shifted_softplus(x: Tensor, shift: float = np.log(np.e - 1)) -> Tensor: """Shifted version of the softplus function such that shifted_softplus(0) = 1""" return keras.ops.softplus(x + shift)