1

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.

4

2 回答 2

4

您遇到的问题是由于我们在 GPflow 中更新参数值的新方式。而不是这样做model.parameter = value,你应该使用assign

m.likelihood.variance.assign(0.01) 

这确保参数的类型不会改变。

lengthscale将内核设置为0.25.

在此处输入图像描述

于 2020-02-24T17:34:20.170 回答
0

我在这里问(并回答了自己)同样的问题:GPFlow-2.0 - 问题与 default_float 和似然方差

一般来说,如果您尝试使用 gpflow 2.0,我建议不要使用 readthedocs 上的示例。相反,克隆 github 存储库并使用那里的示例笔记本 - 这些大部分已更新为与 gpflow 2 一起正常工作。

例如,您的回归示例的更新版本可在此处获得:https ://github.com/GPflow/GPflow/blob/develop/doc/source/notebooks/basics/regression.pct.py

此版本适用于 gpflow 2。

于 2020-03-03T13:02:12.363 回答