nnp_training.py 9.1 KB
Newer Older
Gao, Xiang's avatar
Gao, Xiang committed
1
2
# -*- coding: utf-8 -*-
"""
Gao, Xiang's avatar
Gao, Xiang committed
3
4
.. _training-example:

Gao, Xiang's avatar
Gao, Xiang committed
5
6
7
8
9
10
11
12
Train Your Own Neural Network Potential
=======================================

This example shows how to use TorchANI train your own neural network potential.
"""

###############################################################################
# To begin with, let's first import the modules we will use:
13
14
15
import torch
import ignite
import torchani
16
17
18
import tqdm
import timeit
import tensorboardX
Gao, Xiang's avatar
Gao, Xiang committed
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
import os
import sys


###############################################################################
# Now let's setup training hyperparameters. Note that here for our demo purpose
# , we set both training set and validation set the ``ani_gdb_s01.h5`` in
# TorchANI's repository. This allows this program to finish very quick, because
# that dataset is very small. But this is wrong and should be avoided for any
# serious training. These paths assumes the user run this script under the
# ``examples`` directory of TorchANI's repository. If you download this script,
# you should manually set the path of these files in your system before this
# script can run successfully.

# training and validation set
try:
    path = os.path.dirname(os.path.realpath(__file__))
except NameError:
    path = os.getcwd()
training_path = os.path.join(path, '../dataset/ani_gdb_s01.h5')
validation_path = os.path.join(path, '../dataset/ani_gdb_s01.h5')

# checkpoint file to save model when validation RMSE improves
model_checkpoint = 'model.pt'

# max epochs to run the training
max_epochs = 20

# Compute training RMSE every this steps. Since the training set is usually
# huge and the loss funcition does not directly gives us RMSE, we need to
# check the training RMSE to see overfitting.
training_rmse_every = 5

# device to run the training
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# batch size
batch_size = 1024

# log directory for tensorboardX
log = 'runs'


###############################################################################
# Now let's read our constants and self energies from constant files and
# construct AEV computer.
const_file = os.path.join(path, '../torchani/resources/ani-1x_dft_x8ens/rHCNO-5.2R_16-3.5A_a4-8.params')  # noqa: E501
sae_file = os.path.join(path, '../torchani/resources/ani-1x_dft_x8ens/sae_linfit.dat')  # noqa: E501
consts = torchani.neurochem.Constants(const_file)
aev_computer = torchani.AEVComputer(**consts)
energy_shifter = torchani.neurochem.load_sae(sae_file)


###############################################################################
# Now let's define atomic neural networks. Here in this demo, we use the same
# size of neural network for all atom types, but this is not necessary.
def atomic():
    model = torch.nn.Sequential(
        torch.nn.Linear(384, 128),
        torch.nn.CELU(0.1),
        torch.nn.Linear(128, 128),
        torch.nn.CELU(0.1),
        torch.nn.Linear(128, 64),
        torch.nn.CELU(0.1),
        torch.nn.Linear(64, 1)
    )
    return model


Gao, Xiang's avatar
Gao, Xiang committed
88
89
90
91
92
93
94
95
96
nn = torchani.ANIModel([atomic() for _ in range(4)])
print(nn)

###############################################################################
# If checkpoint from previous training exists, then load it.
if os.path.isfile(model_checkpoint):
    nn.load_state_dict(torch.load(model_checkpoint))
else:
    torch.save(nn.state_dict(), model_checkpoint)
Gao, Xiang's avatar
Gao, Xiang committed
97
98
99
100
101
102
103
104
105
106
107
108


###############################################################################
# The output energy tensor has shape ``(N, 1)`` where ``N`` is the number of
# different structures in each minibatch. However, in the dataset, the label
# has shape ``(N,)``. To make it possible to subtract these two tensors, we
# need to flatten the output tensor.
class Flatten(torch.nn.Module):
    def forward(self, x):
        return x[0], x[1].flatten()


Gao, Xiang's avatar
Gao, Xiang committed
109
model = torch.nn.Sequential(aev_computer, nn, Flatten()).to(device)
Gao, Xiang's avatar
Gao, Xiang committed
110
111
112
113
114
115
116
117
118
119
120
121
122


###############################################################################
# Now setup tensorboardX.
writer = tensorboardX.SummaryWriter(log_dir=log)

###############################################################################
# Now load training and validation datasets into memory. Note that we need to
# subtracting energies by the self energies of all atoms for each molecule.
# This makes the range of energies in a reasonable range. The second argument
# defines how to convert species as a list of string to tensor, that is, for
# all supported chemical symbols, which is correspond to ``0``, which
# correspond to ``1``, etc.
123
training = torchani.data.BatchedANIDataset(
Gao, Xiang's avatar
Gao, Xiang committed
124
125
    training_path, consts.species_to_tensor, batch_size, device=device,
    transform=[energy_shifter.subtract_from_dataset])
126

Gao, Xiang's avatar
Gao, Xiang committed
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
validation = torchani.data.BatchedANIDataset(
    validation_path, consts.species_to_tensor, batch_size, device=device,
    transform=[energy_shifter.subtract_from_dataset])

