-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathlayers.py
107 lines (79 loc) · 3.44 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy as np
import torch
import torch.nn as nn
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import add_self_loops
class LightGCNConv(MessagePassing):
def __init__(self, dim):
super(LightGCNConv, self).__init__(aggr='add')
self.dim = dim
def forward(self, x, edge_index, edge_weight):
return self.propagate(edge_index, x=x, edge_weight=edge_weight)
def message(self, x_j, edge_weight):
return edge_weight.view(-1, 1) * x_j
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.dim)
class BipartiteGCNConv(MessagePassing):
def __init__(self, dim):
super(BipartiteGCNConv, self).__init__(aggr='add')
self.dim = dim
def forward(self, x, edge_index, edge_weight, size):
return self.propagate(edge_index, x=x, edge_weight=edge_weight, size=size)
def message(self, x_j, edge_weight):
return edge_weight.view(-1, 1) * x_j
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.dim)
class BiGNNConv(MessagePassing):
r"""Propagate a layer of Bi-interaction GNN
.. math::
output = (L+I)EW_1 + LE \otimes EW_2
"""
def __init__(self, in_channels, out_channels):
super().__init__(aggr='add')
self.in_channels, self.out_channels = in_channels, out_channels
self.lin1 = torch.nn.Linear(in_features=in_channels, out_features=out_channels)
self.lin2 = torch.nn.Linear(in_features=in_channels, out_features=out_channels)
def forward(self, x, edge_index, edge_weight):
x_prop = self.propagate(edge_index, x=x, edge_weight=edge_weight)
x_trans = self.lin1(x_prop + x)
x_inter = self.lin2(torch.mul(x_prop, x))
return x_trans + x_inter
def message(self, x_j, edge_weight):
return edge_weight.view(-1, 1) * x_j
def __repr__(self):
return '{}({},{})'.format(self.__class__.__name__, self.in_channels, self.out_channels)
class SRGNNConv(MessagePassing):
def __init__(self, dim):
# mean aggregation to incorporate weight naturally
super(SRGNNConv, self).__init__(aggr='mean')
self.lin = torch.nn.Linear(dim, dim)
def forward(self, x, edge_index):
x = self.lin(x)
return self.propagate(edge_index, x=x)
class SRGNNCell(nn.Module):
def __init__(self, dim):
super(SRGNNCell, self).__init__()
self.dim = dim
self.incomming_conv = SRGNNConv(dim)
self.outcomming_conv = SRGNNConv(dim)
self.lin_ih = nn.Linear(2 * dim, 3 * dim)
self.lin_hh = nn.Linear(dim, 3 * dim)
self._reset_parameters()
def forward(self, hidden, edge_index):
input_in = self.incomming_conv(hidden, edge_index)
reversed_edge_index = torch.flip(edge_index, dims=[0])
input_out = self.outcomming_conv(hidden, reversed_edge_index)
inputs = torch.cat([input_in, input_out], dim=-1)
gi = self.lin_ih(inputs)
gh = self.lin_hh(hidden)
i_r, i_i, i_n = gi.chunk(3, -1)
h_r, h_i, h_n = gh.chunk(3, -1)
reset_gate = torch.sigmoid(i_r + h_r)
input_gate = torch.sigmoid(i_i + h_i)
new_gate = torch.tanh(i_n + reset_gate * h_n)
hy = (1 - input_gate) * hidden + input_gate * new_gate
return hy
def _reset_parameters(self):
stdv = 1.0 / np.sqrt(self.dim)
for weight in self.parameters():
weight.data.uniform_(-stdv, stdv)