-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh_fre_conv.py
More file actions
42 lines (32 loc) · 1.49 KB
/
high_fre_conv.py
File metadata and controls
42 lines (32 loc) · 1.49 KB
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
import torch
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import add_self_loops, degree
class highConv(MessagePassing):
def __init__(self, in_channels, out_channels, aggr='add'):
super(highConv, self).__init__(aggr='add') # "Add" aggregation.
#self.lin = torch.nn.Linear(in_channels, out_channels)
self.gate = torch.nn.Linear(2*in_channels, 1)
def forward(self, x, edge_index):
# x has shape [N, in_channels]
# edge_index has shape [2, E]
# Step 1: Add self-loops to the adjacency matrix.
#edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))
# Step 2: Linearly transform node feature matrix.
#x = self.lin(x)
# Step 3-5: Start propagating messages.
return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x)
def message(self, x_i, x_j, edge_index, size):
# x_j e.g.[135090, 512]
# Step 3: Normalize node features.
row, col = edge_index
deg = degree(row, size[0], dtype=x_j.dtype)
deg_inv_sqrt = deg.pow(-0.5)
norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
#h2 = x_i - x_j
h2 = torch.cat([x_i, x_j], dim=1)
alpha_g = torch.tanh(self.gate(h2))#e.g.[135090, 1]
return norm.view(-1, 1) * (x_j) *alpha_g
def update(self, aggr_out):
# aggr_out has shape [N, out_channels]
# Step 5: Return new node embeddings.
return aggr_out