model.py 1.07 KB
Newer Older
1
2
3
4
5
import torch
import torchani
import os


6
7
8
9
10
11
12
13
14
15
16
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
17
18
19


def get_or_create_model(filename, benchmark=False,
20
                        device=torch.device('cpu')):
21
    aev_computer = torchani.AEVComputer(benchmark=benchmark)
22
23
24
    model = torchani.models.CustomModel(
        benchmark=benchmark,
        per_species={
25
26
27
28
            'C': atomic(),
            'H': atomic(),
            'N': atomic(),
            'O': atomic(),
29
        })
30
31
32
33

    class Flatten(torch.nn.Module):

        def forward(self, x):
34
            return x[0], x[1].flatten()
35

36
    model = torch.nn.Sequential(aev_computer, model, Flatten())
37
38
39
40
    if os.path.isfile(filename):
        model.load_state_dict(torch.load(filename))
    else:
        torch.save(model.state_dict(), filename)
Gao, Xiang's avatar
Gao, Xiang committed
41
    return model.to(device), torchani.EnergyShifter(aev_computer.species)