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

6
7
8
from ase.io import read

# old gpr
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
9
from kernels import RBF, ConstantKernel as C, WhiteKernel
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
10
from descriptor.fingerprint import Fingerprint
11
12
from delta_functions_multi.delta import delta as deltaFunc
from GPR import GPR
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from custom_calculators import krr_calculator

def relax(structure, calc, Fmax=0.05, steps=200, dmax_cov=None, label='test'):
    a = structure.copy()
    # Set calculator 
    a.set_calculator(calc)
    pos_init = a.get_positions()

    # Catch if linesearch fails
    try:
        dyn = BFGSLineSearch_zlim(a,
                                  logfile=label+'.log',
                                  trajectory=label+'.traj',
                                  pos_init=pos_init,
                                  dmax_cov=dmax_cov)
        dyn.run(fmax = Fmax, steps = steps)
    except Exception as err:
        print('Error in surrogate-relaxation:', err)
        traceback.print_exc()
        traceback.print_exc(file=sys.stderr)
    return a
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

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
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
54
    featureCalculator = Fingerprint(Rc1=Rc1, Rc2=Rc2, binwidth1=binwidth1, Nbins2=Nbins2, sigma1=sigma1, sigma2=sigma2, gamma=gamma, eta=eta, use_angular=use_angular)
55

56
57
    #kernel = C(10, (1e1, 1e6)) * RBF(10, (1,1000))
    kernel = C(100, (1e1, 1e5)) * (RBF(10, (1,1000)) + C(0.01, (0.01, 0.01)) * RBF(10, (1,1000)) + WhiteKernel(1e-5, (1e-5,1e-5)))
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
58
    delta = deltaFunc(atoms=atoms, rcut=6)
59
60
61
62
63
64
65
66
67
    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
68
69
70
71
72
73
74
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
75
76
77
78
79
80
81
82
83
84
85
86

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

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

    def setUp(self):
        print('setUp')
        #self.kernel = gauss_kernel()
87
88
        a = read('structures.traj', index='0')
        self.gpr_old = initialize_old_gpr(a)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
89
        #self.gpr = gpr()
90
        self.gpr = gpr(kernel='double')
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
91
92
93
94

    def tearDown(self):
        print('tearDown\n')
    
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    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)

Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
109
        F_old = np.array([self.gpr_old.predict_force(a).reshape((-1,3)) for a in traj_predict])
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
110
111
        F = np.array([self.gpr.predict_forces(a) for a in traj_predict])
        np.testing.assert_almost_equal(F, F_old)
112
113
114

        # The sign of the std are different in new and old GPR.
        Fstd_old = -np.array([self.gpr_old.predict_force(a, return_error=True)[1].reshape((-1,3)) for a in traj_predict])
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
115
116
117
118
119
        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):
120
121
122
123
        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
124
125
126
127
128
129
        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)
130

Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
131
132
133
134
135
136
137
138
139
140
    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)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
141
        np.testing.assert_almost_equal(-lml_ddTheta, lml_ddTheta_old)
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
142
143
144
145
146
147
148
149
150
151
152
153
154

    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)
    
155
    def test_forces(self):
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
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
        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)

        a = traj_predict[0]
        F = self.gpr.predict_forces(a)
        F_numeric = self.gpr.numerical_forces(a)
        np.testing.assert_almost_equal(F, F_numeric)

    def test_forces_std(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)

        a = traj_predict[0]
        _, Fstd = self.gpr.predict_forces(a, eval_std=True)
        _, Fstd_numeric = self.gpr.numerical_forces(a, eval_std=True)
        np.testing.assert_almost_equal(Fstd, Fstd_numeric)

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

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        #self.gpr_old.train(traj_train)
        #self.gpr.train(traj_train)

        print('theta1:', self.gpr.kernel.theta)
        print('theta2:', self.gpr_old.kernel.theta)
        
        self.gpr.optimize_hyperparameters(traj_train)
        self.gpr_old.train(traj_train, optimize=True)

        
        
        lml = self.gpr.lml
        lml_old = self.gpr_old.log_marginal_likelihood_value_

        theta = self.gpr.kernel.theta
        theta_old = self.gpr_old.kernel_.theta

        print('lml1:', lml)
        print('lml2:', lml_old)
        print('theta1:', theta)
        print('theta2:', theta_old)
        
        np.testing.assert_almost_equal(lml,lml_old)
        np.testing.assert_almost_equal(theta, theta_old)
        
    def test_gpr_relax(self):
        traj = read('structures.traj', index=':')
        traj_train = traj[:100]
        a = traj[100]

        theta0 = 300
        A1 = 1
        A2 = 0.01
        l1 = 1000
        l2 = 300
        noise = 1e-5
        
        #hyper = np.array([theta0,l1,l2,a2,noise])
        #hyper_old = np.array([theta0,a1,l1,a2,l2,noise])
        #self.gpr.kernel.theta = np.log(hyper)
        #self.gpr_old.kernel.theta = np.log(hyper_old)
        
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
228
        self.gpr.train(traj_train)
229
        self.gpr_old.train(traj_train, optimize=False)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
