Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

Commit 64beacd

Browse files
committed
Added kernel wrappers
1 parent cb41ca9 commit 64beacd

File tree

5 files changed

+146
-81
lines changed

5 files changed

+146
-81
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ OBJECTS = \
55
qml/kernels/farad_kernels.so \
66
qml/kernels/fkernels.so
77

8-
# Flags for GCC compilers and system BLAS/LAPACK
9-
COMPILER_FLAGS = --opt='-O3 -fopenmp -m64 -march=native' --f90flags=''
8+
# Flags for GCC compilers and MKL
9+
COMPILER_FLAGS = --opt='-O3 -fopenmp -m64 -march=native' --f90flags='-I${MKLROOT}/include'
1010
LINKER_FLAGS = -lgomp -lpthread -lm -ldl
11-
MATH_LINKER_FLAGS = -lblas -llapack
11+
MATH_LINKER_FLAGS = -L${MKLROOT}/lib/intel64 -lmkl_rt
1212

1313
# F2PY executable
1414
F2PY_EXEC = f2py

examples/energy_krr_cmat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import numpy as np
2929
import qml
3030
from qml.kernels import laplacian_kernel
31-
from qml.kernels import gaussian_kernel
3231
from qml.math import cho_solve
3332

3433

@@ -98,16 +97,17 @@ def get_energies(filename):
9897
llambda = 10**(-10.0)
9998

10099
# Generate training Kernel
100+
print "Calculating training kernel ..."
101101
K = laplacian_kernel(X, X, sigma)
102-
# K = gaussian_kernel(X, X, sigma)
103102

104103
# Solve alpha
104+
print "Solving alphas ..."
105105
K[np.diag_indices_from(K)] += llambda
106106
alpha = cho_solve(K,Y)
107107

108108
# Calculate prediction kernel
109+
print "Calculating prediction kernel ..."
109110
Ks = laplacian_kernel(X, Xs, sigma)
110-
# Ks = gaussian_kernel(X, Xs, sigma)
111111
Yss = np.dot(Ks.transpose(), alpha)
112112

113113
# Print final RMSD

qml/kernels/arad_kernels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import numpy as np
2424

25-
from farad_kernels import fget_kernels_arad
26-
from farad_kernels import fget_symmetric_kernels_arad
25+
from .farad_kernels import fget_kernels_arad
26+
from .farad_kernels import fget_symmetric_kernels_arad
2727

