Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fixed badges and added tests for gp reg #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions 12 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ install:
- pip install coveralls travis-sphinx==2.0.0

env:
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append --ignore=pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append --ignore=pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append --ignore=pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"

script:
- . ./scripts/test.sh $TESTCMD
Expand Down
8 changes: 4 additions & 4 deletions 8 README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ project: https://github.com/parsing-science/pymc3_models.
.. |Binder| image:: https://mybinder.org/badge.svg
:target: https://mybinder.org/v2/gh/pymc-learn/pymc-learn/master?filepath=%2Fdocs%2Fnotebooks?urlpath=lab

.. |Travis| image:: https://api.travis-ci.org/pymc-learn/pymc-learn.svg?branch=master
:target: https://travis-ci.org/pymc-learn/pymc-learn
.. |Travis| image:: https://travis-ci.com/pymc-learn/pymc-learn.svg?branch=master
:target: https://travis-ci.com/pymc-learn/pymc-learn

.. |Coverage| image:: https://coveralls.io/repos/github/pymc-learn/pymc-learn/badge.svg?branch=master
:target: https://coveralls.io/github/pymc-learn/pymc-learn?branch=master
.. |Coverage| image:: https://coveralls.io/repos/github/pymc-learn/pymc-learn/badge.svg
:target: https://coveralls.io/github/pymc-learn/pymc-learn

.. |Python27| image:: https://img.shields.io/badge/python-2.7-blue.svg
:target: https://badge.fury.io/py/pymc-learn
Expand Down
222 changes: 106 additions & 116 deletions 222 pmlearn/gaussian_process/tests/test_gpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,56 @@
#
# License: BSD 3 clause

# import pytest
import pytest
import numpy.testing as npt
# import pandas.testing as pdt
import pandas.testing as pdt
import shutil
import tempfile

import numpy as np
import pymc3 as pm
# from pymc3 import summary
# from sklearn.gaussian_process import \
# GaussianProcessRegressor as skGaussianProcessRegressor
# from sklearn.model_selection import train_test_split
#
#
# from pmlearn.exceptions import NotFittedError
from pymc3 import summary
from sklearn.gaussian_process import \
GaussianProcessRegressor as skGaussianProcessRegressor
from sklearn.model_selection import train_test_split


from pmlearn.exceptions import NotFittedError
from pmlearn.gaussian_process import (GaussianProcessRegressor)
# ,
# SparseGaussianProcessRegressor,
# StudentsTProcessRegressor)


class TestGaussianProcessRegressor(object):
"""
Compare the logp of GPR models in pmlearn to sklearn
"""

def setup_method(self):
"""Setup the data for testing
"""
self.num_pred = 1
self.num_training_samples = 20
self.length_scale = 0.1
self.signal_variance = 0.01
self.noise_variance = 0.01
self.X = np.random.randn(self.num_training_samples, self.num_pred)
self.y = np.random.randn(self.num_training_samples) * \
self.noise_variance
self.Xnew = np.random.randn(50, self.num_pred)
self.pnew = np.random.randn(50) * self.noise_variance
with pm.Model() as model:
cov_func = self.signal_variance**2 * \
pm.gp.cov.ExpQuad(self.num_pred, self.length_scale)
gp = pm.gp.Latent(cov_func=cov_func)
f = gp.prior("f", self.X, reparameterize=False)
p = gp.conditional("p", self.Xnew)

self.latent_logp = model.logp({"f": self.y, "p": self.pnew})
self.plogp = p.logp({"f": self.y, "p": self.pnew})

self.test_gpr = GaussianProcessRegressor(kernel=cov_func)
self.num_training_samples = 300

self.length_scale = 1.0
self.signal_variance = 0.1
self.noise_variance = 0.1

X = np.linspace(start=0, stop=10,
num=self.num_training_samples)[:, None]

cov_func = self.signal_variance ** 2 * pm.gp.cov.ExpQuad(
1, self.length_scale)
mean_func = pm.gp.mean.Zero()

f_true = np.random.multivariate_normal(
mean_func(X).eval(),
cov_func(X).eval() + 1e-8 * np.eye(self.num_training_samples),
1).flatten()
y = f_true + \
self.noise_variance * np.random.randn(self.num_training_samples)

self.X_train, self.X_test, self.y_train, self.y_test = \
train_test_split(X, y, test_size=0.3)

self.advi_gpr = GaussianProcessRegressor()

self.test_dir = tempfile.mkdtemp()

def teardown_method(self):
Expand All @@ -67,93 +67,83 @@ def test_advi_fit_returns_correct_model(self):
# This print statement ensures PyMC3 output won't overwrite
# the test name
print('')
self.test_gpr.fit(self.X, self.y)

npt.assert_equal(self.num_pred, self.test_gpr.num_pred)
npt.assert_almost_equal(self.signal_variance,
int(self.test_GPR.summary['mean']['signal_variance__0']),
0)
self.assertAlmostEqual(self.length_scale,
int(self.test_GPR.summary['mean']['length_scale__0_0']),
0)
self.assertAlmostEqual(self.noise_variance,
int(self.test_GPR.summary['mean']['noise_variance__0']),
0)
self.advi_gpr.fit(self.X_train, self.y_train,
inference_args={"n": 25000})

