model.py 944 Bytes
Newer Older
1
2
3
4
5
import torch
import torchani
import os


6
7
8
9
builtins = torchani.neurochem.Builtins()
consts = builtins.consts
aev_computer = builtins.aev_computer
shift_energy = builtins.energy_shifter
10
11


12
13
14
15
16
17
18
19
20
21
22
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
23
24


25
def get_or_create_model(filename, device=torch.device('cpu')):
26
    model = torchani.ANIModel([atomic() for _ in range(4)])
27
28
29
30

    class Flatten(torch.nn.Module):

        def forward(self, x):
31
            return x[0], x[1].flatten()
32

33
    model = torch.nn.Sequential(aev_computer, model, Flatten())
34
35
36
37
    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
38
    return model.to(device)