model.py 929 Bytes
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, device=torch.device('cpu')):
Gao, Xiang's avatar
Gao, Xiang committed
20
    aev_computer = torchani.buildins.aev_computer
21
22
23
24
25
26
    model = torchani.ANIModel([
        ('C', atomic()),
        ('H', atomic()),
        ('N', atomic()),
        ('O', atomic()),
    ])
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)