-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers_sub_Mean.py
45 lines (37 loc) · 1.46 KB
/
layers_sub_Mean.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
import math
import torch
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
class GraphConvolution(Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_features, out_features, Graph_obj, bias=True):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.FloatTensor(in_features, out_features))
self.G = Graph_obj
if bias:
self.bias = Parameter(torch.FloatTensor(out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input, adj_matrix, degree_norm):
mapped_fea = torch.mm(input, self.weight)
aggregate_1hop_emb = torch.mm(adj_matrix, mapped_fea)
avg_1hop_emb = aggregate_1hop_emb * degree_norm
new_emb_mat = mapped_fea - avg_1hop_emb
if self.bias is not None:
return new_emb_mat + self.bias
else:
return new_emb_mat
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'