test_kernel.py 1.72 KB
Newer Older
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np
import unittest
from kernel import gauss_kernel, double_gauss_kernel

class test_kernel(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('setupClass')

    @classmethod
    def tearDownClass(cls):
        print('teardownClass')

    def setUp(self):
        print('setUp')
        #self.kernel = gauss_kernel()
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
18
19
        #self.kernel_old = C(10, (1e1, 1e6)) * RBF(10, (1,1000))
        
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
20
21
        #self.kernel = double_gauss_kernel(Nsplit_eta=2, eta=10)
        self.kernel = gauss_kernel(Nsplit_eta=2, eta=10)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
22
23
24

    def tearDown(self):
        print('tearDown\n')
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
25

Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
26
27
28
29
30
31
32
33
34
35
36
    def test_jac(self):
        x1 = np.array([1.,2.,3.])
        x2 = np.array([1.5,2.,1.])
        x3 = np.array([3.,1.,1.])
        y = np.c_[x1,x2,x3].T
        np.testing.assert_almost_equal(self.kernel.kernel_jacobian(x1,x2),
                                       self.kernel.numerical_jacobian(x1,x2))
        np.testing.assert_almost_equal(self.kernel.kernel_jacobian(x1,y),
                                       self.kernel.numerical_jacobian(x1,y))
        np.testing.assert_almost_equal(self.kernel.kernel_jacobian(x2,y),
                                       self.kernel.numerical_jacobian(x2,y))
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
37
        
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
38
39
40
41
    def test_hyper_jac(self):
        x1 = np.array([1.,2.,3.])
        x2 = np.array([1.5,2.,1.])
        x3 = np.array([3.,1.,1.])
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
42
43
        X = np.c_[x1,x2].T
        K_ddtheta = self.kernel.kernel_hyperparameter_gradient(X)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
44
        K_ddtheta_num = self.kernel.numerical_hyperparameter_gradient(X)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
45
46
47
        #print(K_ddtheta, '\n\n')
        #print(K_ddtheta_num, '\n\n')
        #print(K_ddtheta-K_ddtheta_num, '\n')
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
48
49
        np.testing.assert_almost_equal(K_ddtheta, K_ddtheta_num)

Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
50
51
52
if __name__ == '__main__':
    unittest.main()