-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.py
162 lines (132 loc) · 5.95 KB
/
Model.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
from torch import nn
import torch.nn.functional as F
import torch
from torch_geometric.nn import GCNConv, GATv2Conv, GATConv
from torch_geometric.nn import global_mean_pool
from transformers import AutoModel
# Graph Encoder from Baseline
class GraphEncoder(nn.Module):
def __init__(self, num_node_features, nout, nhid, graph_hidden_channels):
super(GraphEncoder, self).__init__()
self.nhid = nhid
self.nout = nout
self.relu = nn.ReLU()
self.ln = nn.LayerNorm((nout))
self.conv1 = GCNConv(num_node_features, graph_hidden_channels)
self.conv2 = GCNConv(graph_hidden_channels, graph_hidden_channels)
self.conv3 = GCNConv(graph_hidden_channels, graph_hidden_channels)
self.mol_hidden1 = nn.Linear(graph_hidden_channels, nhid)
self.mol_hidden2 = nn.Linear(nhid, nout)
def forward(self, graph_batch):
x = graph_batch.x
edge_index = graph_batch.edge_index
batch = graph_batch.batch
x = self.conv1(x, edge_index)
x = x.relu()
x = self.conv2(x, edge_index)
x = x.relu()
x = self.conv3(x, edge_index)
x = global_mean_pool(x, batch)
x = self.mol_hidden1(x).relu()
x = self.mol_hidden2(x)
return x
# Text Encoder from Baseline
class TextEncoder(nn.Module):
def __init__(self, model_name):
super(TextEncoder, self).__init__()
self.bert = AutoModel.from_pretrained(model_name)
def forward(self, input_ids, attention_mask):
encoded_text = self.bert(input_ids, attention_mask=attention_mask)
return encoded_text.last_hidden_state[:,0,:]
# Baseline model
class Model_baseline(nn.Module):
def __init__(self, model_name, num_node_features,nout, nhid, graph_hidden_channels):
super(Model_baseline, self).__init__()
self.graph_encoder = GraphEncoder(num_node_features, nout, nhid, graph_hidden_channels)
self.text_encoder = TextEncoder(model_name)
def forward(self, graph_batch, input_ids, attention_mask):
graph_encoded = self.graph_encoder(graph_batch)
text_encoded = self.text_encoder(input_ids, attention_mask)
return graph_encoded, text_encoded
def get_text_encoder(self):
return self.text_encoder
def get_graph_encoder(self):
return self.graph_encoder
# Graph Encoder using GATConv
class GraphEncoderGAT(nn.Module):
def __init__(self, num_node_features, nout, nhid, graph_hidden_channels, dropout=0.2):
super(GraphEncoderGAT, self).__init__()
self.nhid = nhid
self.nout = nout
self.gat1 = GATConv(num_node_features, graph_hidden_channels, heads=10, dropout=dropout)
self.gat2 = GATConv(graph_hidden_channels * 10, graph_hidden_channels, dropout=dropout)
self.mol_hidden1 = nn.Linear(graph_hidden_channels, nhid)
self.mol_hidden2 = nn.Linear(nhid, nout)
def forward(self, graph_batch):
x1 = graph_batch.x
edge_index = graph_batch.edge_index
batch = graph_batch.batch
x1, weight = self.gat1(x1, edge_index,return_attention_weights=True)
x1 = F.elu(x1)
x1 = F.dropout(x1, p=0.2, training=self.training)
x1, weight2 = self.gat2(x1, edge_index, return_attention_weights=True)
x1 = F.elu(x1)
x1 = F.dropout(x1, p=0.2, training=self.training)
x1 = global_mean_pool(x1, batch)
x1 = self.mol_hidden1(x1).relu()
x1 = self.mol_hidden2(x1)
return x1
# Model using GATConv
class ModelGAT(nn.Module):
def __init__(self, model_name, num_node_features, nout, nhid, graph_hidden_channels):
super(ModelGAT, self).__init__()
self.graph_encoder = GraphEncoderGAT(num_node_features, nout, nhid, graph_hidden_channels)
self.text_encoder = TextEncoder(model_name)
def forward(self, graph_batch, input_ids, attention_mask):
graph_encoded = self.graph_encoder(graph_batch)
text_encoded = self.text_encoder(input_ids, attention_mask)
return graph_encoded, text_encoded
def get_text_encoder(self):
return self.text_encoder
def get_graph_encoder(self):
return self.graph_encoder
#Graph Encoder using GATv2Conv
class GraphEncoderGATv2(nn.Module):
def __init__(self, num_node_features, nout, nhid, graph_hidden_channels):
super(GraphEncoderGATv2, self).__init__()
self.nhid = nhid
self.nout = nout
self.relu = nn.ReLU()
self.ln = nn.LayerNorm((nout))
self.conv1 = GATv2Conv(num_node_features, graph_hidden_channels, heads=5)
self.conv2 = GATv2Conv(graph_hidden_channels, graph_hidden_channels, heads=5)
self.conv3 = GATv2Conv(graph_hidden_channels, graph_hidden_channels, heads=5)
self.mol_hidden1 = nn.Linear(graph_hidden_channels, nhid)
self.mol_hidden2 = nn.Linear(nhid, nout)
def forward(self, graph_batch):
x = graph_batch.x
edge_index = graph_batch.edge_index
batch = graph_batch.batch
x = self.conv1(x, edge_index)
x = x.relu()
x = self.conv2(x, edge_index)
x = x.relu()
x = self.conv3(x, edge_index)
x = global_mean_pool(x, batch)
x = self.mol_hidden1(x).relu()
x = self.mol_hidden2(x)
return x
# Model using GATv2Conv
class ModelGATv2(nn.Module):
def __init__(self, model_name, num_node_features, nout, nhid, graph_hidden_channels):
super(ModelGATv2, self).__init__()
self.graph_encoder = GraphEncoderGATv2(num_node_features, nout, nhid, graph_hidden_channels)
self.text_encoder = TextEncoder(model_name)
def forward(self, graph_batch, input_ids, attention_mask):
graph_encoded = self.graph_encoder(graph_batch)
text_encoded = self.text_encoder(input_ids, attention_mask)
return graph_encoded, text_encoded
def get_text_encoder(self):
return self.text_encoder
def get_graph_encoder(self):
return self.graph_encoder