-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
461 lines (381 loc) · 18.6 KB
/
models.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import sys
import torch
import time
import json
import os
import copy
import numpy as np
import pandas as pd
import torch.optim as optim
from torch.utils.data import DataLoader, Subset, SubsetRandomSampler
import torch.nn as nn
from torch.nn import ( Linear, Bilinear, Sigmoid, Softplus, ELU, ReLU, SELU,
CELU, BatchNorm1d, ModuleList, Sequential,Tanh )
from torch.nn.modules.module import Module
import torch.nn.functional as F
from torch.nn.utils import clip_grad_value_
import re
from datetime import datetime
def get_activation(name):
act_name = name.lower()
m = re.match(r"(\w+)\((\d+\.\d+)\)", act_name)
if m is not None:
act_name, alpha = m.groups()
alpha = float(alpha)
print(act_name, alpha)
else:
alpha = 1.0
if act_name == 'softplus':
return Softplus()
elif act_name == 'ssp':
return SSP()
elif act_name == 'elu':
return ELU(alpha)
elif act_name == 'relu':
return ReLU()
elif act_name == 'selu':
return SELU()
elif act_name == 'celu':
return CELU(alpha)
elif act_name == 'sigmoid':
return Sigmoid()
elif act_name == 'tanh':
return Tanh()
else:
raise NameError("Not supported activation: {}".format(name))
class Metric(object):
def __init__(self, metric_fn, name):
self.metric_fn = metric_fn
self.name = name
self.total_metric = 0.0
self.total_count = 0
def __call__(self, predictoins, targets):
return self.metric_fn(predictoins, targets)
def add_batch_metric(self, predictoins, targets):
metric_tensor = self.metric_fn(predictoins, targets)
self.total_metric += metric_tensor.item() * targets.size(0)
self.total_count += targets.size(0)
return metric_tensor
def get_total_metric(self):
score = self.total_metric / self.total_count
self.total_metric = 0.0
self.total_count = 0
return score
class History(object):
def __init__(self,file_path="data/history.csv"):
self.history_path = file_path
self.file = open(file_path, 'w')
self.first = True
def write(self, epoch, metrics):
if self.first:
self.first = False
header = ','.join([name for name, _ in metrics])
header = 'epoch,' + header + '\n'
self.file.write(header)
row = ','.join(['{}'.format(metric) for _, metric in metrics])
row = '{},'.format(epoch) + row + '\n'
self.file.write(row)
self.file.flush()
def close(self):
self.file.close()
class Checkpoint(object):
def __init__(self, model):
self.model = model
self.best_metric = None
self.best_weights = model.weights
def check(self, metric):
if self.best_metric is None or metric < self.best_metric:
self.best_metric = metric
self.best_weights = self.model.weights
def restore(self):
self.model.weights = self.best_weights
class Model(object):
def __init__(self, time, device, model, name, optimizer, scheduler, clip_value=None,
metrics=[('loss', nn.MSELoss()), ('mae', nn.L1Loss())]):
self.time=time
self.name=name
self.model=model.to(device)
self.optimizer = optimizer
self.scheduler = scheduler
self.metrics = [Metric(metric, name) for name, metric in metrics]
self.clip_value = clip_value
self.device=device
def _set_mode(self, phase):
if phase == 'train':
self.model.train() # Set model to training mode
else:
self.model.eval() # Set model to evaluate mode
def _process_batch(self, input, targets, phase):
nodes=input.nodes.to(self.device)
edge_sources=input.edge_sources.to(self.device)
edge_targets=input.edge_targets.to(self.device)
edge_distance=input.edge_distance.to(self.device)
graph_indices=input.graph_indices.to(self.device)
node_counts=input.node_counts.to(self.device)
combine_sets=input.combine_sets.to(self.device)
plane_wave=input.plane_wave.to(self.device)
targets = targets.to(self.device)
self.optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = self.model(nodes,edge_sources,edge_targets,edge_distance,graph_indices,node_counts,combine_sets,plane_wave)
metric_tensors = [metric.add_batch_metric(outputs, targets) for metric in self.metrics]
if phase == 'train':
loss = metric_tensors[0]
loss.backward()
if self.clip_value is not None:
clip_grad_value_(self.model.parameters(), self.clip_value)
self.optimizer.step()
return metric_tensors, outputs
def train(self, train_dl, val_dl, num_epochs):
since = time.time()
dataloaders = {'train': train_dl, 'val': val_dl}
if not os.path.exists(f'data/{self.time}'):
os.makedirs(f'data/{self.time}')
history = History(file_path=f'data/{self.time}/history_{self.name}.csv'.format(self.name))
checkpoint = Checkpoint(self)
for epoch in range(num_epochs):
epoch_since = time.time()
print('Epoch {}/{}'.format(epoch, num_epochs - 1), flush=True)
#self.scheduler.step()
print('current lr:', self.optimizer.param_groups[0]['lr'])
train_val_metrics = []
for phase in ['train', 'val']:
self._set_mode(phase)
for input, targets in dataloaders[phase]:
_, outputs = self._process_batch(input, targets, phase)
epoch_metrics = [(metric.name, metric.get_total_metric()) for metric in self.metrics]
text = ' '.join(['{}: {:.4f}'.format(name, metric) for name, metric in epoch_metrics])
print('{} {}'.format(phase, text))
if phase == 'val':
metric = epoch_metrics[1][1]
checkpoint.check(metric)
train_val_metrics += [('_'.join([phase, name]), metric) for name, metric in epoch_metrics]
self.scheduler.step()
history.write(epoch, train_val_metrics)
time_elapsed = time.time() - epoch_since
print('Elapsed time (sec.): {:.3f}'.format(time_elapsed))
print()
history.close()
if num_epochs > 0:
time_elapsed = time.time() - since
print('Total elapsed time (sec.): {:.3f}'.format(time_elapsed))
print('The best val metric: {:4f}'.format(checkpoint.best_metric))
print()
# load the best model weights
checkpoint.restore()
def evaluate(self, dataloader):
self.model.eval() # Set model to evaluate mode
# Iterate over data.
all_outputs = []
all_targets = []
all_graph_vec = []
for input, targets in dataloader:
nodes=input.nodes.to(self.device)
edge_sources=input.edge_sources.to(self.device)
edge_targets=input.edge_targets.to(self.device)
edge_distance=input.edge_distance.to(self.device)
graph_indices=input.graph_indices.to(self.device)
node_counts=input.node_counts.to(self.device)
combine_sets=input.combine_sets.to(self.device)
plane_wave=input.plane_wave.to(self.device)
targets = targets.to(self.device)
with torch.set_grad_enabled(False):
outputs,graph_vec = self.model(nodes,edge_sources,edge_targets,edge_distance,graph_indices,node_counts,combine_sets,plane_wave,output_graph=True)
outputs = outputs.to(torch.device("cpu")).numpy()
targets = targets.to(torch.device("cpu")).numpy()
graph_vec = graph_vec.to(torch.device("cpu")).numpy()
all_outputs.append(outputs)
all_targets.append(targets)
all_graph_vec.append(graph_vec)
all_outputs = np.concatenate(all_outputs)
all_targets = np.concatenate(all_targets)
all_graph_vec = np.concatenate(all_graph_vec)
all_outputs = torch.FloatTensor(all_outputs).to(self.device)
all_targets = torch.FloatTensor(all_targets).to(self.device)
total_metrics = [(metric.name, metric(all_outputs, all_targets).item()) for metric in self.metrics]
text = ' '.join(['{}: {:.4f}'.format(name, metric) for name, metric in total_metrics])
print('test {}'.format(text))
all_outputs = all_outputs.to(torch.device("cpu")).numpy()
all_targets = all_targets.to(torch.device("cpu")).numpy()
return all_outputs, all_targets, all_graph_vec
def predict(self, dataloader):
self.model.eval() # Set model to evaluate mode
# Iterate over data.
all_outputs = []
all_graph_vec = []
for input in dataloader:
nodes = input.nodes.to(self.device)
edge_sources = input.edge_sources.to(self.device)
edge_targets = input.edge_targets.to(self.device)
edge_distance = input.edge_distance.to(self.device)
graph_indices = input.graph_indices.to(self.device)
node_counts = input.node_counts.to(self.device)
combine_sets = input.combine_sets.to(self.device)
plane_wave = input.plane_wave.to(self.device)
with torch.set_grad_enabled(False):
outputs, graph_vec = self.model(nodes, edge_sources, edge_targets, edge_distance, graph_indices,
node_counts, combine_sets, plane_wave, output_graph=True)
outputs = outputs.to(torch.device("cpu")).numpy()
graph_vec = graph_vec.to(torch.device("cpu")).numpy()
all_outputs.append(outputs)
all_graph_vec.append(graph_vec)
all_outputs = np.concatenate(all_outputs)
all_graph_vec = np.concatenate(all_graph_vec)
all_outputs = torch.FloatTensor(all_outputs).to(self.device)
all_outputs = all_outputs.to(torch.device("cpu")).numpy()
return all_outputs, all_graph_vec
def save(self, model_path="model"):
if not os.path.exists(f'model/{self.time}'):
os.makedirs(f'model/{self.time}')
model_path=f"model/{self.time}/model_{self.name}.pth"
torch.save(self.model.state_dict(), model_path)
def load(self, model_path):
self.model.load_state_dict(torch.load(model_path))
@property
def weights(self):
return copy.deepcopy(self.model.state_dict())
@weights.setter
def weights(self, state):
self.model.load_state_dict(state)
def _bn_act(num_features, activation, use_batch_norm=False):
# batch normal + activation
if use_batch_norm:
if activation is None:
return BatchNorm1d(num_features)
else:
return Sequential(BatchNorm1d(num_features), activation)
else:
return activation
class NodeEmbedding(Module):
"""
Node Embedding layer
"""
def __init__(self, in_features, out_features, activation=Sigmoid(),
use_batch_norm=False, bias=False):
super(NodeEmbedding, self).__init__()
self.linear = Linear(in_features, out_features, bias=bias)
self.activation = _bn_act(out_features, activation, use_batch_norm)
def forward(self, input):
output=self.linear(input)
output = self.activation(output)
return output
class OLP(Module):
def __init__(self, in_features, out_features, activation=ELU(),
use_batch_norm=False, bias=False):
# One layer Perceptron
super(OLP, self).__init__()
self.linear = Linear(in_features, out_features, bias=bias)
self.activation = _bn_act(out_features, activation, use_batch_norm)
def forward(self, input):
z = self.linear(input)
if self.activation:
z = self.activation(z)
return z
class Gated_pooling(Module):
def __init__(self, in_features, out_features, activation=ELU(),
use_batch_norm=False, bias=False):
super(Gated_pooling, self).__init__()
self.linear1 = Linear(in_features, out_features, bias=bias)
self.activation1 = _bn_act(out_features, activation, use_batch_norm)
self.linear2 = Linear(in_features, out_features, bias=bias)
self.activation2 = _bn_act(out_features, activation, use_batch_norm)
def forward(self, input,graph_indices,node_counts):
z = self.activation1(self.linear1(input))*self.linear2(input)
graphcount=len(node_counts)
device=z.device
blank=torch.zeros(graphcount,z.shape[1]).to(device)
blank.index_add_(0, graph_indices, z)/node_counts.unsqueeze(1)
#output = self.activation2(self.linear2(blank)) ################对每个图加起来
return blank
class GatedGraphConvolution(Module):
def __init__(self,n_node_feat, in_features, out_features, N_shbf ,N_srbf,n_grid_K,n_Gaussian, gate_activation=Sigmoid(),
use_node_batch_norm=False, use_edge_batch_norm=False,
bias=False, conv_type=0, MLP_activation=ELU()):
super(GatedGraphConvolution, self).__init__()
k1= n_Gaussian # k is the number of basis
k2=n_grid_K**3
self.linear1_vector = Linear(k1, out_features, bias=bias) # linear for combine sets
self.linear1_vector_gate = Linear(k1, out_features, bias=bias) # linear for combine sets
self.activation1_vector_gate = _bn_act(out_features, gate_activation, use_edge_batch_norm)
self.linear2_vector = Linear(k2, out_features, bias=bias) # linear for plane waves
self.linear2_vector_gate = Linear(k2, k2, bias=bias) # linear for plane waves
self.activation2_vector_gate = _bn_act(k2, gate_activation, use_edge_batch_norm)
self.linear_gate = Linear(in_features, out_features, bias=bias)
self.activation_gate = _bn_act(out_features, gate_activation, use_edge_batch_norm)
self.linear_MLP = Linear(in_features, out_features, bias=bias)
self.activation_MLP = _bn_act(out_features, MLP_activation, use_edge_batch_norm)
def forward(self, input,nodes, edge_sources, edge_targets, rij ,combine_sets,plane_wave,cutoff):
ni = input[edge_sources].contiguous()
nj = input[edge_targets].contiguous()
rij=rij.unsqueeze(1).contiguous()
mask=rij<cutoff
delta= (ni-nj)/rij
final_fe=torch.cat([ni,nj,delta],dim=1)
del ni,nj,delta
torch.cuda.empty_cache()
e_gate = self.activation_gate(self.linear_gate(final_fe))
e_MLP = self.activation_MLP(self.linear_MLP(final_fe))
z1 = self.linear1_vector(combine_sets)
gate=self.activation2_vector_gate(self.linear2_vector_gate(plane_wave))
z2 = self.linear2_vector(plane_wave*gate)
z = e_gate * e_MLP * (z1+z2) * mask
#z = e_gate * e_MLP * mask
del z1,z2,e_gate,e_MLP
torch.cuda.empty_cache()
output = input.clone()
output.index_add_(0, edge_sources, z)
return output
class geo_CGNN(nn.Module):
def __init__(self,n_node_feat, n_hidden_feat,n_GCN_feat,conv_bias,N_block,node_activation, MLP_activation,use_node_batch_norm,use_edge_batch_norm, N_shbf ,N_srbf, cutoff,max_nei,n_MLP_LR,n_grid_K,n_Gaussian):
super(geo_CGNN, self).__init__()
self.cutoff=cutoff
self.N_block=N_block
node_activation=get_activation(node_activation)
MLP_activation=get_activation(MLP_activation)
self.embedding = NodeEmbedding(n_node_feat, n_hidden_feat)
n2v_concatent_feat = n_hidden_feat*3 #ni+nj+delta
self.conv = [GatedGraphConvolution(n_node_feat,n2v_concatent_feat, n_hidden_feat, N_shbf ,N_srbf,n_grid_K,n_Gaussian,
gate_activation=node_activation, # sigmoid
MLP_activation=MLP_activation, # Elu >1
use_node_batch_norm=use_node_batch_norm,
use_edge_batch_norm=use_edge_batch_norm,
bias=conv_bias)]
self.MLP_psi2n=[OLP(n_hidden_feat, n_hidden_feat, activation=MLP_activation, use_batch_norm=use_node_batch_norm, bias=conv_bias)]
self.conv += [GatedGraphConvolution(n_node_feat,n2v_concatent_feat, n_hidden_feat, N_shbf ,N_srbf,n_grid_K,n_Gaussian,
gate_activation=node_activation,
MLP_activation=MLP_activation,
use_node_batch_norm=use_node_batch_norm,
use_edge_batch_norm=use_edge_batch_norm,
bias=conv_bias) for _ in range(N_block-1)]
self.conv=ModuleList(self.conv)
self.MLP_psi2n = [OLP(n_hidden_feat, n_hidden_feat, activation=MLP_activation, use_batch_norm=use_node_batch_norm, bias=conv_bias) for _ in range(N_block)]
self.MLP_psi2n=ModuleList(self.MLP_psi2n)
# gated pooling for every block
self.gated_pooling=[Gated_pooling(n_hidden_feat, n_GCN_feat, activation=MLP_activation ,use_batch_norm=use_node_batch_norm, bias=conv_bias) for _ in range(N_block)]
self.gated_pooling=ModuleList(self.gated_pooling)
# final linear regression
self.linear_regression=[OLP(int(n_GCN_feat/2**(i-1)), int(n_GCN_feat/2**i) , activation=MLP_activation, use_batch_norm=use_node_batch_norm, bias=conv_bias) for i in range(1,n_MLP_LR)]
self.linear_regression += [OLP(int(n_GCN_feat/2**(n_MLP_LR-1)), 1 , activation=None, use_batch_norm=None, bias=conv_bias)]
self.linear_regression=ModuleList(self.linear_regression)
'''
# final linear regression
self.linear_regression=[OLP(int(n_GCN_feat/i), int(n_GCN_feat/(i+1)) , activation=MLP_activation, use_batch_norm=use_node_batch_norm, bias=conv_bias) for i in range(1,n_MLP_LR)]
self.linear_regression += [OLP(int(n_GCN_feat/n_MLP_LR), 1 , activation=None, use_batch_norm=None, bias=conv_bias)]
self.linear_regression=ModuleList(self.linear_regression)
'''
def forward(self,nodes,edge_sources,edge_targets,edge_distance,graph_indices,node_counts,combine_sets,plane_wave,output_graph=False):
x = self.embedding(nodes)
Poolingresults=[]
for i in range(self.N_block):
x = self.conv[i](x,nodes, edge_sources, edge_targets,edge_distance,combine_sets,plane_wave,self.cutoff)
poo=self.gated_pooling[i](x,graph_indices,node_counts)
Poolingresults.append(poo)
x = self.MLP_psi2n[i](x)
graph_vec=torch.sum(torch.stack(Poolingresults),dim=0)
y=graph_vec
for lr in self.linear_regression:
y=lr(y)
if output_graph:
return y.squeeze(),graph_vec
else:
return y.squeeze()