model.py 1.14 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
    prepare = torchani.PrepareInput(aev_computer.species)
23
24
25
    model = torchani.models.CustomModel(
        benchmark=benchmark,
        per_species={
26
27
28
29
            'C': atomic(),
            'H': atomic(),
            'N': atomic(),
            'O': atomic(),
30
        })
31
32
33
34

    class Flatten(torch.nn.Module):

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

    model = torch.nn.Sequential(prepare, aev_computer, model, Flatten())
38
39
40
41
    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
42
    return model.to(device), torchani.EnergyShifter(aev_computer.species)