-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtorch-ae.py
More file actions
167 lines (145 loc) · 5.64 KB
/
torch-ae.py
File metadata and controls
167 lines (145 loc) · 5.64 KB
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
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import numpy as np
class MyDataParallel(nn.DataParallel):
def __getattr__(self, name):
return getattr(self.module, name)
class torch_AE_Dataset(torch.utils.data.Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, index):
return self.data[index], self.data[index]
class torch_AE(nn.Module):
def __init__(self, input_size, encoder_size, latent_size, decoder_size=None, mirror=False):
super(torch_AE, self).__init__()
self.input_size = input_size
self.latent_size = latent_size
self.encoder_size = [self.input_size] + encoder_size + [self.latent_size]
if mirror == False:
self.encoder_size = [self.input_size] + encoder_size + [self.latent_size]
else:
self.decoder_size = [self.latent_size] + encoder_size[::-1] + [self.input_size]
self.encoder = nn.ModuleList()
self.decoder = nn.ModuleList()
self.activ = nn.Tanh()
#self.activ = nn.ReLU()
for layer_index in range(len(self.encoder_size)-1):
self.encoder.append(nn.Linear(self.encoder_size[layer_index],self.encoder_size[layer_index+1]))
for layer_index in range(len(self.decoder_size)-1):
self.decoder.append(nn.Linear(self.decoder_size[layer_index],self.decoder_size[layer_index+1]))
def forward(self, x):
output = self.encode(x)
output = self.decode(output)
return output
def encode(self, x):
output = x
for layer_index in range(len(self.encoder_size)-1):
output = self.encoder[layer_index](output)
output = self.activ(output)
return output
def decode(self, x):
output = x
for layer_index in range(len(self.encoder_size)-2):
output = self.decoder[layer_index](output)
output = self.activ(output)
output = self.decoder[len(self.encoder_size)-2](output)
return output
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
random_split_seed = 42
num_epochs = 50
batch_size = 10000
test_ratio = 0.01
learning_rate = 0.001
latent_size = 2
encoder_size = [64,256,64]
mirror = True
adir = "cycloalkane/c6-kabsch"
raw_data_filename = adir+"/dscrp-cyclohexane-bb-600k-kabsch"
model_sufix = "-600k-kabsch"
useweights = False
"""
bb_dscrp = [0,1,2,24,25,26]
h_dscrp = []
for i in range(dim):
if i not in bb_dscrp:
h_dscrp.append(i)
weigths = [1 if i in h_dscrp else 0 for i in range(dim)]
weigths = torch.tensor(weigths)
"""
raw_data = []
with open(raw_data_filename,"r") as fin:
for aline in fin:
if "Frame" not in aline:
linelist = aline.strip().split()
raw_data.append([float(i) for i in linelist[1:]])
#raw_data.append([float(linelist[i]) for i in range(1,dim+1)])
dim = len(raw_data[0])
print("Input dimension: %d"%(dim))
raw_dataset = torch_AE_Dataset(torch.from_numpy(np.array(raw_data,dtype=np.float32)))
total_n_sample = len(raw_dataset)
input_size = len(raw_dataset[0][0])
total_n_test_sample = int(test_ratio*total_n_sample)
total_n_train_sample = total_n_sample - total_n_test_sample
print('Number of training samples: %d'%(total_n_train_sample))
print('Number of testing samples: %d'%( total_n_test_sample))
train_dataset, test_dataset = torch.utils.data.random_split(raw_dataset, [total_n_train_sample, total_n_test_sample], generator=torch.Generator().manual_seed(random_split_seed))
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size,shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size,shuffle=False)
'''
examples= iter(train_loader)
X, y = examples.next()
print(X.shape,y.shape)
print(X,y)
'''
model = torch_AE(input_size, encoder_size, latent_size, mirror=mirror)
#print("Let's use", torch.cuda.device_count(), "GPUs!")
#model = nn.DataParallel(model)
#odel = MyDataParallel(model)
model.to(device)
if useweights == True:
lossfunc = nn.MSELoss(reduction='none')
weigths = weigths.to(device)
else:
lossfunc = nn.MSELoss()
optim = torch.optim.Adam(model.parameters(), lr=learning_rate)
#optim = torch.optim.SGD(model.parameters(), lr=0.01, momentum = 0.9 ,dampening = 0.01)
n_total_steps = len(train_loader)
for epoch in range(num_epochs):
for i, (X, y) in enumerate(train_loader):
X = X.to(device)
y = y.to(device)
#weigths = weights.to(device)
output = model(X)
loss = lossfunc(output, y)
if useweights == True:
loss = (loss * weigths / weigths.sum()).mean()
optim.zero_grad()
loss.backward()
optim.step()
#if i % 10 == 0:
# print(i*batch_size)
#print("")
print('epoch %d / %d\tloss = %.4e'%(epoch+1,num_epochs,loss.item()))
avg_loss = []
with torch.no_grad():
n_correct = 0
n_samples = 0
for X, y in test_loader:
X = X.to(device)
y = y.to(device)
output = model(X)
loss = lossfunc(output, y)
if useweights == True:
loss = (loss * weigths / weigths.sum()).mean()
n_samples += y.shape[0]
avg_loss.append(loss.item())
print('number of test samples = %d\naverage loss = %.4e'%(n_samples,np.mean(np.array(avg_loss))))
modelname = adir+'/ae'
for i in [input_size]+encoder_size+[latent_size]:
modelname += '-%d'%i
modelname += model_sufix+'.pt'
torch.save(model.state_dict(), modelname)