-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain_qm.py
228 lines (190 loc) · 10 KB
/
train_qm.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
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
import numpy as np
import tensorflow as tf
import matplotlib as mpl
# mpl.use('Agg')
import time
import os
import argparse
from datetime import timedelta
from tensorflow_addons import optimizers, metrics
from kgcnn.data.qm import QMGraphLabelScaler
import kgcnn.training.schedule
import kgcnn.training.scheduler
from kgcnn.training.history import save_history_score
from kgcnn.metrics.metrics import ScaledMeanAbsoluteError, ScaledRootMeanSquaredError
from sklearn.model_selection import KFold
from kgcnn.utils.plots import plot_train_test_loss, plot_predict_true
from kgcnn.model.serial import deserialize as deserialize_model
from kgcnn.data.serial import deserialize as deserialize_dataset
from kgcnn.training.hyper import HyperParameter
from kgcnn.utils.devices import set_devices_gpu
from kgcnn.data.utils import save_pickle_file, load_pickle_file
class GNNTrainConfig:
def __init__(self,
hyper="hyper/hyper_mp_e_form.py",
category= None,
model=None,
dataset=None,
make= None,
gpu=None,
fold=None,
seed=42):
self.hyper = hyper
self.category = category
self.model = model
self.dataset = dataset
self.make = make
self.gpu = gpu
self.seed = seed
self.fold = fold
def to_dict(self):
return vars(self)
# Usage:
config = GNNTrainConfig(hyper="training/hyper/hyper_qm9_energies.py", model='DenseGNN', make='make_qm9', dataset='QM9Dataset', seed=42)
print("Input of argparse:", config.to_dict())
args = config.to_dict()
# Set seed.
np.random.seed(args["seed"])
tf.random.set_seed(args["seed"])
tf.keras.utils.set_random_seed(args["seed"])
# Assigning GPU.
set_devices_gpu(args["gpu"])
# HyperParameter is used to store and verify hyperparameter.
hyper = HyperParameter(
hyper_info=args["hyper"], hyper_category=args["category"],
model_name=args["model"], model_class=args["make"], dataset_class=args["dataset"])
hyper.verify()
# dataset = deserialize_dataset(hyper["dataset"])
# filepath = hyper.results_file_path()
# save_pickle_file(dataset, os.path.join(filepath, "DENSE_data.pickle"))
datapath = r"/home/deep/gcnn_keras-master/results/QM9Dataset/DenseGNN_model_default_EGNN/DENSE_data.pickle"
dataset = load_pickle_file(datapath)
# Check if dataset has the required properties for model input. This includes a quick shape comparison.
# The name of the keras `Input` layer of the model is directly connected to property of the dataset.
# Example 'edge_indices' or 'node_attributes'. This couples the keras model to the dataset.
dataset.assert_valid_model_input(hyper["model"]["config"]["inputs"])
# Filter the dataset for invalid graphs. At the moment invalid graphs are graphs which do not have the property set,
# which is required by the model's input layers, or if a tensor-like property has zero length.
dataset.clean(hyper["model"]["config"]["inputs"])
data_length = len(dataset) # Length of the cleaned dataset.
# For `QMDataset`, always train on graph, labels. The `QMDataset` has property 'label_names' and 'label_units', since
# targets can be in eV, Hartree, Bohr, GHz etc. Must be defined by subclasses of the dataset.
labels = np.array(dataset.obtain_property("graph_labels"))
label_names = dataset.label_names
label_units = dataset.label_units
if len(labels.shape) <= 1:
labels = np.expand_dims(labels, axis=-1)
# Training on multiple targets for regression. This can is often required to train on orbital energies of ground
# or excited state or energies and enthalpies etc.
multi_target_indices = hyper["training"]["multi_target_indices"] if "multi_target_indices" in hyper[
"training"] else None
if multi_target_indices is not None:
labels = labels[:, multi_target_indices]
if label_names is not None:
label_names = [label_names[i] for i in multi_target_indices]
if label_units is not None:
label_units = [label_units[i] for i in multi_target_indices]
print("Labels %s in %s have shape %s" % (label_names, label_units, labels.shape))
# For QMDataset, also the atomic number is required to properly pre-scale extensive quantities like total energy.
atoms = dataset.obtain_property("node_number")
# Cross-validation via random KFold split form `sklearn.model_selection`.
# Or from dataset information.
if hyper["training"]["cross_validation"] is None:
print("Using dataset splits.")
train_test_indices = dataset.get_split_indices()
else:
kf = KFold(**hyper["training"]["cross_validation"]["config"])
train_test_indices = [
[train_index, test_index] for train_index, test_index in kf.split(X=np.zeros((data_length, 1)), y=labels)]
# Training on splits. Since training on QM datasets can be expensive, there is a 'execute_splits' parameter to not
# train on all splits for testing. Can be set via command line or hyperparameter.
execute_folds = args["fold"]
if "execute_folds" in hyper["training"]:
execute_folds = hyper["training"]["execute_folds"]
splits_done = 0
history_list = []
model, hist, x_test, y_test, scaler, atoms_test = None, None, None, None, None, None
train_indices_all, test_indices_all = [], []
for i, (train_index, test_index) in enumerate(train_test_indices):
test_indices_all.append(test_index)
train_indices_all.append(train_index)
# Only do execute_splits out of the k-folds of cross-validation.
if execute_folds:
if i not in execute_folds:
continue
print("Running training on fold: %s" % i)
# Make the model for current split using model kwargs from hyperparameter.
# They are always updated on top of the models default kwargs.
model = deserialize_model(hyper["model"])
# First select training and test graphs from indices, then convert them into tensorflow tensor
# representation. Which property of the dataset and whether the tensor will be ragged is retrieved from the
# kwargs of the keras `Input` layers ('name' and 'ragged').
x_train, y_train = dataset[train_index].tensor(hyper["model"]["config"]["inputs"]), labels[train_index]
x_test, y_test = dataset[test_index].tensor(hyper["model"]["config"]["inputs"]), labels[test_index]
# Also keep the same information for atomic numbers of the molecules.
atoms_test = [atoms[i] for i in test_index]
atoms_train = [atoms[i] for i in train_index]
# Normalize training and test targets. For QM datasets this training script uses the `QMGraphLabelScaler` class.
# Note that the QMGraphLabelScaler must receive a (serialized) list of individual scaler, one per each target.
# These are extensive or intensive scaler, but could be expanded to have other normalization methods.
if "scaler" in hyper["training"]:
print("Using QMGraphLabelScaler.")
# Atomic number argument here!
scaler = QMGraphLabelScaler(**hyper["training"]["scaler"]["config"]).fit(y=y_train, atomic_number=atoms_train)
y_train = scaler.transform(y=y_train, atomic_number=atoms_train)
y_test = scaler.transform(y=y_test, atomic_number=atoms_test)
# If scaler was used we add rescaled standard metrics to compile.
scaler_scale = scaler.get_scaling()
mae_metric = ScaledMeanAbsoluteError(scaler_scale.shape, name="scaled_mean_absolute_error")
rms_metric = ScaledRootMeanSquaredError(scaler_scale.shape, name="scaled_root_mean_squared_error")
if scaler.scale_ is not None:
mae_metric.set_scale(scaler_scale)
rms_metric.set_scale(scaler_scale)
metrics = [mae_metric, rms_metric]
else:
print("Not using QMGraphLabelScaler.")
metrics = None
# Compile model with optimizer and loss
model.compile(**hyper.compile(loss="mean_absolute_error", metrics=metrics))
print(model.summary())
# Start and time training
start = time.time()
hist = model.fit(x_train, y_train,
validation_data=(x_test, y_test),
**hyper.fit())
stop = time.time()
print("Print Time for training: ", str(timedelta(seconds=stop - start)))
# Get loss from history
history_list.append(hist)
splits_done = splits_done + 1
# Make output directory
filepath = hyper.results_file_path()
postfix_file = hyper["info"]["postfix_file"]
# Plot prediction
predicted_y = model.predict(x_test, verbose=0)
true_y = y_test
if scaler:
predicted_y = scaler.inverse_transform(y=predicted_y, atomic_number=atoms_test)
true_y = scaler.inverse_transform(y=true_y, atomic_number=atoms_test)
plot_predict_true(predicted_y, true_y,
filepath=filepath, data_unit=label_units,
model_name=hyper.model_name, dataset_name=hyper.dataset_class, target_names=label_names,
file_name=f"predict{postfix_file}.png")
# Plot training- and test-loss vs epochs for all splits.
data_unit = hyper["data"]["data_unit"] if "data_unit" in hyper["data"] else ""
plot_train_test_loss(history_list, loss_name=None, val_loss_name=None,
model_name=hyper.model_name, data_unit=data_unit, dataset_name=hyper.dataset_class,
filepath=filepath, file_name=f"loss{postfix_file}.png")
# Save keras-model to output-folder.
model.save(os.path.join(filepath, f"model{postfix_file}"))
# Save original data indices of the splits.
np.savez(os.path.join(filepath, f"{hyper.model_name}_test_indices_{postfix_file}.npz"), *test_indices_all)
np.savez(os.path.join(filepath, f"{hyper.model_name}_train_indices_{postfix_file}.npz"), *train_indices_all)
# Save hyperparameter again, which were used for this fit.
hyper.save(os.path.join(filepath, f"{hyper.model_name}_hyper{postfix_file}.json"))
# Save score of fit result for as text file.
save_history_score(history_list, loss_name=None, val_loss_name=None,
model_name=hyper.model_name, data_unit=data_unit, dataset_name=hyper.dataset_class,
model_class=hyper.model_class, multi_target_indices=multi_target_indices,
execute_folds=execute_folds, seed=args["seed"],
filepath=filepath, file_name=f"score{postfix_file}.yaml")