-
Notifications
You must be signed in to change notification settings - Fork 29
/
le_conv.py
55 lines (42 loc) · 2.1 KB
/
le_conv.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.nn import Parameter
from torch_geometric.utils import remove_self_loops, add_self_loops
from torch_scatter import scatter_add
from torch_geometric.nn.inits import uniform
class LEConv(torch.nn.Module):
r"""Args:
in_channels (int): Size of each input sample.
out_channels (int): Size of each output sample.
bias (bool, optional): If set to :obj:`False`, the layer will not learn
an additive bias. (default: :obj:`True`)
"""
def __init__(self, in_channels, out_channels, bias=True):
super(LEConv, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.lin1 = torch.nn.Linear(in_channels, out_channels, bias=bias)
self.lin2 = torch.nn.Linear(in_channels, out_channels, bias=bias)
self.weight = Parameter(torch.Tensor(in_channels, out_channels))
self.reset_parameters()
def reset_parameters(self):
uniform(self.in_channels, self.weight)
self.lin1.reset_parameters()
self.lin2.reset_parameters()
def forward(self, x, edge_index, edge_weight=None, size=None):
""""""
num_nodes = x.shape[0]
h = torch.matmul(x, self.weight)
if edge_weight is None:
edge_weight = torch.ones((edge_index.size(1), ),
dtype=x.dtype,
device=edge_index.device)
edge_index, edge_weight = remove_self_loops(edge_index=edge_index, edge_attr=edge_weight)
deg = scatter_add(edge_weight, edge_index[0], dim=0, dim_size=num_nodes) #+ 1e-10
h_j = edge_weight.view(-1, 1) * h[edge_index[1]]
aggr_out = scatter_add(h_j, edge_index[0], dim=0, dim_size=num_nodes)
out = ( deg.view(-1, 1) * self.lin1(x) + aggr_out) + self.lin2(x)
edge_index, edge_weight = add_self_loops(edge_index=edge_index, edge_weight=edge_weight, num_nodes=num_nodes)
return out
def __repr__(self):
return '{}({}, {})'.format(self.__class__.__name__, self.in_channels,
self.out_channels)