# def test_nuts_fit_returns_correct_model(self):
# # This print statement ensures PyMC3 output won't overwrite the test name
# print('')
# self.test_nuts_GPR.fit(self.X_train, self.y_train, inference_type='nuts')
#
# self.assertEqual(self.num_pred, self.test_nuts_GPR.num_pred)
# self.assertAlmostEqual(self.signal_variance,
# int(self.test_nuts_GPR.summary['mean']['signal_variance__0']),
# 0)
# self.assertAlmostEqual(self.length_scale,
# int(self.test_nuts_GPR.summary['mean']['length_scale__0_0']),
# 0)
# self.assertAlmostEqual(self.noise_variance,
# int(self.test_nuts_GPR.summary['mean']['noise_variance__0']),
# 0)
npt.assert_equal(self.num_pred, self.advi_gpr.num_pred)
npt.assert_almost_equal(
self.signal_variance,
self.advi_gpr.summary['mean']['signal_variance__0'],
0)
npt.assert_almost_equal(
self.length_scale,
self.advi_gpr.summary['mean']['length_scale__0_0'],
0)
npt.assert_almost_equal(
self.noise_variance,
self.advi_gpr.summary['mean']['noise_variance__0'],
0)


# class GaussianProcessRegressorPredictTestCase(GaussianProcessRegressorTestCase):
# def test_predict_returns_predictions(self):
# print('')
# self.test_GPR.fit(self.X_train, self.y_train)
# preds = self.test_GPR.predict(self.X_test)
# self.assertEqual(self.y_test.shape, preds.shape)
#
# def test_predict_returns_mean_predictions_and_std(self):
# print('')
# self.test_GPR.fit(self.X_train, self.y_train)
# preds, stds = self.test_GPR.predict(self.X_test, return_std=True)
# self.assertEqual(self.y_test.shape, preds.shape)
# self.assertEqual(self.y_test.shape, stds.shape)
#
# def test_predict_raises_error_if_not_fit(self):
# print('')
# with self.assertRaises(NotFittedError) as no_fit_error:
# test_GPR = GaussianProcessRegressor()
# test_GPR.predict(self.X_train)
#
# expected = 'Run fit on the model before predict.'
# self.assertEqual(str(no_fit_error.exception), expected)
class TestGaussianProcessRegressorPredict(TestGaussianProcessRegressor):
def test_predict_returns_predictions(self):
print('')
self.advi_gpr.fit(self.X_train, self.y_train,
inference_args={"n": 25000})
preds = self.advi_gpr.predict(self.X_test)
npt.assert_equal(self.y_test.shape, preds.shape)

def test_predict_returns_mean_predictions_and_std(self):
print('')
self.advi_gpr.fit(self.X_train, self.y_train,
inference_args={"n": 25000})
preds, stds = self.advi_gpr.predict(self.X_test, return_std=True)
npt.assert_equal(self.y_test.shape, preds.shape)
npt.assert_equal(self.y_test.shape, stds.shape)

def test_predict_raises_error_if_not_fit(self):
print('')
with pytest.raises(NotFittedError):
advi_gpr = GaussianProcessRegressor()
advi_gpr.predict(self.X_train)


class TestGaussianProcessRegressorScore(TestGaussianProcessRegressor):
def test_score_matches_sklearn_performance(self):
print('')
sk_gpr = skGaussianProcessRegressor()
sk_gpr.fit(self.X_train, self.y_train)
sk_gpr_score = sk_gpr.score(self.X_test, self.y_test)

self.advi_gpr.fit(self.X_train, self.y_train,
inference_args={"n": 25000})
advi_gpr_score = self.advi_gpr.score(self.X_test, self.y_test)

npt.assert_almost_equal(sk_gpr_score, advi_gpr_score, 1)


class TestGaussianProcessRegressorSaveAndLoad(TestGaussianProcessRegressor):
def test_save_and_load_work_correctly(self):
print('')
self.advi_gpr.fit(self.X_train, self.y_train,
inference_args={"n": 25000})
score1 = self.advi_gpr.score(self.X_test, self.y_test)
self.advi_gpr.save(self.test_dir)

gpr2 = GaussianProcessRegressor()
gpr2.load(self.test_dir)

npt.assert_equal(self.advi_gpr.inference_type, gpr2.inference_type)
npt.assert_equal(self.advi_gpr.num_pred, gpr2.num_pred)
npt.assert_equal(self.advi_gpr.num_training_samples,
gpr2.num_training_samples)
pdt.assert_frame_equal(summary(self.advi_gpr.trace),
summary(gpr2.trace))

score2 = gpr2.score(self.X_test, self.y_test)
npt.assert_almost_equal(score1, score2, 0)


# class GaussianProcessRegressorScoreTestCase(GaussianProcessRegressorTestCase):
# def test_score_matches_sklearn_performance(self):
# print('')
# skGPR = skGaussianProcessRegressor()
# skGPR.fit(self.X_train, self.y_train)
# skGPR_score = skGPR.score(self.X_test, self.y_test)
#
# self.test_GPR.fit(self.X_train, self.y_train)
# test_GPR_score = self.test_GPR.score(self.X_test, self.y_test)
#
# self.assertAlmostEqual(skGPR_score, test_GPR_score, 1)
#
#
# class GaussianProcessRegressorSaveAndLoadTestCase(GaussianProcessRegressorTestCase):
# def test_save_and_load_work_correctly(self):
# print('')
# self.test_GPR.fit(self.X_train, self.y_train)
# score1 = self.test_GPR.score(self.X_test, self.y_test)
# self.test_GPR.save(self.test_dir)
#
# GPR2 = GaussianProcessRegressor()
# GPR2.load(self.test_dir)
#
# self.assertEqual(self.test_GPR.inference_type, GPR2.inference_type)
# self.assertEqual(self.test_GPR.num_pred, GPR2.num_pred)
# self.assertEqual(self.test_GPR.num_training_samples, GPR2.num_training_samples)
# pd.testing.assert_frame_equal(summary(self.test_GPR.trace),
# summary(GPR2.trace))
#
# score2 = GPR2.score(self.X_test, self.y_test)
# self.assertAlmostEqual(score1, score2, 1)
#
#
# class StudentsTProcessRegressorTestCase(unittest.TestCase):
#
# def setUp(self):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.