Generator function to create two layer GPAM model in GPFlux.
Args:
num_input (int): Number of input dimensions.
num_data (int, optional): Number of data points used for training, important to calculate loss properly. Defaults to 1.
Z (ndarray, optional): Array of inducing locations. Defaults to None - will be generated at random.
num_inducing (int, optional): Number of inducing points. Defaults to 50.
return_layers (bool, optional): Set to true if individual layers are to be returned alongside model. Defaults to False.
n_latent (int, optional): Dimension of latent space. Defaults to 2.
Source code in GPyEDS/GPAM.py
51
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 | def create_two_layer_GPAM_from_scratch(num_input, num_data = 1, Z = None, num_inducing = 50, return_layers = False, n_latent = 2):
"""Generator function to create two layer GPAM model in GPFlux.
Args:
num_input (int): Number of input dimensions.
num_data (int, optional): Number of data points used for training, important to calculate loss properly. Defaults to 1.
Z (ndarray, optional): Array of inducing locations. Defaults to None - will be generated at random.
num_inducing (int, optional): Number of inducing points. Defaults to 50.
return_layers (bool, optional): Set to true if individual layers are to be returned alongside model. Defaults to False.
n_latent (int, optional): Dimension of latent space. Defaults to 2.
Returns:
model (gpflux model): final model object.
"""
if Z is not None:
pass
else:
Z = np.random.rand(num_inducing, num_input)
kernel1 = gpflow.kernels.SquaredExponential(lengthscales=[1]*num_input)
inducing_variable1 = gpflow.inducing_variables.InducingPoints(Z.copy())
gp_layer1 = gpflux.layers.GPLayer(
kernel1, inducing_variable1, num_data=num_data, num_latent_gps=n_latent,mean_function=gpflow.mean_functions.Zero()
)
kernel2 = gpflow.kernels.SquaredExponential(lengthscales=[1]*n_latent)
inducing_variable2 = gpflow.inducing_variables.InducingPoints(np.random.rand(num_inducing,n_latent))
gp_layer2 = gpflux.layers.GPLayer(
kernel2,
inducing_variable2,
num_data=num_data,
num_latent_gps=num_input,
mean_function=gpflow.mean_functions.Zero(),
)
likelihood_layer = gpflux.layers.LikelihoodLayer(gpflow.likelihoods.Gaussian(0.1))
two_layer_dgp = gpflux.models.DeepGP([gp_layer1, gp_layer2], likelihood_layer)
model = two_layer_dgp.as_training_model()
model.compile(tf.optimizers.Adam(0.01))
if return_layers:
return model, gp_layer1, gp_layer2
else:
return model
|