This repository was archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathqmlearn.py
More file actions
343 lines (264 loc) · 11.7 KB
/
qmlearn.py
File metadata and controls
343 lines (264 loc) · 11.7 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
import glob
import numpy as np
from qml import qmlearn
import sklearn.pipeline
import sklearn.model_selection
def data():
"""
Using the Data object.
"""
print("*** Begin data examples ***")
# The Data object has the same role as the Compound class.
# Where the Compound class is for one compound, the Data class
# Is for multiple
# One can load in a set of xyz files
filenames = sorted(glob.glob("../test/qm7/00*.xyz"))
data = qmlearn.Data(filenames)
print("length of filenames", len(filenames))
print("length of nuclear_charges", len(data.nuclear_charges))
print("length of coordinates", len(data.coordinates))
# Or just load a glob string
data = qmlearn.Data("../test/qm7/00*.xyz")
print("length of nuclear_charges", len(data.nuclear_charges))
# Energies (or other molecular properties) can be stored in the object
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)[:98]
data.set_energies(energies)
print("length of energies", len(data.energies))
print("*** End data examples ***")
print()
def preprocessing():
"""
Rescaling energies
"""
print("*** Begin preprocessing examples ***")
# The AtomScaler object does a linear fit of the number of each element to the energy.
data = qmlearn.Data("../test/qm7/*.xyz")
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)
# Input can be nuclear_charges and energies
print("Energies before rescaling", energies[:3])
rescaled_energies = qmlearn.preprocessing.AtomScaler().fit_transform(data.nuclear_charges, energies)
print("Energies after rescaling", rescaled_energies[:3])
# Or a data object can be used
data.set_energies(energies)
data2 = qmlearn.preprocessing.AtomScaler().fit_transform(data)
print("Energies after rescaling", data2.energies[:3])
print("*** End preprocessing examples ***")
print()
def representations():
"""
Creating representations. Currently implemented representations are
CoulombMatrix, AtomicCoulombMatrix, AtomicSLATM, GlobalSLATM,
FCHLRepresentations, AtomCenteredSymmetryFunctions.
(BagOfBonds is still missing)
"""
print("*** Begin representations examples ***")
data = qmlearn.Data("../test/qm7/*.xyz")
# Representations can be created from a data object
model = qmlearn.representations.CoulombMatrix(sorting ='row-norm')
representations = model.generate(data)
print("Shape of representations:", representations.shape)
# Alternatively the data object can be passed at initialization of the representation class
# and only select molecule indices can be parsed
model = qmlearn.representations.CoulombMatrix(data)
representations = model.generate([0,5,7,16])
print("Shape of representations:", representations.shape)
print("*** End representations examples ***")
print()
def kernels():
"""
Create kernels. Currently implemented kernels are GaussianKernel,
LaplacianKernel, FCHLKernel.
"""
print("*** Begin kernels examples ***")
data = qmlearn.Data("../test/qm7/*.xyz")
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)
data.set_energies(energies)
# Kernels can be created from representations
model = qmlearn.representations.CoulombMatrix(data)
indices = np.arange(100)
representations = model.generate(indices)
model = qmlearn.kernels.GaussianKernel(sigma='auto')
symmetric_kernels = model.generate(representations[:80])
print("Shape of symmetric kernels:", symmetric_kernels.shape)
asymmetric_kernels = model.generate(representations[:80], representations[80:])
print("Shape of asymmetric kernels:", asymmetric_kernels.shape)
# Atomic representations can be used as well
model = qmlearn.representations.AtomicCoulombMatrix(data)
indices = np.arange(100)
representations = model.generate(indices)
model = qmlearn.kernels.GaussianKernel(sigma='auto')
symmetric_kernels = model.generate(representations[:80], representation_type = 'atomic')
print("Shape of symmetric kernels:", symmetric_kernels.shape)
asymmetric_kernels = model.generate(representations[:80], representations[80:], representation_type = 'atomic')
print("Shape of asymmetric kernels:", asymmetric_kernels.shape)
print("*** End kernels examples ***")
print()
def models():
"""
Regression models. Only KernelRidgeRegression implemented so far.
"""
print("*** Begin models examples ***")
filenames = sorted(glob.glob("../test/qm7/*.xyz"))
data = qmlearn.Data(filenames)
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)
model = qmlearn.representations.CoulombMatrix(data)
# Create 1000 random indices
indices = np.random.choice(np.arange(len(energies)), size=1000, replace=False)
representations = model.generate(indices)
model = qmlearn.kernels.GaussianKernel(sigma='auto')
symmetric_kernels = model.generate(representations[:800])
asymmetric_kernels = model.generate(representations[:800], representations[800:])
# Model can be fit giving kernel matrix and energies
model = qmlearn.models.KernelRidgeRegression()
model.fit(symmetric_kernels, energies[indices[:800]])
print("Fitted KRR weights:", model.alpha[:3])
# Predictions can be had from an asymmetric kernel
predictions = model.predict(asymmetric_kernels)
print("Predicted energies:", predictions[:3])
print("True energies:", energies[indices[:3]])
# Or the score (default negative mae) can be had directly
scores = model.score(asymmetric_kernels, energies[indices[800:]])
print("Negative MAE:", scores)
print("*** End models examples ***")
print()
def pipelines():
"""
Constructing scikit-learn pipelines
"""
print("*** Begin pipelines examples ***")
# It is much easier to do all this with a scikit-learn pipeline
# Create data
data = qmlearn.Data("../test/qm7/*.xyz")
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)
data.set_energies(energies)
# Create model
model = sklearn.pipeline.make_pipeline(
qmlearn.preprocessing.AtomScaler(data),
qmlearn.representations.CoulombMatrix(),
qmlearn.kernels.GaussianKernel(),
qmlearn.models.KernelRidgeRegression(),
)
# Create 1000 random indices
indices = np.random.choice(np.arange(len(energies)), size=1000, replace=False)
model.fit(indices[:800])
scores = model.score(indices[800:])
print("Negative MAE:", scores)
# Passing alchemy=False to kernels makes sure that the atomic kernel only compares C to C, H to H etc.
# This will speed up kernels of some representations dramatically, but only works in pipelines
# Create model
model = sklearn.pipeline.make_pipeline(
qmlearn.preprocessing.AtomScaler(data),
qmlearn.representations.CoulombMatrix(),
qmlearn.kernels.GaussianKernel(alchemy=False),
qmlearn.models.KernelRidgeRegression(),
)
# Create 1000 random indices
indices = np.random.choice(np.arange(len(energies)), size=1000, replace=False)
model.fit(indices[:800])
scores = model.score(indices[800:])
print("Negative MAE without alchemy:", scores)
print("*** End pipelines examples ***")
print()
def pipelines_2():
"""
Scikit learn pipeline with a molecular neural network
"""
print("\n *** Begin pipelines example with molecular Neural Network ***")
data = qmlearn.Data("../test/qm7/*.xyz")
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)
data.set_energies(energies)
# Create model
model = sklearn.pipeline.make_pipeline(
qmlearn.preprocessing.AtomScaler(data),
qmlearn.representations.CoulombMatrix(),
qmlearn.models.NeuralNetwork(iterations=500, batch_size=50, learning_rate=0.005),
)
indices = np.arange(1000)
np.random.shuffle(indices)
model.fit(indices[:100])
# Score on the TRAINING set, since you won't get good predictions in 500 iterations
scores = model.score(indices[:100])
print("Negative MAE:", scores)
print("*** End pipelines example with molecular Neural Network *** \n")
def pipelines_3():
"""
Scikit learn pipeline with an atomic neural network
"""
print("\n *** Begin pipelines example with atomic Neural Network ***")
data = qmlearn.Data("../test/qm7/*.xyz")
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)
data.set_energies(energies)
# Create model
model = sklearn.pipeline.make_pipeline(
qmlearn.preprocessing.AtomScaler(data),
qmlearn.representations.AtomCenteredSymmetryFunctions(),
qmlearn.models.NeuralNetwork(iterations=500, batch_size=50, learning_rate=0.005),
)
indices = np.arange(1000)
np.random.shuffle(indices)
model.fit(indices[:100])
# Score on the TRAINING set, since you won't get good predictions in 500 iterations
scores = model.score(indices[:100])
print("Negative MAE:", scores)
print("*** End pipelines example with atomic Neural Network *** \n")
def cross_validation():
"""
Doing cross validation with qmlearn
"""
print("*** Begin CV examples ***")
# Create data
data = qmlearn.Data("../test/qm7/*.xyz")
energies = np.loadtxt("../test/data/hof_qm7.txt", usecols=1)
data.set_energies(energies)
# Create model
model = sklearn.pipeline.make_pipeline(
qmlearn.preprocessing.AtomScaler(data),
qmlearn.representations.CoulombMatrix(),
qmlearn.kernels.GaussianKernel(),
qmlearn.models.KernelRidgeRegression(),
# memory='/dev/shm/' ### This will cache the previous steps to the virtual memory and might speed up gridsearch
)
# Create 1000 random indices
indices = np.random.choice(np.arange(len(energies)), size=1000, replace=False)
# 3-fold CV of a given model can easily be done
scores = sklearn.model_selection.cross_validate(model, indices, cv=3)
print("Cross-validated scores:", scores['test_score'])
# Doing a grid search over hyper parameters
params = {'gaussiankernel__sigma': [10, 30, 100],
'kernelridgeregression__l2_reg': [1e-8, 1e-4],
}
grid = sklearn.model_selection.GridSearchCV(model, cv=3, refit=False, param_grid=params)
grid.fit(indices)
print("Best hyper parameters:", grid.best_params_)
print("Best score:", grid.best_score_)
# As an alternative the pipeline can be constructed slightly different, which allows more complex CV
# Create model
model = sklearn.pipeline.Pipeline([
('preprocess', qmlearn.preprocessing.AtomScaler(data)),
('representations', qmlearn.representations.CoulombMatrix()),
('kernel', qmlearn.kernels.GaussianKernel()),
('model', qmlearn.models.KernelRidgeRegression())
],
# memory='/dev/shm/' ### This will cache the previous steps to the virtual memory and might speed up gridsearch
)
# Doing a grid search over hyper parameters
# including which kernel to use
params = {'kernel': [qmlearn.kernels.LaplacianKernel(), qmlearn.kernels.GaussianKernel()],
'kernel__sigma': [10, 30, 100, 1000, 3000, 1000],
'model__l2_reg': [1e-8, 1e-4],
}
grid = sklearn.model_selection.GridSearchCV(model, cv=3, refit=False, param_grid=params)
grid.fit(indices)
print("Best hyper parameters:", grid.best_params_)
print("Best score:", grid.best_score_)
print("*** End CV examples ***")
if __name__ == '__main__':
data()
preprocessing()
representations()
kernels()
models()
pipelines()
cross_validation()
pipelines_2()
pipelines_3()