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


6
7
8
9
consts = torchani.buildins.consts
aev_computer = torchani.buildins.aev_computer


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


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

    class Flatten(torch.nn.Module):

        def forward(self, x):
29
            return x[0], x[1].flatten()
30

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