230

231
232
233
        kappa=2
        calc = self.gpr.get_calculator(kappa)
        calc_old = krr_calculator(self.gpr_old, kappa=kappa)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
234

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        a1 = a.copy()
        a2 = a.copy()
        a1.set_calculator(calc)
        a2.set_calculator(calc_old)

        a_old = a.copy()

        a_relaxed = relax(a, calc, label='test')
        a_old_relaxed = relax(a_old, calc_old, label='test_old')

        print('testing relax')
        
        np.testing.assert_almost_equal(a_relaxed.get_potential_energy(),
                                       a_old_relaxed.get_potential_energy())
        
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
250
if __name__ == '__main__':
251
    unittest.main()
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
252
253
254
255
256
257

    import matplotlib.pyplot as plt
    from ase import Atoms
    from ase.visualize import view
    from descriptor.fingerprint import Fingerprint
    from custom_calculators import doubleLJ_calculator
258
    from gpr import GPR
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
    
    def finite_diff(krr, a, dx=1e-5, eval_std=False):
        Natoms, dim = a.positions.shape
        F = np.zeros((Natoms, dim))
        Fstd = np.zeros((Natoms, dim))
        for ia in range(Natoms):
            for idim in range(dim):
                a_up = a.copy()
                a_down = a.copy()
                a_up.positions[ia,idim] += dx/2
                a_down.positions[ia,idim] -= dx/2


                if not eval_std:
                    E_up = krr.predict_energy(a_up, eval_std=False)
                    E_down = krr.predict_energy(a_down, eval_std=False)
                    F[ia,idim] = -(E_up - E_down)/dx
                else:
                    E_up, err_up = krr.predict_energy(a_up, eval_std=True)
                    E_down, err_down = krr.predict_energy(a_down, eval_std=True)
                    
                    F[ia,idim] = -(E_up - E_down)/dx
                    Fstd[ia,idim] = -(err_up - err_down)/dx
        if eval_std:
            return F[1,0], Fstd[1,0]
        else:
            return F
    
    def createData(r):
        positions = np.array([[0,0,0],[r,0,0]])
        a = Atoms('2H', positions, cell=[3,3,1], pbc=[0,0,0])
        calc = doubleLJ_calculator()
        a.set_calculator(calc)
        return a

    def test1():
        a_train = [createData(r) for r in [0.9,1,1.3,2,3]]

        view(a_train[3])
        
        E_train = np.array([a.get_potential_energy() for a in a_train])
        Natoms = a_train[0].get_number_of_atoms()
        
        Rc1 = 5
        binwidth1 = 0.2
        sigma1 = 0.2
        
        Rc2 = 4
        Nbins2 = 30
        sigma2 = 0.2
        
        gamma = 1
        eta = 30
        use_angular = False
        
        descriptor = Fingerprint(Rc1=Rc1, Rc2=Rc2, binwidth1=binwidth1, Nbins2=Nbins2, sigma1=sigma1, sigma2=sigma2, gamma=gamma, eta=eta, use_angular=use_angular)
        
        # Set up KRR-model
        gpr = GPR(kernel='single', descriptor=descriptor)
        
        gpr.train(atoms_list=a_train)
        
        Ntest = 500
        r_test = np.linspace(0.87, 3.5, Ntest)
        E_test = np.zeros(Ntest)
        err_test = np.zeros(Ntest)
        F_test = np.zeros(Ntest)
        Fstd_test = np.zeros(Ntest)
        
        E_true = np.zeros(Ntest)
        F_true = np.zeros(Ntest)
        F_num = np.zeros(Ntest)
        Fstd_num = np.zeros(Ntest)
        for i, r in enumerate(r_test):
            ai = createData(r)
            E, err = gpr.predict_energy(ai, eval_std=True)
            E_test[i] = E
            err_test[i] = err
            
            result_test = gpr.predict_forces(ai, eval_std=True)
            F_test[i] = result_test[0][1,0]
            Fstd_test[i] = result_test[1][1,0]
            F_num[i], Fstd_num[i] = finite_diff(gpr, ai, eval_std=True)
            
            E_true[i] = ai.get_potential_energy()
            F_true[i] = ai.get_forces()[1,0]
            
            
        plt.figure()
        plt.title('Energy')
        plt.xlabel('r')
        plt.ylabel('E')
        plt.plot(r_test, E_true, color='darkgreen', label='true')
        plt.plot(r_test, E_test, color='steelblue', label='model')
        plt.fill_between(r_test, E_test-err_test, E_test+err_test, color='steelblue', alpha=0.3, label='model')
        plt.legend()
        
        plt.figure()
        plt.title('Force')
        plt.xlabel('r')
        plt.ylabel('F')
        plt.plot(r_test, F_true, color='darkgreen', label='true')
        plt.plot(r_test, F_test, color='steelblue', label='model')
        plt.plot(r_test, F_num, 'k:', label='num')
        plt.plot(r_test, F_test-Fstd_test, color='crimson', label='model')
        plt.plot(r_test, F_num-Fstd_num, 'k:', label='num')
        plt.legend()

    test1()
    plt.show()