try:
import gpflux
except ModuleNotFoundError:
%pip install gpflux
import gpflux
from gpflux.architectures import Config, build_constant_input_dim_deep_gp
from gpflux.models import DeepGP
try:
import tensorflow as tf
except ModuleNotFoundError:
%pip install tensorflow
import tensorflow as tf
import numpy as np
import pandas as pd
import gpflow
import gpflux
from gpflux.architectures import Config, build_constant_input_dim_deep_gp
from gpflux.models import DeepGP
tf.keras.backend.set_floatx("float64")
tf.get_logger().setLevel("INFO")
## Data
num_low = 25
num_high = 25
gap = -.1
noise = 0.0001
x = np.vstack((np.linspace(-1, -gap/2.0, num_low)[:, np.newaxis],
np.linspace(gap/2.0, 1, num_high)[:, np.newaxis])).reshape(-1,)
y = np.vstack((np.zeros((num_low, 1)), np.ones((num_high, 1))))
scale = np.sqrt(y.var())
offset = y.mean()
yhat = ((y-offset)/scale).reshape(-1,)
## Model
config = Config(
num_inducing=x.shape[0], inner_layer_qsqrt_factor=1e-3, likelihood_noise_variance=1e-3, whiten=True
)
deep_gp: DeepGP = build_constant_input_dim_deep_gp(
np.array(x.reshape(-1, 1)), num_layers=4, config=config)
training_model: tf.keras.Model =deep_gp.as_training_model()
training_model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.01))
callbacks = [ tf.keras.callbacks.ReduceLROnPlateau("loss", factor=0.95, patience=3, min_lr=1e-6, verbose=0),
gpflux.callbacks.TensorBoard(),
tf.keras.callbacks.ModelCheckpoint(filepath="ckpts/", save_weights_only=True, verbose=0),]
history = training_model.fit(
{"inputs": x.reshape(-1, 1),
"targets": y.reshape(-1, 1)},
batch_size=6,
epochs=1000,
callbacks=callbacks,
verbose=0,
)
## Predict
def plot(model, X, Y, ax=None):
if ax is None:
fig, ax = plt.subplots()
x = X
x_margin = 1.0
N = 50
X = np.linspace(X.min() - x_margin, X.max() + x_margin, N).reshape(-1, 1)
out = model(X)
mu = out.f_mean.numpy().squeeze()
var = out.f_var.numpy().squeeze()
X = X.squeeze()
lower = mu - 2 * np.sqrt(var)
upper = mu + 2 * np.sqrt(var)
ax.set_ylim(Y.min() - 0.5, Y.max() + 0.5)
ax.plot(x, Y, "kx", alpha=0.5)
ax.plot(X, mu, "C1")
ax.set_xlim(-2, 2)
ax.fill_between(X, lower, upper, color="C1", alpha=0.3)
prediction_model = deep_gp.as_prediction_model()
plot(prediction_model, x.reshape(-1, 1), y.reshape(-1, 1))
Describe the bug
I want to fit a Deep GP on step data, so I am using the method shown in GPflux tutorial on the motorcycle dataset. But the fit is not as expected, as shown in Prof.Neil Lawerence's blog. I can fit using PyDeepGP. I have attached the code used by me in GPflux and PyDeepGP
To reproduce
Steps to reproduce the behaviour:
GPflux Implementation
```Plot obtained as a result of the above code:
Expected behaviour
PyDeepGP Implementation
```Plot obtained from above code

System information