forked from koc-lab/scGraPhT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
50 lines (42 loc) · 1.54 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
import math
import torch
from torch.nn.modules.module import Module
from torch.nn.parameter import Parameter
class GraphConvolution(Module):
def __init__(self, in_features, out_features, bias=True):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.FloatTensor(in_features, out_features))
if bias:
self.bias = Parameter(torch.FloatTensor(out_features))
else:
self.register_parameter("bias", None)
self.reset_parameters()
def reset_parameters(self):
stdv = 2.0 / math.sqrt(self.weight.size(1))
# self.weight.data.uniform_(-stdv, stdv)
torch.nn.init.xavier_uniform_(self.weight, gain=torch.nn.init.calculate_gain("linear"))
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
"""
def forward(self, x, adj):
if x.shape[1] == 1:
support = self.weight
else:
support = torch.mm(x, self.weight)
output = torch.mm(adj, support)
if self.bias is not None:
return output + self.bias
else:
return output
"""
def forward(self, x, adj):
support = torch.mm(x, self.weight)
output = torch.mm(adj, support)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return self.__class__.__name__ + " (" + str(self.in_features) + " -> " + str(self.out_features) + ")"