-
Notifications
You must be signed in to change notification settings - Fork 2
/
layer.py
267 lines (237 loc) · 9.16 KB
/
layer.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
import torch
import dgl.nn as dglnn
from torch import nn
class HeteroLinear(nn.Module):
"""Apply linear transformations on heterogeneous inputs.
"""
def __init__(self, in_feats, hidden_feats, dropout=0., bn=False):
"""
Parameters
----------
in_feats : dict[key, int]
Input feature size for heterogeneous inputs. A key can be a string or a tuple of strings.
hidden_feats : int
Output feature size.
dropout : int
The dropout rate.
bn : bool
Use batch normalization or not.
"""
super(HeteroLinear, self).__init__()
self.linears = nn.ModuleDict()
for typ, typ_in_size in in_feats.items():
self.linears[str(typ)] = nn.Linear(typ_in_size, hidden_feats)
nn.init.xavier_uniform_(self.linears[str(typ)].weight)
if bn:
self.bn = nn.BatchNorm1d(hidden_feats)
else:
self.bn = False
if dropout != 0.:
self.dropout = nn.Dropout(dropout)
else:
self.dropout = False
def forward(self, feat):
out_feat = dict()
for typ, typ_feat in feat.items():
out_feat[typ] = self.linears[str(typ)](typ_feat)
if self.bn:
out_feat[typ] = self.bn(out_feat[typ])
if self.dropout:
out_feat[typ] = self.dropout(out_feat[typ])
return out_feat
class Node_Embedding(nn.Module):
"""HeteroGCN block.
"""
def __init__(self, rel_names, in_feats, hidden_feats, dropout=0., bn=False):
"""
Parameters
----------
in_feats : int
Input feature size.
hidden_feats : int
Output feature size.
dropout : int
The dropout rate.
bn : bool
Use batch normalization or not.
"""
super().__init__()
HeteroGraphdict = {}
for rel in rel_names:
graphconv = dglnn.GraphConv(in_feats, hidden_feats)
nn.init.xavier_normal_(graphconv.weight)
HeteroGraphdict[rel] = graphconv
self.embedding = dglnn.HeteroGraphConv(HeteroGraphdict, aggregate='sum')
self.prelu = nn.PReLU()
if bn:
self.bn = nn.BatchNorm1d(hidden_feats)
else:
self.bn = False
if dropout != 0.:
self.dropout = nn.Dropout(dropout)
else:
self.dropout = False
def forward(self, graph, inputs):
h = self.embedding(graph, inputs)
if self.bn:
h = {k: self.bn(v) for k, v in h.items()}
if self.dropout:
h = {k: self.dropout(v) for k, v in h.items()}
h = {k: self.prelu(v) for k, v in h.items()}
return h
class LayerAttention(nn.Module):
"""Layer attention block.
"""
def __init__(self, in_feats, hidden_feats=128):
"""
Parameters
----------
in_feats : int
Input feature size.
hidden_feats : int
Output feature size.
"""
super(LayerAttention, self).__init__()
self.project = nn.Sequential(
nn.Linear(in_feats, hidden_feats),
nn.Tanh(),
nn.Linear(hidden_feats, 1, bias=False)
)
def forward(self, z):
w = self.project(z).mean(0)
beta = torch.softmax(w, dim=0)
beta = beta.expand((z.shape[0],) + beta.shape)
return (beta * z).sum(1)
class MetaPathAggregator(nn.Module):
"""Aggregating the meta-path instances in each bags.
"""
def __init__(self, in_feats, hidden_feats, agg_type='sum', dropout=0., bn=False):
"""
Parameters
----------
in_feats : int
Input feature size.
hidden_feats : int
Output feature size.
agg_type : ["sum", "mean", "Linear", "BiTrans"]
The aggregator to be used.
dropout : int
The dropout rate.
bn : bool
Use batch normalization or not.
"""
super(MetaPathAggregator, self).__init__()
self.agg_type = agg_type
if agg_type == 'sum':
self.aggregator = torch.sum
elif agg_type == 'mean':
self.aggregator = torch.mean
elif agg_type == 'Linear':
self.aggregator = nn.Linear(in_feats * 4, hidden_feats, bias=False)
nn.init.xavier_uniform_(self.aggregator.weight)
elif agg_type == 'BiTrans':
self.aggregator_drug_disease = nn.Linear(in_feats, hidden_feats, bias=False)
self.aggregator_disease_drug = nn.Linear(in_feats, hidden_feats, bias=False)
self.aggregator_drug = nn.Linear(in_feats, int(hidden_feats / 2), bias=False)
self.aggregator_dis = nn.Linear(in_feats, int(hidden_feats / 2), bias=False)
nn.init.xavier_uniform_(self.aggregator_drug_disease.weight)
nn.init.xavier_uniform_(self.aggregator_disease_drug.weight)
nn.init.xavier_uniform_(self.aggregator_drug.weight)
nn.init.xavier_uniform_(self.aggregator_dis.weight)
def forward(self, feature, mp_ins):
mp_ins_drug = mp_ins[:, :, :2]
mp_ins_dis = mp_ins[:, :, 2:]
mp_ins_feat = torch.cat([feature['drug'][mp_ins_drug],
feature['disease'][mp_ins_dis]], dim=2)
if self.agg_type in ['sum', 'mean']:
ins_emb = self.aggregator(mp_ins_feat, dim=2)
elif self.agg_type == 'Linear':
ins_emb = self.aggregator(mp_ins_feat.reshape(mp_ins_feat.shape[0], mp_ins_feat.shape[1],
mp_ins_feat.shape[2] * mp_ins_feat.shape[3]))
else:
hd_feat = mp_ins_feat.shape[3]
mp_ins_feat = mp_ins_feat.reshape(mp_ins_feat.shape[0], mp_ins_feat.shape[1],
mp_ins_feat.shape[2] * mp_ins_feat.shape[3])
dis_feat = (((self.aggregator_drug_disease((mp_ins_feat[:, :, :hd_feat] +
mp_ins_feat[:, :, hd_feat:hd_feat * 2]) / 2)
+ mp_ins_feat[:, :, hd_feat * 2:hd_feat * 3]) / 2)
+ mp_ins_feat[:, :, hd_feat * 3:]) / 2
drug_feat = (((self.aggregator_disease_drug((mp_ins_feat[:, :, hd_feat * 3:]
+ mp_ins_feat[:, :, hd_feat * 2:hd_feat * 3]) / 2)
+ mp_ins_feat[:, :, hd_feat:hd_feat * 2]) / 2)
+ mp_ins_feat[:, :, :hd_feat]) / 2
ins_emb = torch.cat((self.aggregator_drug(drug_feat),
self.aggregator_dis(dis_feat)), dim=2)
return ins_emb
class MILNet(nn.Module):
"""Attention based instance aggregation block for bag embedding.
"""
def __init__(self, in_feats, hidden_feats):
"""
Parameters
----------
in_feats : int
Input feature size.
hidden_feats : int
Output feature size.
"""
super(MILNet, self).__init__()
self.project = nn.Sequential(nn.Linear(in_feats, hidden_feats),
nn.Tanh(),
nn.Linear(hidden_feats, 1, bias=False))
def forward(self, ins_emb, output_attn=True):
attn = torch.softmax(self.project(ins_emb), dim=1)
bag_emb = (ins_emb * attn).sum(dim=1)
if output_attn:
return bag_emb, attn
else:
return bag_emb
class InstanceNet(nn.Module):
"""Instance predictor.
"""
def __init__(self, in_feats, k=3):
"""
Parameters
----------
in_feats : int
Input feature size.
k : int
A topk filtering used in the aggregation of predictions.
"""
super(InstanceNet, self).__init__()
self.k = k
self.weights = nn.Linear(int(in_feats / 2), int(in_feats / 2), bias=False)
nn.init.xavier_uniform_(self.weights.weight)
def forward(self, ins_emb, attn):
drug_ins = ins_emb[:, :, :int(ins_emb.shape[-1] / 2)]
dis_ins = ins_emb[:, :, int(ins_emb.shape[-1] / 2):]
pred = torch.matmul(self.weights(drug_ins).reshape(drug_ins.shape[0], drug_ins.shape[1],
1, drug_ins.shape[2]),
dis_ins.unsqueeze(dim=-1)).squeeze(dim=3)
attn_pred = attn * pred
topk_out = torch.mean(attn_pred.topk(k=self.k, dim=1)[0], dim=1)
return topk_out
class MLP(nn.Module):
"""Bag predictor.
"""
def __init__(self, in_feats, dropout=0.):
"""
Parameters
----------
in_feats : int
Input feature size.
dropout : int
The dropout rate.
"""
super(MLP, self).__init__()
if dropout:
self.dropout = nn.Dropout(dropout)
else:
self.dropout = False
self.linear = nn.Linear(in_feats, 1, bias=False)
nn.init.xavier_uniform_(self.linear.weight)
def forward(self, bag_emb):
if self.dropout:
bag_emb = self.dropout(bag_emb)
outputs = self.linear(bag_emb)
return outputs