###############################################################################
# When iterating the dataset, we will get pairs of input and output
# ``(species_coordinates, properties)``, where ``species_coordinates`` is the
# input and ``properties`` is the output.
#
# ``species_coordinates`` is a list of species-coordinate pairs, with shape
# ``(N, Na)`` and ``(N, Na, 3)``. The reason for getting this type is, when
# loading the dataset and generating minibatches, the whole dataset are
# shuffled and each minibatch contains structures of molecules with a wide
# range of number of atoms. Molecules of different number of atoms are batched
# into single by padding. The way padding works is: adding ghost atoms, with
# species 'X', and do computations as if they were normal atoms. But when
# computing AEVs, atoms with species `X` would be ignored. To avoid computation
# wasting on padding atoms, minibatches are further splitted into chunks. Each
# chunk contains structures of molecules of similar size, which minimize the
# total number of padding atoms required to add. The input list
# ``species_coordinates`` contains chunks of that minibatch we are getting. The
# batching and chunking happens automatically, so the user does not need to
# worry how to construct chunks, but the user need to compute the energies for
# each chunk and concat them into single tensor.
#
# The output, i.e. ``properties`` is a dictionary holding each property. This
# allows us to extend TorchANI in the future to training forces and properties.
#
# We have tools to deal with these data types at :attr:`torchani.ignite` that
# allow us to easily combine the dataset with pytorch ignite. These tools can
# be used as follows:
container = torchani.ignite.Container({'energies': model})
optimizer = torch.optim.Adam(model.parameters())
160
trainer = ignite.engine.create_supervised_trainer(
161
    container, optimizer, torchani.ignite.MSELoss('energies'))
162
evaluator = ignite.engine.create_supervised_evaluator(container, metrics={
163
        'RMSE': torchani.ignite.RMSEMetric('energies')
164
165
166
    })


Gao, Xiang's avatar
Gao, Xiang committed
167
168
###############################################################################
# Now let's register some event handlers to work with tqdm to display progress:
169
170
@trainer.on(ignite.engine.Events.EPOCH_STARTED)
def init_tqdm(trainer):
Gao, Xiang's avatar
Gao, Xiang committed
171
172
    trainer.state.tqdm = tqdm.tqdm(total=len(training),
                                   file=sys.stdout, desc='epoch')
173
174


175
@trainer.on(ignite.engine.Events.ITERATION_COMPLETED)
176
177
def update_tqdm(trainer):
    trainer.state.tqdm.update(1)
178
179
180


@trainer.on(ignite.engine.Events.EPOCH_COMPLETED)
181
182
def finalize_tqdm(trainer):
    trainer.state.tqdm.close()
183
184


Gao, Xiang's avatar
Gao, Xiang committed
185
186
187
188
189
190
###############################################################################
# And some event handlers to compute validation and training metrics:
def hartree2kcal(x):
    return 627.509 * x


191
@trainer.on(ignite.engine.Events.EPOCH_STARTED)
192
def validation_and_checkpoint(trainer):
193
194
195
196
197
198
199
200
201
202
203
204
    def evaluate(dataset, name):
        evaluator = ignite.engine.create_supervised_evaluator(
            container,
            metrics={
                'RMSE': torchani.ignite.RMSEMetric('energies')
            }
        )
        evaluator.run(dataset)
        metrics = evaluator.state.metrics
        rmse = hartree2kcal(metrics['RMSE'])
        writer.add_scalar(name, rmse, trainer.state.epoch)

205
    # compute validation RMSE
Gao, Xiang's avatar
Gao, Xiang committed
206
    evaluate(validation, 'validation_rmse_vs_epoch')
207
208

    # compute training RMSE
Gao, Xiang's avatar
Gao, Xiang committed
209
    if trainer.state.epoch % training_rmse_every == 1:
210
        evaluate(training, 'training_rmse_vs_epoch')
211

Gao, Xiang's avatar
Gao, Xiang committed
212
213
214
    # checkpoint model
    torch.save(nn.state_dict(), model_checkpoint)

215

Gao, Xiang's avatar
Gao, Xiang committed
216
217
218
###############################################################################
# Also some to log elapsed time:
start = timeit.default_timer()
219
220
221
222
223
224
225
226


@trainer.on(ignite.engine.Events.EPOCH_STARTED)
def log_time(trainer):
    elapsed = round(timeit.default_timer() - start, 2)
    writer.add_scalar('time_vs_epoch', elapsed, trainer.state.epoch)


Gao, Xiang's avatar
Gao, Xiang committed
227
228
###############################################################################
# Also log the loss per iteration:
229
@trainer.on(ignite.engine.Events.ITERATION_COMPLETED)
Gao, Xiang's avatar
Gao, Xiang committed
230
def log_loss(trainer):
231
    iteration = trainer.state.iteration
232
    writer.add_scalar('loss_vs_iteration', trainer.state.output, iteration)
233
234


Gao, Xiang's avatar
Gao, Xiang committed
235
236
237
###############################################################################
# And finally, we are ready to run:
trainer.run(training, max_epochs)