In [1]:
import gpflow
import numpy as np
import matplotlib
%matplotlib inline
matplotlib.rcParams['figure.figsize'] = (12, 6)
plt = matplotlib.pyplot
/Users/Flo/anaconda/envs/py36/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
In [2]:
def plot(m):
    xx = np.linspace(-0.1, 1.1, 100).reshape(100, 1)
    mean, var = m.predict_y(xx)
    plt.figure(figsize=(12, 6))
    plt.plot(X, Y, 'kx', mew=2)
    plt.plot(xx, mean, 'C0', lw=2)
    plt.fill_between(xx[:,0],
                     mean[:,0] - 2*np.sqrt(var[:,0]),
                     mean[:,0] + 2*np.sqrt(var[:,0]),
                     color='C0', alpha=0.2)
    plt.xlim(-0.1, 1.1)
In [3]:
# create data
N = 12
np.random.seed(42)
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)
plt.savefig("gpflow_data.pdf",type="pdf")
In [4]:
# create GP
k = gpflow.kernels.RBF(1, lengthscales=0.3)
m = gpflow.models.GPR(X, Y, k)
m.likelihood.variance = 0.01

# predict
plot(m)
In [5]:
# optimize hyperparameters
gpflow.train.ScipyOptimizer().minimize(m)

# predict
plot(m)

plt.savefig("gpflow_fit2.pdf",type="pdf")
INFO:tensorflow:Optimization terminated with:
  Message: b'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
  Objective function value: 15.243668
  Number of iterations: 17
  Number of functions evaluations: 18
In [6]:
# create GP
k = gpflow.kernels.Matern12(1,lengthscales=0.3)
m = gpflow.models.GPR(X, Y, k)

# optimize and predict
gpflow.train.ScipyOptimizer().minimize(m)
plot(m)

plt.savefig("gpflow_fit3.pdf",type="pdf")
INFO:tensorflow:Optimization terminated with:
  Message: b'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
  Objective function value: 14.297836
  Number of iterations: 16
  Number of functions evaluations: 20