-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathlayer.py
56 lines (41 loc) · 1.55 KB
/
layer.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
import torch
from torch import nn
from torch.nn import functional as F
from utils import sparse_dropout, dot
class GraphConvolution(nn.Module):
def __init__(self, input_dim, output_dim, num_features_nonzero,
dropout=0.,
is_sparse_inputs=False,
bias=False,
activation = F.relu,
featureless=False):
super(GraphConvolution, self).__init__()
self.dropout = dropout
self.bias = bias
self.activation = activation
self.is_sparse_inputs = is_sparse_inputs
self.featureless = featureless
self.num_features_nonzero = num_features_nonzero
self.weight = nn.Parameter(torch.randn(input_dim, output_dim))
self.bias = None
if bias:
self.bias = nn.Parameter(torch.zeros(output_dim))
def forward(self, inputs):
# print('inputs:', inputs)
x, support = inputs
if self.training and self.is_sparse_inputs:
x = sparse_dropout(x, self.dropout, self.num_features_nonzero)
elif self.training:
x = F.dropout(x, self.dropout)
# convolve
if not self.featureless: # if it has features x
if self.is_sparse_inputs:
xw = torch.sparse.mm(x, self.weight)
else:
xw = torch.mm(x, self.weight)
else:
xw = self.weight
out = torch.sparse.mm(support, xw)
if self.bias is not None:
out += self.bias
return self.activation(out), support