-
Notifications
You must be signed in to change notification settings - Fork 12
/
models.py
171 lines (145 loc) · 6.69 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
import torch.nn as nn
import torch.nn.functional as F
import torch
import math
from dgllife.model.gnn import GCN
from ban import BANLayer
from torch.nn.utils.weight_norm import weight_norm
def binary_cross_entropy(pred_output, labels):
loss_fct = torch.nn.BCELoss()
m = nn.Sigmoid()
n = torch.squeeze(m(pred_output), 1)
loss = loss_fct(n, labels)
return n, loss
def cross_entropy_logits(linear_output, label, weights=None):
class_output = F.log_softmax(linear_output, dim=1)
n = F.softmax(linear_output, dim=1)[:, 1]
max_class = class_output.max(1)
y_hat = max_class[1] # get the index of the max log-probability
if weights is None:
loss = nn.NLLLoss()(class_output, label.type_as(y_hat).view(label.size(0)))
else:
losses = nn.NLLLoss(reduction="none")(class_output, label.type_as(y_hat).view(label.size(0)))
loss = torch.sum(weights * losses) / torch.sum(weights)
return n, loss
def entropy_logits(linear_output):
p = F.softmax(linear_output, dim=1)
loss_ent = -torch.sum(p * (torch.log(p + 1e-5)), dim=1)
return loss_ent
class DrugBAN(nn.Module):
def __init__(self, **config):
super(DrugBAN, self).__init__()
drug_in_feats = config["DRUG"]["NODE_IN_FEATS"]
drug_embedding = config["DRUG"]["NODE_IN_EMBEDDING"]
drug_hidden_feats = config["DRUG"]["HIDDEN_LAYERS"]
protein_emb_dim = config["PROTEIN"]["EMBEDDING_DIM"]
num_filters = config["PROTEIN"]["NUM_FILTERS"]
kernel_size = config["PROTEIN"]["KERNEL_SIZE"]
mlp_in_dim = config["DECODER"]["IN_DIM"]
mlp_hidden_dim = config["DECODER"]["HIDDEN_DIM"]
mlp_out_dim = config["DECODER"]["OUT_DIM"]
drug_padding = config["DRUG"]["PADDING"]
protein_padding = config["PROTEIN"]["PADDING"]
out_binary = config["DECODER"]["BINARY"]
ban_heads = config["BCN"]["HEADS"]
self.drug_extractor = MolecularGCN(in_feats=drug_in_feats, dim_embedding=drug_embedding,
padding=drug_padding,
hidden_feats=drug_hidden_feats)
self.protein_extractor = ProteinCNN(protein_emb_dim, num_filters, kernel_size, protein_padding)
self.bcn = weight_norm(
BANLayer(v_dim=drug_hidden_feats[-1], q_dim=num_filters[-1], h_dim=mlp_in_dim, h_out=ban_heads),
name='h_mat', dim=None)
self.mlp_classifier = MLPDecoder(mlp_in_dim, mlp_hidden_dim, mlp_out_dim, binary=out_binary)
def forward(self, bg_d, v_p, mode="train"):
v_d = self.drug_extractor(bg_d)
v_p = self.protein_extractor(v_p)
f, att = self.bcn(v_d, v_p)
score = self.mlp_classifier(f)
if mode == "train":
return v_d, v_p, f, score
elif mode == "eval":
return v_d, v_p, score, att
class MolecularGCN(nn.Module):
def __init__(self, in_feats, dim_embedding=128, padding=True, hidden_feats=None, activation=None):
super(MolecularGCN, self).__init__()
self.init_transform = nn.Linear(in_feats, dim_embedding, bias=False)
if padding:
with torch.no_grad():
self.init_transform.weight[-1].fill_(0)
self.gnn = GCN(in_feats=dim_embedding, hidden_feats=hidden_feats, activation=activation)
self.output_feats = hidden_feats[-1]
def forward(self, batch_graph):
node_feats = batch_graph.ndata.pop('h')
node_feats = self.init_transform(node_feats)
node_feats = self.gnn(batch_graph, node_feats)
batch_size = batch_graph.batch_size
node_feats = node_feats.view(batch_size, -1, self.output_feats)
return node_feats
class ProteinCNN(nn.Module):
def __init__(self, embedding_dim, num_filters, kernel_size, padding=True):
super(ProteinCNN, self).__init__()
if padding:
self.embedding = nn.Embedding(26, embedding_dim, padding_idx=0)
else:
self.embedding = nn.Embedding(26, embedding_dim)
in_ch = [embedding_dim] + num_filters
self.in_ch = in_ch[-1]
kernels = kernel_size
self.conv1 = nn.Conv1d(in_channels=in_ch[0], out_channels=in_ch[1], kernel_size=kernels[0])
self.bn1 = nn.BatchNorm1d(in_ch[1])
self.conv2 = nn.Conv1d(in_channels=in_ch[1], out_channels=in_ch[2], kernel_size=kernels[1])
self.bn2 = nn.BatchNorm1d(in_ch[2])
self.conv3 = nn.Conv1d(in_channels=in_ch[2], out_channels=in_ch[3], kernel_size=kernels[2])
self.bn3 = nn.BatchNorm1d(in_ch[3])
def forward(self, v):
v = self.embedding(v.long())
v = v.transpose(2, 1)
v = self.bn1(F.relu(self.conv1(v)))
v = self.bn2(F.relu(self.conv2(v)))
v = self.bn3(F.relu(self.conv3(v)))
v = v.view(v.size(0), v.size(2), -1)
return v
class MLPDecoder(nn.Module):
def __init__(self, in_dim, hidden_dim, out_dim, binary=1):
super(MLPDecoder, self).__init__()
self.fc1 = nn.Linear(in_dim, hidden_dim)
self.bn1 = nn.BatchNorm1d(hidden_dim)
self.fc2 = nn.Linear(hidden_dim, hidden_dim)
self.bn2 = nn.BatchNorm1d(hidden_dim)
self.fc3 = nn.Linear(hidden_dim, out_dim)
self.bn3 = nn.BatchNorm1d(out_dim)
self.fc4 = nn.Linear(out_dim, binary)
def forward(self, x):
x = self.bn1(F.relu(self.fc1(x)))
x = self.bn2(F.relu(self.fc2(x)))
x = self.bn3(F.relu(self.fc3(x)))
x = self.fc4(x)
return x
class SimpleClassifier(nn.Module):
def __init__(self, in_dim, hid_dim, out_dim, dropout):
super(SimpleClassifier, self).__init__()
layers = [
weight_norm(nn.Linear(in_dim, hid_dim), dim=None),
nn.ReLU(),
nn.Dropout(dropout, inplace=True),
weight_norm(nn.Linear(hid_dim, out_dim), dim=None)
]
self.main = nn.Sequential(*layers)
def forward(self, x):
logits = self.main(x)
return logits
class RandomLayer(nn.Module):
def __init__(self, input_dim_list, output_dim=256):
super(RandomLayer, self).__init__()
self.input_num = len(input_dim_list)
self.output_dim = output_dim
self.random_matrix = [torch.randn(input_dim_list[i], output_dim) for i in range(self.input_num)]
def forward(self, input_list):
return_list = [torch.mm(input_list[i], self.random_matrix[i]) for i in range(self.input_num)]
return_tensor = return_list[0] / math.pow(float(self.output_dim), 1.0 / len(return_list))
for single in return_list[1:]:
return_tensor = torch.mul(return_tensor, single)
return return_tensor
def cuda(self):
super(RandomLayer, self).cuda()
self.random_matrix = [val.cuda() for val in self.random_matrix]