Skip to content

Advanced

gpplus.means.advanced_means.CompositeMean ¤

Bases: Mean

A mean function that applies a user-provided transformation module to the input. The transformation is expected to yield a single-dimensional output (shape (batch_size, 1) or (batch_size,)) which represents the mean.

This can be any valid callable or nn.Module, including: - Polynomial feature expansions - Small neural networks - Other custom transforms

Unlike a typical 'base_mean', this class just returns the output of the provided transformation.

Source code in gpplus/means/advanced_means.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class CompositeMean(Mean):
    """
    A mean function that applies a user-provided transformation module
    to the input. The transformation is expected to yield a single-dimensional
    output (shape (batch_size, 1) or (batch_size,)) which represents the mean.

    This can be any valid callable or nn.Module, including:
      - Polynomial feature expansions
      - Small neural networks
      - Other custom transforms

    Unlike a typical 'base_mean', this class just returns the
    output of the provided transformation.
    """

    def __init__(self, input_transform):
        """
        Args:
            input_transform (callable or nn.Module):
                A user-defined transformation that takes x of shape (batch_size, input_dim)
                and returns a single-dimensional output (batch_size,).
        """
        super().__init__()
        self.input_transform = input_transform

    def forward(self, x):
        """
        Applies the user-provided transformation and returns its output
        as the mean.

        Args:
            x (torch.Tensor): Input of shape (batch_size, input_dim).

        Returns:
            torch.Tensor: Mean predictions of shape (batch_size,).
        """
        # Ensure final shape is (batch_size,) by squeezing if needed
        return self.input_transform(x).squeeze(-1)
input_transform = input_transform instance-attribute ¤
__init__(input_transform) ¤

Parameters:

Name Type Description Default
input_transform callable or Module

A user-defined transformation that takes x of shape (batch_size, input_dim) and returns a single-dimensional output (batch_size,).

required
Source code in gpplus/means/advanced_means.py
24
25
26
27
28
29
30
31
32
def __init__(self, input_transform):
    """
    Args:
        input_transform (callable or nn.Module):
            A user-defined transformation that takes x of shape (batch_size, input_dim)
            and returns a single-dimensional output (batch_size,).
    """
    super().__init__()
    self.input_transform = input_transform
forward(x) ¤

Applies the user-provided transformation and returns its output as the mean.

Parameters:

Name Type Description Default
x Tensor

Input of shape (batch_size, input_dim).

required

Returns:

Type Description

torch.Tensor: Mean predictions of shape (batch_size,).

Source code in gpplus/means/advanced_means.py
34
35
36
37
38
39
40
41
42
43
44
45
46
def forward(self, x):
    """
    Applies the user-provided transformation and returns its output
    as the mean.

    Args:
        x (torch.Tensor): Input of shape (batch_size, input_dim).

    Returns:
        torch.Tensor: Mean predictions of shape (batch_size,).
    """
    # Ensure final shape is (batch_size,) by squeezing if needed
    return self.input_transform(x).squeeze(-1)

gpplus.means.advanced_means.NeuralMean ¤

Bases: Mean

A mean function that builds its own MLP internally using InputTransformNet. The final layer of InputTransformNet must have dims=1, so that the network outputs a single scalar per data point.

For example, a valid layer_config might look like:

layer_config = {
    0: {"dims": 64, "activation": nn.ReLU},
    1: {"dims": 32, "activation": nn.ReLU},
    2: {"dims": 1,  "activation": nn.Identity},
}

This ensures the network outputs shape (batch_size, 1), which we then squeeze to (batch_size,).

Source code in gpplus/means/advanced_means.py
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 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
class NeuralMean(Mean):
    """
    A mean function that builds its own MLP internally using InputTransformNet.
    The final layer of InputTransformNet must have `dims=1`, so that the
    network outputs a single scalar per data point.

    For example, a valid layer_config might look like:

        layer_config = {
            0: {"dims": 64, "activation": nn.ReLU},
            1: {"dims": 32, "activation": nn.ReLU},
            2: {"dims": 1,  "activation": nn.Identity},
        }

    This ensures the network outputs shape (batch_size, 1), which we
    then squeeze to (batch_size,).
    """

    def __init__(self, input_dim, layer_config):
        """
        Args:
            input_dim (int): Number of input features.
            layer_config (dict): Configuration for each layer, where the last layer
                                 must have 'dims' == 1.
        """
        super().__init__()
        # Build the main transform network
        self.transform_net = InputTransformNet(input_dim, layer_config)

        # Check that the last layer indeed produces 1 dimension
        last_layer_idx = max(layer_config.keys())  # highest key
        last_layer_dims = layer_config[last_layer_idx]["dims"]
        if last_layer_dims != 1:
            raise ValueError(
                f"For NeuralMean, the final layer in `layer_config` must have dims=1, but got {last_layer_dims}."
            )

    def forward(self, x):
        """
        Computes the mean by passing inputs through the internal network.

        Args:
            x (torch.Tensor): Input of shape (batch_size, input_dim).

        Returns:
            torch.Tensor: Mean predictions of shape (batch_size,).
        """
        out = self.transform_net(x)  # shape (batch_size, 1)
        return out.squeeze(-1)  # shape (batch_size,)
transform_net = InputTransformNet(input_dim, layer_config) instance-attribute ¤
__init__(input_dim, layer_config) ¤

Parameters:

Name Type Description Default
input_dim int

Number of input features.

required
layer_config dict

Configuration for each layer, where the last layer must have 'dims' == 1.

required
Source code in gpplus/means/advanced_means.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def __init__(self, input_dim, layer_config):
    """
    Args:
        input_dim (int): Number of input features.
        layer_config (dict): Configuration for each layer, where the last layer
                             must have 'dims' == 1.
    """
    super().__init__()
    # Build the main transform network
    self.transform_net = InputTransformNet(input_dim, layer_config)

    # Check that the last layer indeed produces 1 dimension
    last_layer_idx = max(layer_config.keys())  # highest key
    last_layer_dims = layer_config[last_layer_idx]["dims"]
    if last_layer_dims != 1:
        raise ValueError(
            f"For NeuralMean, the final layer in `layer_config` must have dims=1, but got {last_layer_dims}."
        )
forward(x) ¤

Computes the mean by passing inputs through the internal network.

Parameters:

Name Type Description Default
x Tensor

Input of shape (batch_size, input_dim).

required

Returns:

Type Description

torch.Tensor: Mean predictions of shape (batch_size,).

Source code in gpplus/means/advanced_means.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def forward(self, x):
    """
    Computes the mean by passing inputs through the internal network.

    Args:
        x (torch.Tensor): Input of shape (batch_size, input_dim).

    Returns:
        torch.Tensor: Mean predictions of shape (batch_size,).
    """
    out = self.transform_net(x)  # shape (batch_size, 1)
    return out.squeeze(-1)  # shape (batch_size,)