ottertune/server/analysis/tests/test_gpr.py

175 lines
6.0 KiB
Python
Raw Normal View History

2019-08-23 08:47:19 -07:00
#
# OtterTune - test_gpr.py
#
# Copyright (c) 2017-18, Carnegie Mellon University Database Group
#
import unittest
2019-11-30 12:58:25 -08:00
import random
2019-11-29 16:39:20 -08:00
import numpy as np
2019-11-30 12:58:25 -08:00
import gpflow
import tensorflow as tf
2019-08-23 08:47:19 -07:00
from sklearn import datasets
from analysis.gp import GPRNP
from analysis.gp_tf import GPR
2019-11-29 16:39:20 -08:00
from analysis.gp_tf import GPRGD
2019-11-30 12:58:25 -08:00
from analysis.gpr import gpr_models
from analysis.gpr.optimize import tf_optimize
2020-01-22 04:38:18 -08:00
from analysis.gpr.predict import gpflow_predict
2019-08-23 08:47:19 -07:00
# test numpy version GPR
class TestGPRNP(unittest.TestCase):
@classmethod
def setUpClass(cls):
super(TestGPRNP, cls).setUpClass()
boston = datasets.load_boston()
data = boston['data']
X_train = data[0:500]
X_test = data[500:]
y_train = boston['target'][0:500].reshape(500, 1)
cls.model = GPRNP(length_scale=1.0, magnitude=1.0)
cls.model.fit(X_train, y_train, ridge=1.0)
cls.gpr_result = cls.model.predict(X_test)
2020-01-22 04:38:18 -08:00
def test_ypreds(self):
2019-08-23 08:47:19 -07:00
ypreds_round = [round(x[0], 4) for x in self.gpr_result.ypreds]
expected_ypreds = [0.0181, 0.0014, 0.0006, 0.0015, 0.0039, 0.0014]
self.assertEqual(ypreds_round, expected_ypreds)
2020-01-22 04:38:18 -08:00
def test_sigmas(self):
2019-08-23 08:47:19 -07:00
sigmas_round = [round(x[0], 4) for x in self.gpr_result.sigmas]
2019-11-29 16:39:20 -08:00
expected_sigmas = [1.4142, 1.4142, 1.4142, 1.4142, 1.4142, 1.4142]
2019-08-23 08:47:19 -07:00
self.assertEqual(sigmas_round, expected_sigmas)
# test Tensorflow version GPR
class TestGPRTF(unittest.TestCase):
@classmethod
def setUpClass(cls):
super(TestGPRTF, cls).setUpClass()
boston = datasets.load_boston()
data = boston['data']
X_train = data[0:500]
X_test = data[500:]
y_train = boston['target'][0:500].reshape(500, 1)
2019-11-30 00:30:35 -08:00
cls.model = GPR(length_scale=1.0, magnitude=1.0, ridge=1.0)
cls.model.fit(X_train, y_train)
2019-08-23 08:47:19 -07:00
cls.gpr_result = cls.model.predict(X_test)
2020-01-22 04:38:18 -08:00
def test_ypreds(self):
2019-08-23 08:47:19 -07:00
ypreds_round = [round(x[0], 4) for x in self.gpr_result.ypreds]
expected_ypreds = [0.0181, 0.0014, 0.0006, 0.0015, 0.0039, 0.0014]
self.assertEqual(ypreds_round, expected_ypreds)
2020-01-22 04:38:18 -08:00
def test_sigmas(self):
2019-08-23 08:47:19 -07:00
sigmas_round = [round(x[0], 4) for x in self.gpr_result.sigmas]
2019-11-29 16:39:20 -08:00
expected_sigmas = [1.4142, 1.4142, 1.4142, 1.4142, 1.4142, 1.4142]
self.assertEqual(sigmas_round, expected_sigmas)
2020-01-20 00:09:01 -08:00
# test GPFlow version GPR
2020-01-22 04:38:18 -08:00
class TestGPRGPFlow(unittest.TestCase):
2020-01-20 00:09:01 -08:00
@classmethod
def setUpClass(cls):
2020-01-22 04:38:18 -08:00
super(TestGPRGPFlow, cls).setUpClass()
2020-01-20 00:09:01 -08:00
boston = datasets.load_boston()
data = boston['data']
X_train = data[0:500]
X_test = data[500:]
y_train = boston['target'][0:500].reshape(500, 1)
model_kwargs = {'lengthscales': 1, 'variance': 1, 'noise_variance': 1}
tf.reset_default_graph()
graph = tf.get_default_graph()
gpflow.reset_default_session(graph=graph)
cls.m = gpr_models.create_model('BasicGP', X=X_train, y=y_train,
**model_kwargs)
cls.gpr_result = gpflow_predict(cls.m.model, X_test)
2020-01-22 04:38:18 -08:00
def test_ypreds(self):
2020-01-20 00:09:01 -08:00
ypreds_round = [round(x[0], 4) for x in self.gpr_result.ypreds]
expected_ypreds = [0.0181, 0.0014, 0.0006, 0.0015, 0.0039, 0.0014]
self.assertEqual(ypreds_round, expected_ypreds)
2020-01-22 04:38:18 -08:00
def test_sigmas(self):
2020-01-20 00:09:01 -08:00
sigmas_round = [round(x[0], 4) for x in self.gpr_result.sigmas]
expected_sigmas = [1.4142, 1.4142, 1.4142, 1.4142, 1.4142, 1.4142]
self.assertEqual(sigmas_round, expected_sigmas)
2020-01-22 04:38:18 -08:00
# test GPFlow version Gradient Descent
class TestGDGPFlow(unittest.TestCase):
2019-11-29 16:39:20 -08:00
@classmethod
def setUpClass(cls):
2020-01-22 04:38:18 -08:00
super(TestGDGPFlow, cls).setUpClass()
2019-11-30 12:58:25 -08:00
boston = datasets.load_boston()
data = boston['data']
X_train = data[0:500]
X_test = data[500:501]
y_train = boston['target'][0:500].reshape(500, 1)
X_min = np.min(X_train, 0)
X_max = np.max(X_train, 0)
random.seed(0)
np.random.seed(0)
tf.set_random_seed(0)
model_kwargs = {}
opt_kwargs = {}
opt_kwargs['learning_rate'] = 0.01
opt_kwargs['maxiter'] = 10
opt_kwargs['bounds'] = [X_min, X_max]
opt_kwargs['ucb_beta'] = 1.0
tf.reset_default_graph()
graph = tf.get_default_graph()
gpflow.reset_default_session(graph=graph)
cls.m = gpr_models.create_model('BasicGP', X=X_train, y=y_train, **model_kwargs)
cls.gpr_result = tf_optimize(cls.m.model, X_test, **opt_kwargs)
2020-01-22 04:38:18 -08:00
def test_ypreds(self):
2019-11-30 12:58:25 -08:00
ypreds_round = [round(x[0], 4) for x in self.gpr_result.ypreds]
expected_ypreds = [0.5272]
self.assertEqual(ypreds_round, expected_ypreds)
2020-01-22 04:38:18 -08:00
def test_sigmas(self):
2019-11-30 12:58:25 -08:00
sigmas_round = [round(x[0], 4) for x in self.gpr_result.sigmas]
expected_sigmas = [1.4153]
self.assertEqual(sigmas_round, expected_sigmas)
2020-01-22 04:38:18 -08:00
# test Tensorflow version Gradient Descent
class TestGDTF(unittest.TestCase):
2019-11-30 12:58:25 -08:00
@classmethod
def setUpClass(cls):
2020-01-22 04:38:18 -08:00
super(TestGDTF, cls).setUpClass()
2019-11-30 12:58:25 -08:00
boston = datasets.load_boston()
data = boston['data']
X_train = data[0:500]
X_test = data[500:501]
y_train = boston['target'][0:500].reshape(500, 1)
Xmin = np.min(X_train, 0)
Xmax = np.max(X_train, 0)
random.seed(0)
np.random.seed(0)
tf.set_random_seed(0)
cls.model = GPRGD(length_scale=2.0, magnitude=1.0, max_iter=10, learning_rate=0.01,
ridge=1.0, hyperparameter_trainable=True, sigma_multiplier=1.0)
cls.model.fit(X_train, y_train, Xmin, Xmax)
cls.gpr_result = cls.model.predict(X_test)
2020-01-22 04:38:18 -08:00
def test_ypreds(self):
2019-11-30 12:58:25 -08:00
ypreds_round = [round(x[0], 4) for x in self.gpr_result.ypreds]
expected_ypreds = [0.5272]
self.assertEqual(ypreds_round, expected_ypreds)
2020-01-22 04:38:18 -08:00
def test_sigmas(self):
2019-11-30 12:58:25 -08:00
sigmas_round = [round(x[0], 4) for x in self.gpr_result.sigmas]
expected_sigmas = [1.4153]
self.assertEqual(sigmas_round, expected_sigmas)