gpr.py 2.55 KB
Newer Older
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
1
2
3
import numpy as np
from kernel import gauss_kernel, double_gauss_kernel

4
5
6
7
8
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
35
36
37
38
39
40
41
42
43
44
45
46
from descriptor.fingerprint import Fingerprint
from prior.prior import repulsive_prior


class gpr_memory():
    def __init__(self, descriptor, prior):
        self.energies = None
        self.features = None
        self.prior_values = None

        self.descriptor = descriptor
        self.prior = prior

    def save_data(self, atoms_list):
        energies_save = np.array([a.get_potential_energy() for a in atoms_list])
        self.save_energies(energies_save)

        features_save = descriptor.get_featureMat(atoms_list)
        self.save_features(features_save)

        if self.prior is not None:
            prior_values_save = np.array([self.prior.energy(a) for a in atoms_list])
            self.save_prior_values(prior_values_save)

    def save_energies(self, energies_save):
        if self.energies in None:
            self.energies = energies_save
        else:
            self.energies = np.r_[self.energies, energies_save]
    
    def save_features(self, features_save):
        if self.features in None:
            self.features = features_save
     	else:
            self.features = np.r_[self.features, features_save]

    def save_prior_values(self, prior_values_save):
        if self.prior_values in None:
            self.prior_values = prior_values_save
     	else:
            self.prior_values = np.r_[self.prior_values, prior_values_save]


Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
47
48
49
50
51
52
53
54
55
56
57
class gpr():
    """Gaussian Process Regression
    
    Parameters:
    
    descriptor:
    
    kernel:
    
    prior:
    """
58
59
60
61
62
63
64
65
66
67
    def __init__(self, descriptor=None, kernel='single', prior=None):
        if descriptor is None:
            self.descriptor = Fingerprint()
        else:
            self.descriptor = descriptor

        if kernel is 'single':
            self.kernel = gauss_kernel()
        elif kernel is 'double':
            self.kernel = double_gauss_kernel()
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
68
69
        else:
            self.kernel = kernel
70
71
72
73
74
75
76

        if prior is None:
            self.prior = repulsive_prior()
        else:
            self.prior = prior

        self.memory = gpr_memory(self.descriptor, self.prior)
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
77
78
79
80
81
82

    def predict(self, a):
        x = self.descriptor.get_feature(a)
        k = self.kernel.kernel_vector(x, self.X)

        f = k.T.dot(self.alpha) + self.bias + delta
83
84
85
86

    def set_bias(self):
        self.bias = np.mean(self.memory.energies)
        
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
87
    def train(self):
88
        
Malthe Kjær Bisbo's avatar
Malthe Kjær Bisbo committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

    def optimize_hyperparameters(self):
        pass

    def neg_log_likelihood(self):
        pass

    

def func(self, x):
    dsafas

def name():
    """