model.py 1.03 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
20
21
22
23
24
25
26
27
28
def get_or_create_model(filename, device=torch.device('cpu')):
    consts = torchani.neurochem.Constants()
    sae = torchani.neurochem.load_sae()
    aev_computer = torchani.AEVComputer(**consts)
    model = torchani.ANIModel([
        ('C', atomic()),
        ('H', atomic()),
        ('N', atomic()),
        ('O', atomic()),
    ])
29
30
31
32

    class Flatten(torch.nn.Module):

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

35
    model = torch.nn.Sequential(aev_computer, model, Flatten())
36
37
38
39
    if os.path.isfile(filename):
        model.load_state_dict(torch.load(filename))
    else:
        torch.save(model.state_dict(), filename)
40
    return model.to(device), torchani.EnergyShifter(consts.species, sae)