-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathModel.py
More file actions
178 lines (149 loc) · 7.7 KB
/
Model.py
File metadata and controls
178 lines (149 loc) · 7.7 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
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
# encoding: utf-8
import torch
import torch.nn as nn
import torch.nn.functional as F
import dgl
def activation_func(activation):
return nn.ModuleDict([
['relu', nn.ReLU(inplace=True)],
['leaky_relu', nn.LeakyReLU(negative_slope=0.01, inplace=True)],
])[activation]
def norm_func(norm, n_channel):
return nn.ModuleDict([
['instance', nn.InstanceNorm1d(n_channel)],
])[norm]
class AtomEmbLayer(nn.Module):
def __init__(self, n_in, n_out,
atom_emb_in=7, atom_emb_h=256,
norm='instance', activation='relu',
*args, **kwargs
):
super(AtomEmbLayer, self).__init__()
self.norm = norm
self.fn_atom_norm = norm_func(norm, atom_emb_in)
self.fn_atom_linear = nn.Linear(atom_emb_in, atom_emb_h, bias=False)
self.fn_atom_activation = activation_func(activation)
self.fn_atom_norm2 = norm_func(norm, atom_emb_h)
self.fn_atom_linear2 = nn.Linear(atom_emb_h, atom_emb_h, bias=False)
def forward(self, G):
atom_emb = G.ndata['atom_emb'] ## atom_emb: [40, 14, 7]
# first layer
atom_emb = self.fn_atom_norm(atom_emb) ## atom_emb: [40, 14, 7]
atom_emb = self.fn_atom_linear(atom_emb) ## atom_emb: [40, 14, 256]
atom_emb = torch.mean(atom_emb, 1) ## atom_emb: [40, 256]
atom_emb = self.fn_atom_activation(atom_emb) ## atom_emb: [40, 256]
# second layer
atom_emb = self.fn_atom_norm2(atom_emb.unsqueeze(1)).squeeze() if self.norm=="instance" else self.fn_atom_norm2(atom_emb) ## atom_emb: [40, 256]
atom_emb = self.fn_atom_linear2(atom_emb) ## atom_emb: [40, 256]
atom_emb = self.fn_atom_activation(atom_emb) ## atom_emb: [40, 256]
# cat
x = torch.cat((G.ndata['nfeat'], atom_emb), dim=1)
G.ndata['nfeat'] = x ## atom_emb: [40, 284]
return G
class EdgeApplyModule(nn.Module):
def __init__(self, n_in, n_out, norm='instance', activation='leaky_relu', LSTM=False, *args, **kwargs):
super(EdgeApplyModule, self).__init__()
self.norm, self.LSTM = norm, LSTM
self.fn_norm = norm_func(norm, n_in)
self.fn_linear = nn.Linear(n_in, n_out)
self.fn_activation = activation_func(activation)
if self.LSTM: self.fn_lstm = nn.LSTMCell(n_out, n_out, bias=False)
self.attn_fc = nn.Linear(n_out, 1, bias=False)
def forward(self, edges):
# edges.src['nfeat']:[664, 284], edges.data['efeat']:[664, 15], edges.dst['nfeat']:[664, 284]
x = torch.cat((edges.src['nfeat'], edges.data['efeat'], edges.dst['nfeat']), 1) # [664, 583]
x = self.fn_norm(x.unsqueeze(1)).squeeze() if self.norm=="instance" else self.fn_norm(x) # [664, 583]
x = self.fn_linear(x) # [664, 256]
x = self.fn_activation(x) # [664, 256]
if self.LSTM:
# hidden: efeat, cell: efeat_c
if not 'efeat_c' in edges.data: edges.data['efeat_c'] = torch.zeros_like(edges.data['efeat'])
x, c = self.fn_lstm(x, (edges.data['efeat'], edges.data['efeat_c']))
attn = self.attn_fc(x) # [664, 1]
if self.LSTM: return {'efeat': x, 'attn': attn, 'efeat_c': c}
return {'efeat': x, 'attn': attn}
def message_func(edge):
return {'_efeat': edge.data['efeat'], '_attn': edge.data['attn']}
def reduce_func(node):
alpha = F.softmax(node.mailbox['_attn'], dim=1) # node.batch_size: dgl will batch nodes with same degrees node.mailbox['_attn']: [1, 6, 1]
attn_feat = torch.sum(alpha * node.mailbox['_efeat'], dim=1) # alpha: [1, 6, 1] node.mailbox['_efeat']: [1, 6, 256] attn_feat: [1,256]
feat = torch.cat((node.data['nfeat'], attn_feat), 1) # feat: [1, 540]
return {'_nfeat': feat}
class NodeApplyModule(nn.Module):
def __init__(self, n_in, n_out, norm='instance', activation='relu', LSTM=False,
*args, **kwargs
):
super(NodeApplyModule, self).__init__()
self.norm, self.LSTM = norm, LSTM
self.fn_norm = norm_func(norm, n_in)
self.fn_linear = nn.Linear(n_in, n_out)
self.fn_activation = activation_func(activation)
if self.LSTM: self.fn_lstm = nn.LSTMCell(n_out, n_out, bias=False)
def forward(self, nodes):
x = nodes.data['_nfeat']
x = self.fn_norm(x.unsqueeze(1)).squeeze() if self.norm=="instance" else self.fn_norm(x)
x = self.fn_linear(x)
x = self.fn_activation(x)
if self.LSTM:
if not 'nfeat_c' in nodes.data: nodes.data['nfeat_c'] = torch.zeros_like(nodes.data['nfeat'])
x, c = self.fn_lstm(x, (nodes.data['nfeat'], nodes.data['nfeat_c']))
return {'nfeat': x, 'nfeat_c': c}
return {'nfeat': x}
class MessagePassingLayer(nn.Module):
def __init__(self,
node_n_in, node_n_out, edge_n_in, edge_n_out,
norm='instance', activation='relu',
LSTM=False, last_layer=False,
atom_emb=False, atom_emb_h=256,
*args, **kwargs):
super(MessagePassingLayer, self).__init__()
self.LSTM, self.last_layer = LSTM, last_layer
self.edge_update = EdgeApplyModule(edge_n_in+node_n_in*2, edge_n_out, norm=norm, LSTM=LSTM)
self.node_update = NodeApplyModule(node_n_in+edge_n_out, node_n_out, norm, activation, LSTM=LSTM, )
def forward(self, G):
G.apply_edges(self.edge_update)
G.update_all(message_func, reduce_func, self.node_update)
if self.last_layer: G.apply_edges(self.edge_update)
return G
class GNN(nn.Module):
def __init__(self,
node_n_in=28, node_n_hidden=256, edge_n_in=15, edge_n_hidden=256, n_layers=10, n_output=37,
LSTM=True, atom_emb_in=7, atom_emb_h=256, norm='instance', activation='relu',
distCB=True, QA=False, *args, **kwargs):
super(GNN, self).__init__()
self.distCB, self.QA = distCB, QA
self.layers = nn.ModuleList()
# AtomEmbLayer
self.layers.append(AtomEmbLayer(node_n_in, node_n_hidden, atom_emb_in, atom_emb_h, norm, activation, ))
# first MessagePassingLayer
self.layers.append(MessagePassingLayer(node_n_in+atom_emb_h, node_n_hidden, edge_n_in, edge_n_hidden, norm, activation, ))
# intermediate MessagePassingLayers
for i in range(n_layers - 2):
self.layers.append(MessagePassingLayer(node_n_hidden, node_n_hidden, edge_n_hidden, edge_n_hidden, norm, activation, LSTM=True))
# last MessagePassingLayer
self.layers.append(MessagePassingLayer(node_n_hidden, node_n_hidden, edge_n_hidden, edge_n_hidden, norm, activation, LSTM=True, last_layer=True))
# distCB layer
if self.distCB:
self.output_layer = nn.Sequential(nn.Linear(edge_n_hidden, n_output))
# QA layer
if self.QA:
self.global_qa_linear = nn.Linear(node_n_hidden+edge_n_hidden, 1)
self.local_qa_linear = nn.Linear(node_n_hidden, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, G):
for layer in self.layers:
G = layer(G)
output = {}
# distCB
if self.distCB:
output['distCB'] = self.output_layer(G.edata['efeat']) #[664, 37]
print(G.edata['nfeat'])
# global and local QA
if self.QA:
h_global = torch.cat((dgl.mean_nodes(G, 'nfeat'), dgl.mean_edges(G, 'efeat')), 1)
y_global = self.sigmoid(self.global_qa_linear(h_global))
output['global_lddt'] = y_global
y_local = self.sigmoid(self.local_qa_linear(G.ndata['nfeat']))
output['local_lddt'] = y_local
return output