In [2]:
#import autogp
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf


/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

In [3]:
N_all = 200
N = 50

In [12]:
array1 = 5 * np.linspace(0, 1, num=N_all)

In [13]:
array1.shape


Out[13]:
(200,)

In [16]:
inputs = 5 * np.linspace(0, 1, num=N_all)[:, np.newaxis]

In [20]:
inputs.shape #200 points, unidimensional


Out[20]:
(200, 1)

In [21]:
outputs = np.sin(inputs)

In [22]:
outputs.shape


Out[22]:
(200, 1)

In [25]:
idx = np.arange(N_all)
idx


Out[25]:
array([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
        13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
        26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
        39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  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,  95,  96,  97,  98,  99, 100, 101, 102, 103,
       104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
       117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
       130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
       143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
       156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
       169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
       182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
       195, 196, 197, 198, 199])

In [27]:
np.random.shuffle(idx)
idx


Out[27]:
array([ 93,  49, 175,  69,  17,  81, 199,  27,   6,  34,  68,  47, 157,
       192,  91, 116, 120,  15,  26,   2, 115, 140, 152, 155,  65, 114,
        70,  89,  14,   3,  31, 183, 132,  10, 124, 160,  74, 165, 153,
       198, 169,  18,  52,  71, 102, 105,  57, 186,  92, 171, 103,  28,
        32, 173, 122, 195,   4, 128,  53, 174,  96, 119, 121, 134,  95,
         7, 185,  79,  41, 180,  35, 158, 143, 112,  25, 108, 100,  72,
        46, 109,  56,  67, 117, 135, 106, 168, 138,  61,  86,  30,  40,
        11,  98, 177, 162,  22, 163, 196, 182, 144, 179, 133, 150,  66,
        88, 187,  16, 190,  76, 137, 170, 188,  54,  64, 118,  63,  51,
       178,  90,  33,  82, 156,  77,  39, 147,  85,  94, 181, 176,  29,
        44,  58,  87,  42,  50, 146,  99, 113,  36, 161,  83,  13, 193,
       127, 172,  38,  59,   0,  75,   1,  97,  84, 191, 142,  48, 151,
       189, 184, 130, 111, 197, 154, 166,   5, 139,  78, 167, 126, 148,
        45,  43, 131, 149,  19, 129,   9,  24,  37, 136,  80, 125, 110,
       141,  12, 164, 194, 123,  60,  55, 107, 145, 104, 159,  73,  62,
       101,  21,  23,   8,  20])

In [29]:
xtrain = inputs[idx[:N]] #take 50 points in inputs corresponding to this 
# random indices
xtrain


Out[29]:
array([[ 2.33668342],
       [ 1.23115578],
       [ 4.39698492],
       [ 1.73366834],
       [ 0.42713568],
       [ 2.03517588],
       [ 5.        ],
       [ 0.67839196],
       [ 0.15075377],
       [ 0.85427136],
       [ 1.70854271],
       [ 1.18090452],
       [ 3.94472362],
       [ 4.8241206 ],
       [ 2.28643216],
       [ 2.91457286],
       [ 3.01507538],
       [ 0.37688442],
       [ 0.65326633],
       [ 0.05025126],
       [ 2.88944724],
       [ 3.51758794],
       [ 3.81909548],
       [ 3.89447236],
       [ 1.63316583],
       [ 2.86432161],
       [ 1.75879397],
       [ 2.2361809 ],
       [ 0.35175879],
       [ 0.07537688],
       [ 0.77889447],
       [ 4.59798995],
       [ 3.31658291],
       [ 0.25125628],
       [ 3.11557789],
       [ 4.0201005 ],
       [ 1.85929648],
       [ 4.14572864],
       [ 3.84422111],
       [ 4.97487437],
       [ 4.24623116],
       [ 0.45226131],
       [ 1.30653266],
       [ 1.7839196 ],
       [ 2.56281407],
       [ 2.63819095],
       [ 1.4321608 ],
       [ 4.67336683],
       [ 2.31155779],
       [ 4.29648241]])

In [35]:
ytrain = outputs[idx[:N]] #primi 50 punti 
ytrain.shape


Out[35]:
(50, 1)

In [ ]:
data = autogp.datasets.DataSet(xtrain, ytrain) #frame the dataset in
# the way it is supposed to work with AutoGP

In [34]:
xtest = inputs[idx[N:]] #dal 50esimo in poi 
xtest.shape


Out[34]:
(150, 1)

In [36]:
ytest = outputs[idx[N:]]
ytest.shape


Out[36]:
(150, 1)

In [ ]:
#Initialize the Gaussian process.
likelihood = autogp.likelihoods.Gaussian() #specifico la likelihood
kernel = [autogp.kernels.RadialBasis(1)] #specifico il kernel
inducing_inputs = xtrain

In [ ]:
#questa funzione andra' a costruire tutto il computational graph 
#per questa determinata forma di lik e kernel
model = autogp.GaussianProcess(likelihood, kernel, inducing_inputs)

In [ ]:
optimizer = tf.train.RMSPropOptimizer(0.005) #che optimizar utilizzare
#step di ottimizzazione dove run la sessione e specifico quanti
#setp voglio fare
model.fit(data, optimizer, loo_steps=50, var_steps=50, epochs=1000)

In [ ]:
# Predict new inputs.
ypred, _ = model.predict(xtest)

In [ ]:
# Plot results
plt.plot(xtrain, ytrain, '.', mew=2)
plt.plot(xtest, ytest, 'o', mew=2)
plt.plot(xtest, ypred, 'x', mew=2)
plt.show()