-
Notifications
You must be signed in to change notification settings - Fork 3
/
layers.py
executable file
·68 lines (54 loc) · 2.1 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
import matplotlib.pyplot as plt
from initializations import *
from preprocessing import normalize_adj_torch
class GSRLayer(nn.Module):
def __init__(self, hr_dim):
super(GSRLayer, self).__init__()
self.weights = torch.from_numpy(
weight_variable_glorot(hr_dim)).type(torch.FloatTensor)
self.weights = torch.nn.Parameter(
data=self.weights, requires_grad=True)
def forward(self, A, X):
with torch.autograd.set_detect_anomaly(True):
lr = A
lr_dim = lr.shape[0]
f = X
eig_val_lr, U_lr = torch.symeig(lr, eigenvectors=True, upper=True)
# U_lr = torch.abs(U_lr)
eye_mat = torch.eye(lr_dim).type(torch.FloatTensor)
s_d = torch.cat((eye_mat, eye_mat), 0)
a = torch.matmul(self.weights, s_d)
b = torch.matmul(a, torch.t(U_lr))
f_d = torch.matmul(b, f)
f_d = torch.abs(f_d)
f_d = f_d.fill_diagonal_(1)
adj = f_d
X = torch.mm(adj, adj.t())
X = (X + X.t())/2
X = X.fill_diagonal_(1)
return adj, torch.abs(X)
class GraphConvolution(nn.Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_features, out_features, dropout, act=F.relu):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.dropout = dropout
self.act = act
self.weight = torch.nn.Parameter(
torch.FloatTensor(in_features, out_features))
self.reset_parameters()
def reset_parameters(self):
torch.nn.init.xavier_uniform_(self.weight)
def forward(self, input, adj):
input = F.dropout(input, self.dropout, self.training)
support = torch.mm(input, self.weight)
output = torch.mm(adj, support)
output = self.act(output)
return output