-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
233 lines (185 loc) · 7.67 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
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
from __future__ import division
from __future__ import print_function
import time
import numpy as np
import scipy.sparse as sp
from utils import *
import networkx as nx
import tensorflow.compat.v1 as tf
from sklearn.metrics import roc_auc_score
from sklearn.metrics import average_precision_score
flags = tf.app.flags
FLAGS = flags.FLAGS
class Model(object):
def __init__(self, **kwargs):
allowed_kwargs = {'name', 'logging'}
for kwarg in kwargs.keys():
assert kwarg in allowed_kwargs, 'Invalid keyword argument: ' + kwarg
name = kwargs.get('name')
if not name:
name = self.__class__.__name__.lower()
self.name = name
logging = kwargs.get('logging', False)
self.logging = logging
self.vars = {}
self.placeholders = {}
self.layers = []
self.activations = []
self.inputs = None
self.outputs = None
self.hid = None
self.loss = 0
self.accuracy = 0
self.optimizer = None
self.opt_op = None
def _build(self):
raise NotImplementedError
def build(self):
""" Wrapper for _build() """
with tf.variable_scope(self.name):
self._build()
# activations
self.activations.append(self.inputs)
for layer in self.layers:
hidden = layer(self.activations[-1])
self.activations.append(hidden)
self.outputs = self.activations[-1]
self.hid = self.activations[-2]
# Store model variables for easy access
variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.name)
self.vars = {var.name: var for var in variables}
# Build metrics
self._loss()
self._accuracy()
self.opt_op = self.optimizer.minimize(self.loss)
def predict(self):
pass
def hidd(self):
pass
def _loss(self):
raise NotImplementedError
def _accuracy(self):
raise NotImplementedError
def save(self, sess=None):
if not sess:
raise AttributeError("TensorFlow session not provided.")
saver = tf.train.Saver(self.vars)
save_path = saver.save(sess, "tmp/%s.ckpt" % self.name)
print("Model saved in file: %s" % save_path)
def load(self, sess=None):
if not sess:
raise AttributeError("TensorFlow session not provided.")
saver = tf.train.Saver(self.vars)
save_path = "tmp/%s.ckpt" % self.name
saver.restore(sess, save_path)
print("Model restored from file: %s" % save_path)
class GraphConvolution():
"""Basic graph convolution layer for undirected graph without edge labels."""
def __init__(self, input_dim, output_dim, adj, name, dropout=0., act=tf.nn.relu):
self.name = name
self.vars = {}
self.issparse = False
with tf.variable_scope(self.name + '_vars'):
self.vars['weights'] = weight_variable_glorot(input_dim, output_dim, name='weights')
self.dropout = dropout
self.adj = adj
self.act = act
def __call__(self, inputs):
with tf.name_scope(self.name):
x = inputs
x = tf.nn.dropout(x, 1 - self.dropout)
x = tf.matmul(x, self.vars['weights'])
x = tf.sparse_tensor_dense_matmul(self.adj, x)
outputs = self.act(x)
return outputs
class GraphConvolutionSparse():
"""Graph convolution layer for sparse inputs."""
def __init__(self, input_dim, output_dim, adj, features_nonzero, name, dropout=0., act=tf.nn.relu):
self.name = name
self.vars = {}
self.issparse = False
with tf.variable_scope(self.name + '_vars'):
self.vars['weights'] = weight_variable_glorot(input_dim, output_dim, name='weights')
self.dropout = dropout
self.adj = adj
self.act = act
self.issparse = True
self.features_nonzero = features_nonzero
def __call__(self, inputs):
with tf.name_scope(self.name):
x = inputs
x = dropout_sparse(x, 1 - self.dropout, self.features_nonzero)
x = tf.sparse_tensor_dense_matmul(x, self.vars['weights'])
x = tf.sparse_tensor_dense_matmul(self.adj, x)
outputs = self.act(x)
return outputs
class InnerProductDecoder():
"""Decoder model layer for link prediction."""
def __init__(self, input_dim, name, dropout=0., act=tf.nn.sigmoid):
self.name = name
self.issparse = False
self.dropout = dropout
self.act = act
def __call__(self, inputs):
with tf.name_scope(self.name):
# inputs = tf.nn.dropout(inputs, 1 - self.dropout)
# x = tf.transpose(inputs)
# x = tf.matmul(inputs, x)
# x = tf.reshape(x, [-1])
# outputs = self.act(x)
inputs = tf.nn.dropout(inputs, 1 - self.dropout)
num_drug = 106
# drug_matrix = inputs[0:num_drug,:]
# miRNA_matrix = inputs[num_drug::,:]
drug_matrix = tf.slice(inputs, [0, 0], [num_drug, -1])
miRNA_matrix = tf.slice(inputs, [num_drug, 0], [860 - num_drug, -1])
# x = tf.transpose(inputs)
miRNA_matrix = tf.transpose(miRNA_matrix)
x = tf.matmul(drug_matrix, miRNA_matrix)
x = tf.reshape(x, [-1])
# outputs = self.act(x)
return x
class GCNModel():
def __init__(self, placeholders, num_features, features_nonzero, num_nodes, num_edges, name):
self.name = name
self.placeholders = placeholders
self.inputs = placeholders['features']
self.input_dim = num_features
self.features_nonzero = features_nonzero
self.adj = placeholders['adj_norm']
self.dropout = placeholders['dropout']
self.num_nodes = num_nodes
self.num_edges = num_edges
# self.mask = placeholders['labels_mask']
# self.negative_mask = placeholders['negative_mask']
with tf.variable_scope(self.name):
self.build()
def build(self):
hidden = GraphConvolutionSparse(
name='gcn_sparse_layer',
input_dim=self.input_dim,
output_dim=FLAGS.hidden2,
adj=self.adj,
features_nonzero=self.features_nonzero,
act=tf.nn.relu,
dropout=self.dropout)(self.inputs)
self.embeddings = GraphConvolution(
name='gcn_dense_layer',
input_dim=FLAGS.hidden2,
output_dim=FLAGS.hidden2,
adj=self.adj,
act=lambda x: x,
dropout=self.dropout)(hidden)
self.reconstructions = InnerProductDecoder(
name='gcn_decoder',
input_dim=FLAGS.hidden2,
act=lambda x: x)(self.embeddings)
pos_weight = float(self.num_nodes ** 2 - self.num_edges) / self.num_edges
norm = self.num_nodes ** 2 / float((self.num_nodes ** 2 - self.num_edges) * 2)
pos_weight = 1
self.cost = gcn_masked_softmax_cross_entropy(self.reconstructions, tf.reshape(
tf.sparse_tensor_to_dense(self.placeholders['adj_label'], validate_indices=False),
[-1]), self.placeholders['positive_mask'], self.placeholders['negative_mask'], pos_weight)
self.optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate) # Adam Optimizer
self.opt_op = self.optimizer.minimize(self.cost)
self.grads_vars = self.optimizer.compute_gradients(self.cost)