"packaging/vscode:/vscode.git/clone" did not exist on "cc3259ba93a277725fc885ea372836a64c02e9a4"
model.py 1.18 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
17
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)
    )
    model.output_length = 1
    return model
18
19
20
21
22


def get_or_create_model(filename, benchmark=False,
                        device=torchani.default_device):
    aev_computer = torchani.SortedAEV(benchmark=benchmark, device=device)
23
    prepare = torchani.PrepareInput(aev_computer.species, aev_computer.device)
24
25
26
27
    model = torchani.models.CustomModel(
        reducer=torch.sum,
        benchmark=benchmark,
        per_species={
28
29
30
31
            'C': atomic(),
            'H': atomic(),
            'N': atomic(),
            'O': atomic(),
32
        })
33
34
35
36
37
38
39

    class Flatten(torch.nn.Module):

        def forward(self, x):
            return x.flatten()

    model = torch.nn.Sequential(prepare, aev_computer, model, Flatten())
40
41
42
43
44
    if os.path.isfile(filename):
        model.load_state_dict(torch.load(filename))
    else:
        torch.save(model.state_dict(), filename)
    return model.to(device)