Repository navigation
Expand file tree
/
Copy pathlayers.py
More file actions
148 lines (130 loc) · 5.2 KB
/
Copy pathlayers.py
File metadata and controls
148 lines (130 loc) · 5.2 KB
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
import tensorflow as tf
from utils import *
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 = {}
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 MLP():
"""Multi-layer perceptron (MLP) class."""
def __init__(self, input_dim, hidden_dims, output_dim, name, dropout=0., act=tf.nn.relu):
self.name = name
self.vars = {}
with tf.variable_scope(self.name + '_vars'):
# Define MLP layers based on hidden_dims
self.hidden_layers = []
for i, h_dim in enumerate(hidden_dims):
if i == 0:
input_dim = input_dim
else:
input_dim = hidden_dims[i-1]
self.hidden_layers.append(tf.keras.layers.Dense(h_dim, activation=act, name=f'hidden_layer{i+1}'))
self.output_layer = tf.keras.layers.Dense(output_dim, activation=None, name='output_layer')
self.dropout = dropout
self.act = act
def __call__(self, inputs):
with tf.name_scope(self.name):
x = inputs
for layer in self.hidden_layers:
x = tf.nn.dropout(x, 1 - self.dropout)
x = layer(x)
x = self.output_layer(x)
outputs = self.act(x)
return outputs
class InnerProductDecoder():
"""Decoder model layer for link prediction."""
def __init__(self, input_dim, name, num_r, dropout=0., act=tf.nn.sigmoid):
self.name = name
self.vars = {}
self.issparse = False
self.dropout = dropout
self.act = act
self.num_r = num_r
with tf.variable_scope(self.name + '_vars'):
self.vars['weights'] = weight_variable_glorot(
input_dim, input_dim, name='weights')
def __call__(self, inputs):
with tf.name_scope(self.name):
inputs = tf.nn.dropout(inputs, 1-self.dropout)
R = inputs[0:self.num_r, :]
D = inputs[self.num_r:, :]
R = tf.matmul(R, self.vars['weights'])
D = tf.transpose(D)
x = tf.matmul(R, D)
x = tf.reshape(x, [-1])
outputs = self.act(x)
return outputs
class DotProductDecoder():
"""Decoder model layer for link prediction."""
def __init__(self, input_dim, name, num_r, dropout=0., act=tf.nn.sigmoid):
self.name = name
self.vars = {}
self.issparse = False
self.dropout = dropout
self.act = act
self.num_r = num_r
def __call__(self, inputs):
with tf.name_scope(self.name):
inputs = tf.nn.dropout(inputs, 1 - self.dropout)
R = inputs[0:self.num_r, :]
D = inputs[self.num_r:, :]
R = tf.matmul(R, tf.transpose(D))
x = tf.reshape(R, [-1])
outputs = self.act(x)
return outputs
class BilinearDecoder():
def __init__(self, input_dim, name, num_r, rate=0., act=tf.nn.sigmoid):
self.name = name
self.vars = {}
self.num_r = num_r # 设置 num_r 属性
self.rate = rate
self.act = act
with tf.variable_scope(self.name + '_vars'):
self.vars['weights'] = weight_variable_glorot(input_dim, input_dim, name='weights')
def __call__(self, inputs):
with tf.name_scope(self.name):
inputs = tf.nn.dropout(inputs, rate=self.rate)
R = inputs[:self.num_r, :] # 使用 self.num_r
D = inputs[self.num_r:, :]
R = tf.matmul(R, self.vars['weights'])
D = tf.transpose(D)
x = tf.matmul(R, D)
x = tf.reshape(x, [-1])
outputs = self.act(x)
return outputs