-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
145 lines (118 loc) · 5.44 KB
/
layers.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
import tensorflow as tf
def dropout_sparse(x, keep_prob, num_nonzero_elems):
"""Dropout for sparse tensors. Currently fails for very large sparse tensors (>1M elements)
"""
noise_shape = [num_nonzero_elems]
random_tensor = keep_prob
random_tensor += tf.random.uniform(noise_shape)
dropout_mask = tf.cast(tf.math.floor(random_tensor), dtype=tf.bool)
# print(x)
pre_out = tf.sparse.retain(x, dropout_mask)
return pre_out * (1./keep_prob)
class GraphConvolution(tf.keras.layers.Layer):
"""Basic graph convolution layer for undirected graph without edge labels."""
def __init__(self, input_dim, output_dim, dropout=0., act=tf.nn.relu, **kwargs):
super(GraphConvolution, self).__init__(**kwargs)
self.w = self.add_weight(
shape=(input_dim, output_dim),
initializer="random_normal",
trainable=True,
)
self.output_dim = output_dim
self.dropout = dropout
self.act = act
def call(self, inputs):
x = inputs[0]
adj = inputs[1]
x = tf.keras.layers.Dropout(1-self.dropout)(x)
x = tf.matmul(x, self.w)
x = tf.sparse.sparse_dense_matmul(adj, x)
# x = tf.matmul(self.adj, x)
outputs = self.act(x)
return outputs
class GraphConvolutionSparse(tf.keras.layers.Layer):
"""Graph convolution layer for sparse inputs."""
def __init__(self, input_dim, output_dim, features_nonzero, dropout=0., act=tf.nn.relu, **kwargs):
super(GraphConvolutionSparse, self).__init__(**kwargs)
self.w = self.add_weight(
shape=(input_dim, output_dim),
initializer="random_normal",
trainable=True,
)
self.output_dim = output_dim
self.dropout = dropout
self.act = act
self.issparse = True
self.features_nonzero = features_nonzero
def call(self, inputs):
x = inputs[0]
adj = inputs[1]
x = dropout_sparse(x, 1-self.dropout, self.features_nonzero)
x = tf.sparse.sparse_dense_matmul(x, self.w)
x = tf.sparse.sparse_dense_matmul(adj, x)
outputs = self.act(x)
return outputs
class EncoderAE(tf.keras.layers.Layer):
"""Maps MNIST digits to a triplet (z_mean, z_log_var, z)."""
def __init__(self, num_features, features_nonzero, dropout=0.0, **kwargs):
super(EncoderAE, self).__init__(**kwargs)
self.input_dim = num_features
self.features_nonzero = features_nonzero
self.dropout = dropout
self.hidden1_dim = 32
self.hidden2_dim = 16
self.hidden1 = GraphConvolutionSparse(input_dim=self.input_dim,
output_dim=self.hidden1_dim,
features_nonzero=self.features_nonzero,
act=tf.nn.relu,
dropout=self.dropout)
self.embeddings = GraphConvolution(input_dim=self.hidden1_dim,
output_dim=self.hidden2_dim,
act=lambda x: x,
dropout=self.dropout)
def call(self, inputs):
x = self.hidden1(inputs)
z_mean = self.embeddings([x, inputs[1]])
return z_mean
class EncoderVAE(tf.keras.layers.Layer):
"""Maps MNIST digits to a triplet (z_mean, z_log_var, z)."""
def __init__(self, num_features, num_nodes, features_nonzero, dropout=0.0, **kwargs):
super(EncoderVAE, self).__init__(**kwargs)
self.input_dim = num_features
self.features_nonzero = features_nonzero
self.n_samples = num_nodes
self.dropout = dropout
self.hidden1_dim = 32
self.hidden2_dim = 16
self.hidden1 = GraphConvolutionSparse(input_dim=self.input_dim,
output_dim=self.hidden1_dim,
features_nonzero=self.features_nonzero,
act=tf.nn.relu,
dropout=self.dropout)
self.z_mean = GraphConvolution(input_dim=self.hidden1_dim,
output_dim=self.hidden2_dim,
act=lambda x: x,
dropout=self.dropout)
self.z_log_std = GraphConvolution(input_dim=self.hidden1_dim,
output_dim=self.hidden2_dim,
act=lambda x: x,
dropout=self.dropout)
def call(self, inputs):
x = self.hidden1(inputs)
z_mean = self.z_mean([x, inputs[1]])
z_log_std = self.z_log_std([x, inputs[1]])
self.z = z_mean + tf.random.normal([self.n_samples, self.hidden2_dim]) * tf.math.exp(z_log_std)
return self.z
class InnerProductDecoder(tf.keras.layers.Layer):
"""Decoder model layer for link prediction."""
def __init__(self, dropout=0., act=tf.nn.sigmoid, **kwargs):
super(InnerProductDecoder, self).__init__(**kwargs)
self.dropout = dropout
self.act = act
def call(self, inputs):
inputs = tf.keras.layers.Dropout(1-self.dropout)(inputs)
x = tf.transpose(inputs)
x = tf.matmul(inputs, x)
x = tf.reshape(x, [-1])
outputs = self.act(x)
return outputs