-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kyzhouhzau
committed
May 28, 2020
1 parent
95aa331
commit 807ba46
Showing
9 changed files
with
333 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#! usr/bin/env python3 | ||
# -*- coding:utf-8 -*- | ||
""" | ||
@Author:Kaiyin Zhou | ||
""" | ||
|
||
import tensorflow as tf | ||
|
||
from nlpgnn.gnn.messagepassing import MessagePassing | ||
from nlpgnn.gnn.utils import GNNInput, masksoftmax | ||
|
||
|
||
class GraphAttentionAutoEncoder(MessagePassing): | ||
def __init__(self, | ||
out_features, | ||
heads=1, | ||
dropout_rate=0., | ||
use_bias=False, | ||
kernel_initializer='glorot_uniform', | ||
bias_initializer='zeros', | ||
regularizer=5e-4, | ||
concat=True, | ||
**kwargs): | ||
super(GraphAttentionAutoEncoder, self).__init__(aggr="sum", **kwargs) | ||
self.use_bias = use_bias | ||
self.out_features = out_features | ||
self.heads = heads | ||
self.dropout_rate = dropout_rate | ||
self.kernel_initializer = kernel_initializer | ||
self.bias_initializer = bias_initializer | ||
self.regularizer = regularizer | ||
self.concat = concat | ||
|
||
def build(self, input_shapes): | ||
node_embedding_shapes = input_shapes.node_embeddings | ||
# adjacency_list_shapes = input_shapes.adjacency_lists | ||
in_features = node_embedding_shapes[-1] | ||
|
||
self.att = self.add_weight( | ||
shape=(1, self.heads, 2 * self.out_features), | ||
initializer=self.kernel_initializer, | ||
name='att', | ||
) | ||
|
||
if self.use_bias and self.concat: | ||
self.bias = self.add_weight( | ||
shape=(self.heads * self.out_features,), | ||
initializer=self.bias_initializer, | ||
name='b', | ||
) | ||
elif self.use_bias and not self.concat: | ||
self.bias = self.add_weight( | ||
shape=(self.out_features,), | ||
initializer=self.bias_initializer, | ||
name='b', | ||
) | ||
|
||
self.drop1 = tf.keras.layers.Dropout(self.dropout_rate) | ||
self.drop2 = tf.keras.layers.Dropout(self.dropout_rate) | ||
self.built = True | ||
|
||
def message_function(self, edge_source_states, edge_source, # x_j source | ||
edge_target_states, edge_target, # x_i target | ||
num_incoming_to_node_per_message, # degree target | ||
num_outing_to_node_per_message, # degree source | ||
edge_type_idx, training): | ||
""" | ||
:param edge_source_states: [M,H] | ||
:param edge_target_states: [M,H] | ||
:param num_incoming_to_node_per_message:[M] | ||
:param edge_type_idx: | ||
:param training: | ||
:return: | ||
""" | ||
# 计算注意力系数 | ||
alpha = tf.concat([edge_target_states, edge_source_states], -1) * self.att #[M,heads,2D] | ||
alpha = tf.reduce_sum(alpha, -1) # [M,Head] | ||
alpha = tf.math.sigmoid(alpha) | ||
alpha = masksoftmax(alpha, edge_target) | ||
# alpha = self.drop1(alpha, training=training) | ||
# edge_source_states = self.drop2(edge_source_states, training=training) | ||
# messages = tf.math.sigmoid(edge_source_states) * tf.reshape(alpha, [-1, self.heads, 1]) | ||
messages = edge_source_states * tf.reshape(alpha, [-1, self.heads, 1]) | ||
return messages | ||
|
||
def call(self, inputs, weight, transpose_b, training): | ||
adjacency_lists = inputs.adjacency_lists | ||
node_embeddings = inputs.node_embeddings | ||
node_embeddings = tf.linalg.matmul(node_embeddings, weight, transpose_b=transpose_b) | ||
|
||
node_embeddings = tf.reshape(node_embeddings, [node_embeddings.shape[0], self.heads, -1]) | ||
aggr_out = self.propagate(GNNInput(node_embeddings, adjacency_lists), training) | ||
if self.concat is True: | ||
aggr_out = tf.reshape(aggr_out, [-1, self.heads * self.out_features]) | ||
else: | ||
aggr_out = tf.reduce_mean(aggr_out, 1) | ||
if self.use_bias: | ||
aggr_out += self.bias | ||
return aggr_out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#! encoding="utf-8" | ||
import tensorflow as tf | ||
|
||
from nlpgnn.gnn.GAAEConv import GraphAttentionAutoEncoder | ||
from nlpgnn.gnn.utils import GNNInput | ||
|
||
|
||
class GAAELayer(tf.keras.layers.Layer): | ||
def __init__(self, hidden_dim=16, num_layers=2, heads=1, **kwargs): | ||
super(GAAELayer, self).__init__(**kwargs) | ||
self.hidden_dim = hidden_dim | ||
self.num_layers = num_layers | ||
self.heads = heads | ||
|
||
def build(self, input_shape): | ||
input_dim = input_shape[-1] | ||
|
||
self.weight = [] | ||
self.weight.append(self.add_weight( | ||
shape=(input_dim, self.heads * self.hidden_dim), | ||
name='wt', | ||
)) | ||
for i in range(self.num_layers - 1): | ||
self.weight.append(self.add_weight( | ||
shape=(self.hidden_dim, self.heads * self.hidden_dim), | ||
name='wt', | ||
)) | ||
self.encoder_layers = [] | ||
self.decoder_layers = [] | ||
for layer in range(self.num_layers - 1): | ||
self.encoder_layers.append(GraphAttentionAutoEncoder(self.hidden_dim, heads=self.heads)) | ||
self.decoder_layers.append(GraphAttentionAutoEncoder(self.hidden_dim, heads=self.heads)) | ||
self.encoder_layers.append(GraphAttentionAutoEncoder(self.hidden_dim, heads=self.heads)) | ||
self.decoder_layers.append(GraphAttentionAutoEncoder(input_dim, heads=self.heads)) | ||
|
||
def encoder(self, node_embeddings, adjacency_lists, training): | ||
for layer in range(self.num_layers): | ||
node_embeddings = self.encoder_layers[layer](GNNInput(node_embeddings, adjacency_lists), self.weight[layer], | ||
False, training) | ||
return node_embeddings | ||
|
||
def decoder(self, hidden_embeddings, adjacency_lists, training): | ||
for layer in range(self.num_layers): | ||
hidden_embeddings = self.decoder_layers[layer](GNNInput(hidden_embeddings, adjacency_lists), | ||
self.weight[-(layer+1)], | ||
True, | ||
training) | ||
return hidden_embeddings | ||
|
||
def call(self, node_embeddings, adjacency_lists, training=True): | ||
hidden_embeddings = self.encoder(node_embeddings, adjacency_lists, training) | ||
reconstruct_embedding = self.decoder(hidden_embeddings, adjacency_lists, training) | ||
return hidden_embeddings, reconstruct_embedding | ||
|
||
def predict(self, node_embeddings, adjacency_lists, training=False): | ||
return self(node_embeddings, adjacency_lists, training) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
EMAIL = '[email protected]' | ||
AUTHOR = 'Kaiyin Zhou' | ||
REQUIRES_PYTHON = '>=3.6.0' | ||
VERSION = '0.0.7' | ||
VERSION = '0.0.0' | ||
|
||
REQUIRED = [ | ||
'typeguard', | ||
|
Oops, something went wrong.