-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwater5_normalize.py
239 lines (194 loc) · 7.39 KB
/
water5_normalize.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import torch
import math
import numpy as np
import dscribe
from ase.build import molecule
from dscribe.descriptors import SOAP
from ase.io import read, write
from ase import Atom, Atoms
import time
import os
import sys
import pickle
from ase.io.cube import read_cube, write_cube
from sklearn.metrics import explained_variance_score, mean_squared_error
import py3Dmol
sys.path.append('deepcdp/')
from deepcdp import deepcdp
import torch.nn as nn
import torch.nn.functional as F
from torchsummaryX import summary
from torch.utils.data import Dataset, DataLoader
from torch.nn.utils.rnn import pad_sequence, pack_padded_sequence, pad_packed_sequence
import pandas as pd
import sklearn.metrics as skmetrics
from sklearn.neural_network import MLPRegressor
import gc
import zipfile
import pandas as pd
from tqdm import tqdm
from ase.build import bulk
from ase.visualize.plot import plot_atoms
import datetime
import matplotlib.pyplot as plt
import random
import torch.utils.data as utils_data
from torch.autograd import Variable
from torch.nn.modules.dropout import Dropout
import warnings
warnings.filterwarnings('ignore')
device = 'mps' if torch.backends.mps.is_available() else 'cpu'
print("Device: ", device)
# This ensures that the current MacOS version is at least 12.3+
print(torch.backends.mps.is_available())
# This ensures that the current current PyTorch installation was built with MPS activated.
print(torch.backends.mps.is_built())
rcut = 5 # Local cut off
nmax = 4 # max number of radial basis functions
lmax = 4 # maximum degree of spherical harmonics
soap_W = SOAP( # W = with weighting
species=['O','H'],
rcut=rcut,
nmax=nmax,
lmax=nmax,
sigma=0.5,
periodic=True,
sparse=False,
weighting={"function":"poly","r0":1.5,"c":2,"m":2}
)
# Use the 6 DFT data points.
subsample=10
# Path of the file and filename prefix
datapath = 'data/water5/'
datafilename = 'h2o-ELECTRON_DENSITY-1_'
all_files = os.listdir(f'{datapath}')
data_files =[x for x in all_files
if x.split('.')[-1] == 'cube']
idxList=[int(x.split('.cube')[0].split('_')[-1])
for x in data_files if x.split('.')[-1] == 'cube']
cubefile = lambda x:f'{datapath}{x}' # creating a lambda function
print(idxList)
# generating an object of type deepcdp (imported)
CDP=deepcdp(soap_W)
# generates a box of points. Be mindful of the gamma value used.
CDP.create_box(sample_cubeFile=datapath+data_files[0], gamma=90)
totalfiles=int(os.popen(f'ls {datapath} | wc -l').read().split('\n')[0])
# useing ase's read_cube to read a sample cube file
dicttest=read_cube(open(datapath+data_files[0]))
sampleSubs=dicttest['atoms']
sampleSubs.set_pbc((True, True, True))
# randomly picking snapshots for training
data_files_sub = random.sample(data_files, k=subsample)
# generating final dataset
CDP.generate_cube_data(data_files_sub,cubefile,sampleSubs)
normX = CDP.trainX - np.mean(CDP.trainX,axis=0)/np.std(CDP.trainX,axis=0)
soaps=Variable(torch.Tensor(normX))
rhos=Variable(torch.Tensor(CDP.trainY))
# Define the model class
torch.manual_seed(1)
class NN(torch.nn.Module):
def __init__(self, input_size, hidden_sizes, output_size):
super(NN, self).__init__()
hidden_1, hidden_2, hidden_3, = hidden_sizes
layers = [
nn.Linear(input_size, hidden_1),
# nn.BatchNorm1d(hidden_1),
nn.ReLU(),
nn.Linear(hidden_1, hidden_2),
# nn.BatchNorm1d(hidden_2),
nn.ReLU(),
nn.Linear(hidden_2, hidden_3),
# nn.BatchNorm1d(hidden_3),
# nn.ReLU(),
# nn.Linear(hidden_3, hidden_4),
# # nn.BatchNorm1d(hidden_3),
# nn.ReLU(),
# nn.Linear(hidden_4, hidden_5),
# nn.BatchNorm1d(hidden_3),
nn.ReLU(),
nn.Linear(hidden_3, output_size)
]
self.layers = nn.Sequential(*layers)
# def _init_weights(self, m):
# for param in m.parameters():
# nn.init.uniform_(param.data, -1, 1)
def forward(self, A0):
x = self.layers(A0)
return x
batch_size=500
# Define the hyperparameters
num_epochs = 40
learning_rate = 1e-3
# Define the model and loss function
model = NN(soaps.shape[1], [500,500,500], 1).to(device)
# Defining loss function
criterion = nn.MSELoss()
# Using the Adam optimizer
optimizer = torch.optim.Adam(model.parameters(),
lr=learning_rate,
weight_decay=1e-4)
# Using a ReduceLROnPlateau scheduler on the loss function to tweak the LR.
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,
mode="min",
factor=0.5,
patience=2,
verbose=True,
threshold=0.01)
training_samples = utils_data.TensorDataset(soaps, rhos)
data_loader_trn = utils_data.DataLoader(training_samples,
batch_size=batch_size,
drop_last=False,
shuffle=True)
model_dict=torch.load('sample-models/water5/water-test1-norm4.pt')
model.load_state_dict(model_dict['model_state_dict'])
optimizer.load_state_dict(model_dict['optimizer_state_dict'])
scheduler.load_state_dict(model_dict['scheduler_state_dict'])
# Train the model
cum_loss_epoch=[]
for epoch in range(num_epochs):
batch_bar = tqdm(total=len(data_loader_trn), dynamic_ncols=True, leave=False, position=0, desc='Train')
model.train()
cum_loss = 0
# Convert inputs and labels to tensors
for batch_idx, (data, target) in enumerate(data_loader_trn):
inputs = (data.float()).to(device)
labels = (target.float()).reshape(-1,1).to(device)
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
cum_loss+=loss.item()
batch_bar.set_postfix(
loss="{:.04f}".format(cum_loss),
lr="{:.04f}".format(float(optimizer.param_groups[0]['lr'])))
batch_bar.update()
batch_bar.close()
print("Epoch {}/{}: Train Loss {:.04f}, Learning Rate {:.04f}".format(
epoch + 1,
num_epochs,
float(cum_loss),
float(float(optimizer.param_groups[0]['lr']))
))
scheduler.step(cum_loss)
cum_loss_epoch +=[cum_loss]
np.savetxt('sample-models/water/cum_loss-test2-500.dat',cum_loss_epoch)
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {cum_loss:.4f}')
plt.plot(model(soaps.to(device)).cpu().detach().numpy())
plt.plot(rhos, alpha=0.5)
plt.show()
if (epoch+1)%2==0:
model.eval()
acc=skmetrics.r2_score(model(soaps.to(device)).cpu().detach().numpy(),rhos)
print(f"Training R2 value: {acc:1.4f}")
if (epoch+1) % 5 == 0:
print('Saving model')
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict' : scheduler.state_dict(),
}, f"sample-models/LiF-torch/water-test2-500-"+str(epoch)+".pt")
# print()