test_gpr.py 4.17 KB
Newer Older
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
1
2
3
4
import numpy as np
import unittest
from gpr import gpr

5
6
7
from ase.io import read

# old gpr
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
8
from kernels import RBF, ConstantKernel as C, WhiteKernel
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
from featureCalculators_multi.angular_fingerprintFeature_cy import Angular_Fingerprint
from delta_functions_multi.delta import delta as deltaFunc
from GPR import GPR

def initialize_old_gpr(atoms):
    ### Set up feature ###

    # Radial part
    Rc1 = 6
    binwidth1 = 0.2
    sigma1 = 0.2
    
    # Angular part
    Rc2 = 4
    Nbins2 = 30
    sigma2 = 0.2
    gamma = 2
    
    # Radial/angular weighting
    eta = 20
    use_angular = True
    
    # Initialize feature
    featureCalculator = Angular_Fingerprint(atoms, Rc1=Rc1, Rc2=Rc2, binwidth1=binwidth1, Nbins2=Nbins2, sigma1=sigma1, sigma2=sigma2, gamma=gamma, eta=eta, use_angular=use_angular)

    kernel = C(10, (1e1, 1e6)) * (C(1, (1, 1)) * RBF(10, (1,1000)) + C(0.01, (0.01, 0.01)) * RBF(10, (1,1000)) + WhiteKernel(1e-5, (1e-6,1e-2)))
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
35
    delta = deltaFunc(atoms=atoms, rcut=6)
36
37
38
39
40
41
42
43
44
    gpr = GPR(kernel=kernel,
          featureCalculator=featureCalculator,
          delta_function=delta,
          bias_func=None,
          optimize=False,
          n_restarts_optimizer=1)

    return gpr

Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
45
46
47
48
49
50
51
def get_E_with_std(traj, gpr):
    E = []
    F = []
    for a in traj:
        e = gpr.predict_energy(a, )

class test_gpr(unittest.TestCase):
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
52
53
54
55
56
57
58
59
60
61
62
63

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

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

    def setUp(self):
        print('setUp')
        #self.kernel = gauss_kernel()
64
65
66
        a = read('structures.traj', index='0')
        self.gpr_old = initialize_old_gpr(a)
        self.gpr = gpr()
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
67
68
69
70

    def tearDown(self):
        print('tearDown\n')
    
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    def test_compare_training_with_old(self):
        traj = read('structures.traj', index=':50')
        traj_train = traj[:40]
        traj_predict = traj[40:]

        self.gpr_old.train(traj_train, optimize=False)
        self.gpr.train(traj_train)

        np.testing.assert_almost_equal(self.gpr.alpha, self.gpr_old.alpha)

        E_old = np.array([self.gpr_old.predict_energy(a, return_error=True)[:2] for a in traj_predict])
        E = np.array([self.gpr.predict_energy(a, eval_std=True) for a in traj_predict])
        np.testing.assert_almost_equal(E, E_old)

        F_old = np.array([self.gpr_old.predict_force(a) for a in traj_predict])
        F = np.array([self.gpr.predict_forces(a) for a in traj_predict])
        np.testing.assert_almost_equal(F, F_old)
        
        Fstd_old = np.array([self.gpr_old.predict_force(a, return_error=True)[1] for a in traj_predict])
        Fstd = np.array([self.gpr.predict_forces(a, eval_std=True)[1] for a in traj_predict])
        np.testing.assert_almost_equal(Fstd, Fstd_old)


    def test_compare_lml_with_old(self):
95
96
97
98
        traj = read('structures.traj', index=':50')
        traj_train = traj[:40]
        traj_predict = traj[40:]

Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
99
100
101
102
103
104
        self.gpr_old.train(traj_train)
        self.gpr.train(traj_train)

        lml_old = self.gpr_old.log_marginal_likelihood_value_
        lml_new = -self.gpr.neg_log_marginal_likelihood(eval_gradient=False)
        np.testing.assert_almost_equal(lml_new, lml_old)
105

Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    def test_compare_lml_gradient_with_old(self):
        traj = read('structures.traj', index=':50')
        traj_train = traj[:40]
        traj_predict = traj[40:]

        self.gpr_old.train(traj_train)
        self.gpr.train(traj_train)

        _, lml_ddTheta_old = self.gpr_old.log_marginal_likelihood(self.gpr_old.kernel.theta, eval_gradient=True)
        _, lml_ddTheta = self.gpr.neg_log_marginal_likelihood(eval_gradient=True)
        np.testing.assert_almost_equal(lml_ddTheta, lml_ddTheta_old)

    def test_lml_gradient(self):
        traj = read('structures.traj', index=':50')
        traj_train = traj[:40]
        traj_predict = traj[40:]

        self.gpr_old.train(traj_train)
        self.gpr.train(traj_train)

        _, lml_ddTheta = self.gpr.neg_log_marginal_likelihood(eval_gradient=True)
        lml_ddTheta_numeric = self.gpr.numerical_neg_lml()
        np.testing.assert_almost_equal(lml_ddTheta, lml_ddTheta_numeric)
    
130
131
    def test_forces(self):
        pass
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
132
133
134

if __name__ == '__main__':
    unittest.main()