-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel_r.py
143 lines (111 loc) · 5 KB
/
model_r.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from modules import CoAttentionMessagePassingNetwork
class DrugDrugInteractionNetworkR(nn.Module):
def __init__(
self,
n_atom_type, n_bond_type,
d_node, d_edge, d_atom_feat, d_hid,
n_prop_step,
n_side_effect=None,
n_lbls = 12,
n_head=1, dropout=0.1,
update_method='res', score_fn='trans'):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.atom_proj = nn.Linear(d_node + d_atom_feat, d_node)
self.atom_emb = nn.Embedding(n_atom_type, d_node, padding_idx=0)
self.bond_emb = nn.Embedding(n_bond_type, d_edge, padding_idx=0)
nn.init.xavier_normal_(self.atom_emb.weight)
nn.init.xavier_normal_(self.bond_emb.weight)
self.side_effect_emb = None
self.se_head_proj_w = None
self.se_tail_proj_w = None
if n_side_effect is not None:
self.side_effect_emb = nn.Embedding(n_side_effect, d_hid)
nn.init.xavier_normal_(self.side_effect_emb.weight)
self.se_head_proj_w = nn.Parameter(torch.FloatTensor(n_side_effect, d_hid, d_hid))
self.se_tail_proj_w = nn.Parameter(torch.FloatTensor(n_side_effect, d_hid, d_hid))
nn.init.xavier_normal_(self.se_head_proj_w)
nn.init.xavier_normal_(self.se_tail_proj_w)
self.encoder = CoAttentionMessagePassingNetwork(
d_hid=d_hid, n_head=n_head, n_prop_step=n_prop_step,
update_method=update_method, dropout=dropout)
if score_fn == 'trans':
self.head_proj = nn.Linear(d_hid, d_hid, bias=False)
self.tail_proj = nn.Linear(d_hid, d_hid, bias=False)
nn.init.xavier_normal_(self.head_proj.weight)
nn.init.xavier_normal_(self.tail_proj.weight)
self.scoring_fn = self.cal_sym_translation_score
elif score_fn == 'factor':
self.eye = nn.Parameter(torch.eye(d_hid, requires_grad=False).unsqueeze(0))
self.global_rel = nn.Linear(d_hid, d_hid, bias=False)
nn.init.xavier_normal_(self.global_rel.weight)
self.scoring_fn = self.cal_factorize_score
else:
raise NotImplementedError
self.lbl_predict = nn.Linear(d_hid, n_lbls)
self.__score_fn = score_fn
@property
def score_fn(self):
return self.__score_fn
def get_diagonal_matrix(self, vec):
sz_b, d_vec = vec.size()
return vec.view(sz_b, d_vec, 1) * self.eye
def forward(
self,
seg_m1, atom_type1, atom_feat1, bond_type1,
inn_seg_i1, inn_idx_j1, out_seg_i1, out_idx_j1,
seg_m2, atom_type2, atom_feat2, bond_type2,
inn_seg_i2, inn_idx_j2, out_seg_i2, out_idx_j2,
se_idx=None, drug_se_seg=None):
atom1 = self.dropout(self.atom_comp(atom_feat1, atom_type1))
atom2 = self.dropout(self.atom_comp(atom_feat2, atom_type2))
bond1 = self.dropout(self.bond_emb(bond_type1))
bond2 = self.dropout(self.bond_emb(bond_type2))
d1_vec, d2_vec, attn1, attn2 = self.encoder(
seg_m1, atom1, bond1, inn_seg_i1, inn_idx_j1, out_seg_i1, out_idx_j1,
seg_m2, atom2, bond2, inn_seg_i2, inn_idx_j2, out_seg_i2, out_idx_j2)
if self.side_effect_emb is not None:
se_vec = self.dropout(self.side_effect_emb(se_idx))
se_head_proj = self.dropout(self.se_head_proj_w.index_select(0, se_idx))
se_tail_proj = self.dropout(self.se_tail_proj_w.index_select(0, se_idx))
hvecs0 = [se_vec, d1_vec, d2_vec]
d1_vec = d1_vec.index_select(0, drug_se_seg)
d2_vec = d2_vec.index_select(0, drug_se_seg)
#score, hvecs = self.scoring_fn(d1_vec, d2_vec, se_vec, se_head_proj, se_tail_proj)
fwd_score, hvecs1 = self.cal_translation_score(
head=d1_vec, tail=d2_vec, rel=se_vec,
head_proj=se_head_proj, tail_proj=se_tail_proj)
bwd_score, hvecs2 = self.cal_translation_score(
head=d2_vec, tail=d1_vec, rel=se_vec,
head_proj=se_head_proj, tail_proj=se_tail_proj)
score = fwd_score + bwd_score
norm_loss = sum([self.cal_vec_norm_loss(v) for v in hvecs0 + hvecs1 + hvecs2])
return score, norm_loss
else:
pred1 = self.lbl_predict(d1_vec)
pred2 = self.lbl_predict(d2_vec)
return pred1, pred2, attn1, attn2
def atom_comp(self, atom_feat, atom_idx):
atom_emb = self.atom_emb(atom_idx)
node = self.atom_proj(torch.cat([atom_emb, atom_feat], -1))
return node
def cal_vec_norm_loss(self, vec, dim=1):
norm = torch.norm(vec, dim=dim)
return torch.mean(F.relu(norm - 1))
def cal_translation_score(self, head, tail, rel, head_proj, tail_proj):
proj_head = torch.bmm(head_proj, head.unsqueeze(-1)).view_as(head)
proj_tail = torch.bmm(tail_proj, tail.unsqueeze(-1)).view_as(tail)
diff = torch.norm(proj_head + rel - proj_tail, dim=1)
return diff, [proj_head, proj_tail]
def cal_sym_translation_score(self, d1_vec, d2_vec, se_vec, head_proj, tail_proj):
h_d1_vec = torch.bmm(head_proj, d1_vec.unsqueeze(-1)).view_as(d1_vec)
t_d1_vec = torch.bmm(tail_proj, d1_vec.unsqueeze(-1)).view_as(d1_vec)
h_d2_vec = torch.bmm(head_proj, d2_vec.unsqueeze(-1)).view_as(d2_vec)
t_d2_vec = torch.bmm(tail_proj, d2_vec.unsqueeze(-1)).view_as(d2_vec)
forw_score = torch.norm(h_d1_vec + se_vec - t_d2_vec, dim=1)
back_score = torch.norm(h_d2_vec + se_vec - t_d1_vec, dim=1)
translation_score = forw_score + back_score
return translation_score, [h_d1_vec, t_d1_vec, h_d2_vec, t_d2_vec]