2828
PTP = {\
2929
1 :[1,1] ,2: [1,8]#Row1

qml/kernels/kernels.py

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import numpy as np
2424
from numpy import empty, asfortranarray, ascontiguousarray, zeros
2525

26-
from fkernels import fgaussian_kernel
27-
from fkernels import flaplacian_kernel
28-
from fkernels import fget_vector_kernels_gaussian
29-
from fkernels import fget_vector_kernels_laplacian
26+
from .fkernels import fgaussian_kernel
27+
from .fkernels import flaplacian_kernel
28+
from .fkernels import fget_vector_kernels_gaussian
29+
from .fkernels import fget_vector_kernels_laplacian
3030

3131

3232
def laplacian_kernel(A, B, sigma):
@@ -93,72 +93,3 @@ def gaussian_kernel(A, B, sigma):
9393
fgaussian_kernel(A.T, na, B.T, nb, K, sigma)
9494

9595
return K
96-
97-
98-
def get_atomic_kernels_laplacian(mols1, mols2, sigmas):
99-
100-
n1 = np.array([mol.natoms for mol in mols1], dtype=np.int32)
101-
n2 = np.array([mol.natoms for mol in mols2], dtype=np.int32)
102-
103-
amax1 = np.amax(n1)
104-
amax2 = np.amax(n2)
105-
106-
nm1 = len(mols1)
107-
nm2 = len(mols2)
108-
109-
cmat_size = mols1[0].local_coulomb_matrix.shape[1]
110-
111-
x1 = np.zeros((nm1,amax1,cmat_size), dtype=np.float64, order="F")
112-
x2 = np.zeros((nm2,amax2,cmat_size), dtype=np.float64, order="F")
113-
114-
for imol in range(nm1):
115-
x1[imol,:n1[imol],:cmat_size] = mols1[imol].local_coulomb_matrix
116-
117-
for imol in range(nm2):
118-
x2[imol,:n2[imol],:cmat_size] = mols2[imol].local_coulomb_matrix
119-
120-
# Reorder for Fortran speed
121-
x1 = np.swapaxes(x1,0,2)
122-
x2 = np.swapaxes(x2,0,2)
123-
124-
nsigmas = len(sigmas)
125-
126-
sigmas = np.array(sigmas, dtype=np.float64)
127-
128-
return fget_vector_kernels_laplacian(x1, x2, n1, n2, sigmas, \
129-
nm1, nm2, nsigmas)
130-
131-
132-
def get_atomic_kernels_gaussian(mols1, mols2, sigmas):
133-
134-
n1 = np.array([mol.natoms for mol in mols1], dtype=np.int32)
135-
n2 = np.array([mol.natoms for mol in mols2], dtype=np.int32)
136-
137-
amax1 = np.amax(n1)
138-
amax2 = np.amax(n2)
139-
140-
nm1 = len(mols1)
141-
nm2 = len(mols2)
142-
143-
cmat_size = mols1[0].local_coulomb_matrix.shape[1]
144-
145-
x1 = np.zeros((nm1,amax1,cmat_size), dtype=np.float64, order="F")
146-
x2 = np.zeros((nm2,amax2,cmat_size), dtype=np.float64, order="F")
147-
148-
for imol in range(nm1):
149-
x1[imol,:n1[imol],:cmat_size] = mols1[imol].local_coulomb_matrix
150-
151-
for imol in range(nm2):
152-
x2[imol,:n2[imol],:cmat_size] = mols2[imol].local_coulomb_matrix
153-
154-
# Reorder for Fortran speed
155-
x1 = np.swapaxes(x1,0,2)
156-
x2 = np.swapaxes(x2,0,2)
157-
158-
nsigmas = len(sigmas)
159-
160-
sigmas = np.array(sigmas, dtype=np.float64)
161-
162-
return fget_vector_kernels_gaussian(x1, x2, n1, n2, sigmas, \
163-
nm1, nm2, nsigmas)
164-

qml/kernels/wrappers.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2016 Anders Steen Christensen, Felix Faber
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
import numpy as np
24+
from numpy import empty, asfortranarray, ascontiguousarray, zeros
25+
26+
from fkernels import fget_vector_kernels_gaussian
27+
from fkernels import fget_vector_kernels_laplacian
28+
29+
from .arad_kernels import get_atomic_kernels_arad
30+
31+
32+
def get_atomic_kernels_laplacian(mols1, mols2, sigmas):
33+
34+
n1 = np.array([mol.natoms for mol in mols1], dtype=np.int32)
35+
n2 = np.array([mol.natoms for mol in mols2], dtype=np.int32)
36+
37+
amax1 = np.amax(n1)
38+
amax2 = np.amax(n2)
39+
40+
nm1 = len(mols1)
41+
nm2 = len(mols2)
42+
43+
cmat_size = mols1[0].local_coulomb_matrix.shape[1]
44+
45+
x1 = np.zeros((nm1,amax1,cmat_size), dtype=np.float64, order="F")
46+
x2 = np.zeros((nm2,amax2,cmat_size), dtype=np.float64, order="F")
47+
48+
for imol in range(nm1):
49+
x1[imol,:n1[imol],:cmat_size] = mols1[imol].local_coulomb_matrix
50+
51+
for imol in range(nm2):
52+
x2[imol,:n2[imol],:cmat_size] = mols2[imol].local_coulomb_matrix
53+
54+
# Reorder for Fortran speed
55+
x1 = np.swapaxes(x1,0,2)
56+
x2 = np.swapaxes(x2,0,2)
57+
58+
nsigmas = len(sigmas)
59+
60+
sigmas = np.array(sigmas, dtype=np.float64)
61+
62+
return fget_vector_kernels_laplacian(x1, x2, n1, n2, sigmas, \
63+
nm1, nm2, nsigmas)
64+
65+
66+
def get_atomic_kernels_gaussian(mols1, mols2, sigmas):
67+
68+
n1 = np.array([mol.natoms for mol in mols1], dtype=np.int32)
69+
n2 = np.array([mol.natoms for mol in mols2], dtype=np.int32)
70+
71+
amax1 = np.amax(n1)
72+
amax2 = np.amax(n2)
73+
74+
nm1 = len(mols1)
75+
nm2 = len(mols2)
76+
77+
cmat_size = mols1[0].local_coulomb_matrix.shape[1]
78+
79+
x1 = np.zeros((nm1,amax1,cmat_size), dtype=np.float64, order="F")
80+
x2 = np.zeros((nm2,amax2,cmat_size), dtype=np.float64, order="F")
81+
82+
for imol in range(nm1):
83+
x1[imol,:n1[imol],:cmat_size] = mols1[imol].local_coulomb_matrix
84+
85+
for imol in range(nm2):
86+
x2[imol,:n2[imol],:cmat_size] = mols2[imol].local_coulomb_matrix
87+
88+
# Reorder for Fortran speed
89+
x1 = np.swapaxes(x1,0,2)
90+
x2 = np.swapaxes(x2,0,2)
91+
92+
nsigmas = len(sigmas)
93+
94+
sigmas = np.array(sigmas, dtype=np.float64)
95+
96+
return fget_vector_kernels_gaussian(x1, x2, n1, n2, sigmas, \
97+
nm1, nm2, nsigmas)
98+
99+
100+
def arad_kernels(mols1, mols2, sigmas,
101+
width=0.2, cut_distance=5.0, r_width=1.0, c_width=0.5):
102+
103+
amax = mol1[0].arad_descriptor.shape[1]
104+
105+
nm1 = len(mols1)
106+
nm2 = len(mols2)
107+
108+
X1 = np.array([mol.arad_descriptor for mol in mols1]).reshape((nm1,amax,5,amax))
109+
X2 = np.array([mol.arad_descriptor for mol in mols2]).reshape((nm2,amax,5,amax))
110+
111+
Z1 = [mol.nuclear_charges for mol in mols1]
112+
Z2 = [mol.nuclear_charges for mol in mols2]
113+
114+
K = get_atomic_kernels_arad(X1, X2, Z1, Z2, sigmas, \
115+
width=width, cut_distance=cut_distance, r_width=r_width, c_width=c_width)
116+
117+
return K
118+
119+
120+
def arad_symmetric_kernels(mols1, sigmas,
121+
width=0.2, cut_distance=5.0, r_width=1.0, c_width=0.5):
122+
123+
amax = mol1[0].arad_descriptor.shape[1]
124+
125+
nm1 = len(mols1)
126+
127+
X1 = np.array([mol.arad_descriptor for mol in mols1]).reshape((nm1,amax,5,amax))
128+
129+
Z1 = [mol.nuclear_charges for mol in mols1]
130+
131+
K = get_symmetric_atomic_kernels_arad(X1, Z1, sigmas, \
132+
width=width, cut_distance=cut_distance, r_width=r_width, c_width=c_width)
133+
134+
return K

0 commit comments

Comments
 (0)