-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
81 lines (63 loc) · 1.99 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import torch
from torch import nn
from dscribe.descriptors import SOAP
from sklearn.preprocessing import StandardScaler
import pickle
import numpy as np
device = "cuda" if torch.cuda.is_available() else "cpu"
# SOAP descriptors
def Descriptors(atoms, positions):
"""
Generates and scales the SOAP descriptors for a given set of atoms and their positions.
Parameters:
- atoms (list): all W atoms by loaded by ase (Atomic Simulation Environment)
- positions (array): Array of TIS positions. positions.shape = (position_number, 3)
"""
species = ["W"]
rcut = 6
nmax = 8
lmax = 6
sigma = 0.3
# size = 252
soap = SOAP(
species=species,
periodic=True,
rcut=rcut,
nmax=nmax,
lmax=lmax,
sigma = sigma
)
# Scaler
descriptors_soap = soap.create(atoms, positions=positions)
scaler = pickle.load(open("scaler.pkl", 'rb'))
descriptors_soap_scaled = scaler.transform(descriptors_soap)
soaps = torch.Tensor(descriptors_soap_scaled).to(device)
return soaps
# ANN model
class NeuralNetwork(nn.Module):
def __init__(self, soap_size):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_rule_stack = nn.Sequential(
nn.Linear(soap_size, 64),
nn.ReLU(),
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, 1),
)
self.linear_rule_stack.apply(self.init_weights)
def init_weights(self,m):
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
def forward(self,x):
x = nn.functional.normalize(x)
y = self.linear_rule_stack(x)
return y
def Model(soap_size=252):
model = NeuralNetwork(soap_size).to(device)
model_state_dict = torch.load('tis.model')
model.load_state_dict(model_state_dict)
return model