-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathgraph_norm.py
More file actions
63 lines (48 loc) · 2.08 KB
/
Copy pathgraph_norm.py
File metadata and controls
63 lines (48 loc) · 2.08 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import Optional
import torch
from torch import Tensor
try:
from torch_scatter import scatter_mean
except ImportError:
from ..compat_scatter import scatter_mean
from torch_geometric.nn.inits import zeros, ones
class GraphNorm(torch.nn.Module):
r"""Applies graph normalization over individual graphs as described in the
`"GraphNorm: A Principled Approach to Accelerating Graph Neural Network
Training" <https://arxiv.org/abs/2009.03294>`_ paper
.. math::
\mathbf{x}^{\prime}_i = \frac{\mathbf{x} - \alpha \odot
\textrm{E}[\mathbf{x}]}
{\sqrt{\textrm{Var}[\mathbf{x} - \alpha \odot \textrm{E}[\mathbf{x}]]
+ \epsilon}} \odot \gamma + \beta
where :math:`\alpha` denotes parameters that learn how much information
to keep in the mean.
Args:
in_channels (int): Size of each input sample.
eps (float, optional): A value added to the denominator for numerical
stability. (default: :obj:`1e-5`)
"""
def __init__(self, in_channels: int, eps: float = 1e-5):
super(GraphNorm, self).__init__()
self.in_channels = in_channels
self.eps = eps
self.weight = torch.nn.Parameter(torch.Tensor(in_channels))
self.bias = torch.nn.Parameter(torch.Tensor(in_channels))
self.mean_scale = torch.nn.Parameter(torch.Tensor(in_channels))
self.reset_parameters()
def reset_parameters(self):
ones(self.weight)
zeros(self.bias)
ones(self.mean_scale)
def forward(self, x: Tensor, batch: Optional[Tensor] = None) -> Tensor:
""""""
if batch is None:
batch = x.new_zeros(x.size(0), dtype=torch.long)
batch_size = int(batch.max()) + 1
mean = scatter_mean(x, batch, dim=0, dim_size=batch_size)[batch]
out = x - mean * self.mean_scale
var = scatter_mean(out.pow(2), batch, dim=0, dim_size=batch_size)
std = (var + self.eps).sqrt()[batch]
return self.weight * out / std + self.bias
def __repr__(self):
return f'{self.__class__.__name__}({self.in_channels})'