trieste.models.keras.architectures
#
This file contains implementations of neural network architectures with Keras.
Module Contents#
-
class
KerasEnsemble
(networks: Sequence[KerasEnsembleNetwork])[source]# This class builds an ensemble of neural networks, using Keras. Individual networks must be instance of
KerasEnsembleNetwork
. This class is meant to be used withDeepEnsemble
model wrapper, which compiles the model.- Parameters
networks – A list of neural network specifications, one for each member of the ensemble. The ensemble will be built using these specifications.
- Raises
ValueError – If there are no objects in
networks
or we try to create a model with networks whose input or output shapes are not the same.
-
property
ensemble_size
→ int[source]# Returns the size of the ensemble, that is, the number of base learners or individual neural network models in the ensemble.
-
_build_ensemble
() → tensorflow.keras.Model[source]# Builds the ensemble model by combining all the individual networks in a single Keras model. This method relies on
connect_layers
method ofKerasEnsembleNetwork
objects to construct individual networks.- Returns
The Keras model.
-
class
KerasEnsembleNetwork
(input_tensor_spec: tensorflow.TensorSpec, output_tensor_spec: tensorflow.TensorSpec, network_name: str = '')[source]# This class is an interface that defines necessary attributes and methods for neural networks that are meant to be used for building ensembles by
KerasEnsemble
. Subclasses are not meant to build and compile Keras models, instead they are providing specification thatKerasEnsemble
will use to build the Keras model.- Parameters
input_tensor_spec – Tensor specification for the input to the network.
output_tensor_spec – Tensor specification for the output of the network.
network_name – The name to be used when building the network.
-
abstract
connect_layers
() → tuple[tensorflow.Tensor, tensorflow.Tensor][source]# Connects the layers of the neural network. Architecture, layers and layer specifications need to be defined by the subclasses.
- Returns
Input and output tensor of the network, required by
tf.keras.Model
to build a model.
-
class
MultivariateNormalTriL
(event_size: int, convert_to_tensor_fn: Callable[[tensorflow_probability.python.distributions.Distribution], trieste.types.TensorType] = tfp.python.distributions.Distribution.sample, validate_args: bool = False, **kwargs: Any)[source]# Bases:
tensorflow_probability.layers.MultivariateNormalTriL
Fixed version of tfp.layers.MultivariateNormalTriL that handles saving.
-
class
GaussianNetwork
(input_tensor_spec: tensorflow.TensorSpec, output_tensor_spec: tensorflow.TensorSpec, hidden_layer_args: Sequence[dict[str, Any]] = ({'units': 50, 'activation': 'relu'}, {'units': 50, 'activation': 'relu'}), independent: bool = False)[source]# Bases:
KerasEnsembleNetwork
This class defines layers of a probabilistic neural network using Keras. The network architecture is a multilayer fully-connected feed-forward network, with Gaussian distribution as an output. The layers are meant to be built as an ensemble model by
KerasEnsemble
. Note that this is not a Bayesian neural network.- Parameters
input_tensor_spec – Tensor specification for the input to the network.
output_tensor_spec – Tensor specification for the output of the network.
hidden_layer_args – Specification for building dense hidden layers. Each element in the sequence should be a dictionary containing arguments (keys) and their values for a
Dense
hidden layer. Please check Keras Dense layer API for available arguments. Objects in the sequence will sequentially be used to addDense
layers. Length of this sequence determines the number of hidden layers in the network. Default value is two hidden layers, 50 nodes each, with ReLu activation functions. Empty sequence needs to be passed to have no hidden layers.independent – In case multiple outputs are modeled, if set to True then
IndependentNormal
layer is used as the output layer. This models outputs as independent, only the diagonal elements of the covariance matrix are parametrized. If left as the default False, thenMultivariateNormalTriL
layer is used where correlations between outputs are learned as well.
- Raises
ValueError – If objects in
hidden_layer_args
are not dictionaries.
-
connect_layers
() → tuple[tensorflow.Tensor, tensorflow.Tensor][source]# Connect all layers in the network. We start by generating an input tensor based on input tensor specification. Next we generate a sequence of hidden dense layers based on hidden layer arguments. Finally, we generate a dense layer whose nodes act as parameters of a Gaussian distribution in the final probabilistic layer.
- Returns
Input and output tensor of the sequence of layers.