gpflux.helpers#

This module contains helper functions for constructing MultioutputKernel, MultioutputInducingVariables, MeanFunction, and GPLayer objects.

Module Contents#

construct_basic_kernel(kernels: gpflow.kernels.Kernel | List[gpflow.kernels.Kernel], output_dim: int | None = None, share_hyperparams: bool = False) gpflow.kernels.MultioutputKernel[source]#

Construct a MultioutputKernel to use in GPLayers.

Parameters:
  • kernels – A single kernel or list of Kernels. - When a single kernel is passed, the same kernel is used for all outputs. Depending on share_hyperparams, the hyperparameters will be shared across outputs. You must also specify output_dim. - When a list of kernels is passed, each kernel in the list is used on a separate output dimension and a gpflow.kernels.SeparateIndependent is returned.

  • output_dim – The number of outputs. This is equal to the number of latent GPs in the GPLayer. When only a single kernel is specified for kernels, you must also specify output_dim. When a list of kernels is specified for kernels, we assume that len(kernels) == output_dim, and output_dim is not required.

  • share_hyperparams – If True, use the type of kernel and the same hyperparameters (variance and lengthscales) for the different outputs. Otherwise, the same type of kernel (Squared-Exponential, Matern12, and so on) is used for the different outputs, but the kernel can have different hyperparameter values for each.

construct_basic_inducing_variables(num_inducing: int | List[int], input_dim: int, output_dim: int | None = None, share_variables: bool = False, z_init: numpy.ndarray | None = None) gpflow.inducing_variables.MultioutputInducingVariables[source]#

Construct a compatible MultioutputInducingVariables to use in GPLayers.

Parameters:
  • num_inducing – The total number of inducing variables, M. This parameter can be freely chosen by the user. General advice is to set it as high as possible, but smaller than the number of datapoints. The computational complexity of the layer is cubic in M. If a list is passed, each element in the list specifies the number of inducing variables to use for each output_dim.

  • input_dim – The dimensionality of the input data (or features) X. Typically, this corresponds to X.shape[-1]. For InducingPoints, this specifies the dimensionality of Z.

  • output_dim – The dimensionality of the outputs (or targets) Y. Typically, this corresponds to Y.shape[-1] or the number of latent GPs. The parameter is used to determine the number of inducing variable sets to create when a different set is used for each output. The parameter is redundant when num_inducing is a list, because the code assumes that len(num_inducing) == output_dim.

  • share_variables – If True, use the same inducing variables for different outputs. Otherwise, create a different set for each output. Set this parameter to False when num_inducing is a list, because otherwise the two arguments contradict each other. If you set this parameter to True, you must also specify output_dim, because that is used to determine the number of inducing variable sets to create.

  • z_init – Raw values to use to initialise gpflow.inducing_variables.InducingPoints. If None (the default), values will be initialised from N(0, 1). The shape of z_init depends on the other input arguments. If a single set of inducing points is used for all outputs (that is, if share_variables is True), z_init should be rank two, with the dimensions [M, input_dim]. If a different set of inducing points is used for the outputs (ithat is, if num_inducing is a list, or if share_variables is False), z_init should be a rank three tensor with the dimensions [output_dim, M, input_dim].

construct_mean_function(X: numpy.ndarray, D_in: int, D_out: int) gpflow.mean_functions.MeanFunction[source]#

Return gpflow.mean_functions.Identity when D_in and D_out are equal. Otherwise, use the principal components of the inputs matrix X to build a Linear mean function.

Note

The returned mean function is set to be untrainable. To change this, use gpflow.set_trainable().

Parameters:
  • X – A data array with the shape [N, D_in] used to determine the principal components to use to create a Linear mean function when D_in != D_out.

  • D_in – The dimensionality of the input data (or features) X. Typically, this corresponds to X.shape[-1].

  • D_out – The dimensionality of the outputs (or targets) Y. Typically, this corresponds to Y.shape[-1] or the number of latent GPs in the layer.

construct_gp_layer(num_data: int, num_inducing: int, input_dim: int, output_dim: int, kernel_class: Type[gpflow.kernels.Stationary] = gpflow.kernels.SquaredExponential, z_init: numpy.ndarray | None = None, name: str | None = None) gpflux.layers.gp_layer.GPLayer[source]#
Builds a vanilla GP layer with a single kernel shared among all outputs,

shared inducing point variables and zero mean function.

Parameters:
  • num_data – total number of datapoints in the dataset, N. Typically corresponds to X.shape[0] == len(X).

  • num_inducing – total number of inducing variables, M. This parameter can be freely chosen by the user. General advice is to pick it as high as possible, but smaller than N. The computational complexity of the layer is cubic in M.

  • input_dim – dimensionality of the input data (or features) X. Typically, this corresponds to X.shape[-1].

  • output_dim – The dimensionality of the outputs (or targets) Y. Typically, this corresponds to Y.shape[-1].

  • kernel_class – The kernel class used by the layer. This can be as simple as gpflow.kernels.SquaredExponential, or more complex, for example, lambda **_: gpflow.kernels.Linear() + gpflow.kernels.Periodic(). It will be passed a lengthscales keyword argument.

  • z_init – The initial value for the inducing variable inputs.

  • name – The name for the GP layer.

make_dataclass_from_class(dataclass: Any, instance: object, **updates: object) Any[source]#

Take a regular object instance with a superset of fields for a dataclasses.dataclass (@dataclass-decorated class), and return an instance of the dataclass. The instance has all of the dataclass’s fields but might also have more. key=value keyword arguments supersede the fields in instance.

xavier_initialization_numpy(input_dim: int, output_dim: int) numpy.ndarray[source]#

Generate initial weights for a neural network layer with the given input and output dimensionality using the Xavier Glorot normal initialiser. From:

Glorot, Xavier, and Yoshua Bengio. “Understanding the difficulty of training deep feedforward neural networks.” Proceedings of the thirteenth international conference on artificial intelligence and statistics. JMLR Workshop and Conference Proceedings, 2010.

Draw samples from a normal distribution centred on \(0\) with standard deviation \(\sqrt(2 / (\text{input_dim} + \text{output_dim}))\).