I am trying to run the code from the gpflow tutorial: https://gpflow.readthedocs.io/en/stable/notebooks/regression.html However, it doesn't work.
The following code:
N = 12
X = np.random.rand(N,1)
Y = np.sin(12*X) + 0.66*np.cos(25*X) + np.random.randn(N,1)*0.1 + 3
plt.plot(X, Y, 'kx', mew=2)
k = gpflow.kernels.Matern52(variance=1.0, lengthscale=1.0)
m = gpflow.models.GPR((X, Y), k, mean_function=None, noise_variance=1.0)
m.likelihood.variance = 0.01
def plot(m):
xx = np.linspace(-0.2, 1.2, 141)[:,None]
xx=tf.convert_to_tensor(xx,dtype=tf.float64)
mean, var = m.predict_y(xx)
plt.figure(figsize=(12, 6))
plt.plot(X, Y, 'kx', mew=2)
plt.plot(xx, mean, 'b', lw=2)
plt.fill_between(xx[:,0], mean[:,0] - 2*np.sqrt(var[:,0]), mean[:,0] + 2*np.sqrt(var[:,0]), color='blue', alpha=0.2)
plt.xlim(-0.1, 1.1)
plot(m)
returns the following error:
InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:AddV2] name: add/
I have windows 10, python 3.6, tensorflow 2.0, tensorflow probability 0.9, and gpflow was installed with pip install -e . command on 21st of february 2020.
Could you help me with this? I do transform the input to double so I think it could be that gpflow updated the code but not the tutorial.
The problem you encounter is due to new way we update parameter values in GPflow. Instead of doing model.parameter = value
you should use assign
:
m.likelihood.variance.assign(0.01)
This makes sure the type of the parameters is not changed.
I was able to get the following plot, after setting the lengthscale
of the kernel to 0.25
.