-
Notifications
You must be signed in to change notification settings - Fork 7
/
exercise_2_3.py
executable file
·52 lines (36 loc) · 1.35 KB
/
exercise_2_3.py
1
2
3
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
47
48
49
50
51
#!/usr/bin/env python
from __future__ import print_function
import numpy as np
import qml
from qml.kernels import gaussian_kernel
from qml.kernels import laplacian_kernel
from qml.math import cho_solve
from tutorial_data import compounds
from tutorial_data import energy_pbe0
if __name__ == "__main__":
# Import QM7, already parsed to QML
from tutorial_data import compounds
from qml.kernels import gaussian_kernel
# For every compound generate a coulomb matrix or BoB
for mol in compounds:
mol.generate_coulomb_matrix(size=23, sorting="row-norm")
# mol.generate_bob(size=23, asize={"O":3, "C":7, "N":3, "H":16, "S":1})
# Make a big 2D array with all the representations
X = np.array([mol.representation for mol in compounds])
# X = np.array([mol.bob for mol in compounds])
# Print all representations
print("Representations:")
print(X)
# Assign 1000 first molecules to the training set
X_training = X[:1000]
Y_training = energy_pbe0[:1000]
sigma = 4000.0
K = gaussian_kernel(X_training, X_training, sigma)
print("Gaussian kernel:")
print(K)
# Add a small lambda to the diagonal of the kernel matrix
K[np.diag_indices_from(K)] += 1e-8
# Use the built-in Cholesky-decomposition to solve
alpha = cho_solve(K, Y_training)
print("Alphas:")
print(alpha)