Normalization Layers
The k3_node.layers.norm module implements 11 specialized normalization layers for graph neural networks, avoiding oversmoothing and gradient instability.
Graph Normalizations
GraphNorm
k3_node.layers.norm.GraphNorm
Bases: Layer
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Size of each input sample. |
required |
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
GraphSizeNorm
k3_node.layers.norm.GraphSizeNorm
Bases: Layer
Applies Graph Size Normalization over each individual graph in a batch of node features:
.. math:: \mathbf{x}^{\prime}_i = \frac{\mathbf{x}_i}{\sqrt{|\mathcal{V}|}}
PairNorm
k3_node.layers.norm.PairNorm
Bases: Layer
Applies pair normalization over node features as described in the
"PairNorm: Tackling Oversmoothing in GNNs"
<https://arxiv.org/abs/1909.12223>_ paper.
.. math:: \mathbf{x}i^c &= \mathbf{x}_i - \frac{1}{n} \sum_i \}^n \mathbf{x
\mathbf{x}_i^{\prime} &= s \cdot
\frac{\mathbf{x}_i^c}{\sqrt{\frac{1}{n} \sum_{i=1}^n
{\| \mathbf{x}_i^c \|}^2_2}}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale
|
float
|
Scaling factor :math: |
1.0
|
scale_individually
|
bool
|
If set to :obj: |
False
|
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
DiffGroupNorm
k3_node.layers.norm.DiffGroupNorm
Bases: Layer
The differentiable group normalization layer from the "Towards Deeper
Graph Neural Networks with Differentiable Group Normalization"
<https://arxiv.org/abs/2006.06972>_ paper, which normalizes node features
group-wise via a learnable soft cluster assignment.
.. math::
\mathbf{S} = \text{softmax} (\mathbf{X} \mathbf{W})
where :math:\mathbf{W} \in \mathbb{R}^{F \times G} denotes a trainable
weight matrix mapping each node into one of :math:G clusters.
Normalization is then performed group-wise via:
.. math::
\mathbf{X}^{\prime} = \mathbf{X} + \lambda \sum_{i = 1}^G
\text{BatchNorm}(\mathbf{S}[:, i] \odot \mathbf{X})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Size of each input sample :math: |
required |
groups
|
int
|
The number of groups :math: |
required |
lamda
|
float
|
The balancing factor :math: |
0.01
|
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
momentum
|
float
|
The value used for the running mean and
running variance computation. (default: :obj: |
0.1
|
affine
|
bool
|
If set to :obj: |
True
|
track_running_stats
|
bool
|
If set to :obj: |
True
|
group_distance_ratio(x, y, eps=1e-05)
staticmethod
Measures the ratio of inter-group distance over intra-group distance.
MessageNorm
k3_node.layers.norm.MessageNorm
Bases: Layer
Applies message normalization over the aggregated messages as described
in the "DeeperGCNs: All You Need to Train Deeper GCNs"
<https://arxiv.org/abs/2006.07739>_ paper.
.. math::
\mathbf{x}_i^{\prime} = \mathbf{x}_{i} + s \cdot
{\| \mathbf{x}_i \|}_2 \cdot
\frac{\mathbf{m}_{i}}{{\|\mathbf{m}_i\|}_2}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learn_scale
|
bool
|
If set to :obj: |
False
|
MeanSubtractionNorm
k3_node.layers.norm.MeanSubtractionNorm
Bases: Layer
Applies layer normalization by subtracting the mean from the inputs
as described in the "Revisiting 'Over-smoothing' in Deep GCNs"
<https://arxiv.org/abs/2003.13663>_ paper.
.. math:: \mathbf{x}i = \mathbf{x}_i - \frac{1}{|\mathcal{V}|} \sum_j}} \mathbf{x
Standard & Hetero Normalizations
BatchNorm
k3_node.layers.norm.BatchNorm
Bases: Layer
Applies batch normalization over a batch of features as described in
the "Batch Normalization: Accelerating Deep Network Training by
Reducing Internal Covariate Shift" <https://arxiv.org/abs/1502.03167>_
paper.
.. math:: \mathbf{x}^{\prime}_i = \frac{\mathbf{x} - \textrm{E}[\mathbf{x}]}{\sqrt{\textrm{Var}[\mathbf{x}] + \epsilon}} \odot \gamma + \beta
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Size of each input sample. |
required |
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
momentum
|
float
|
The value used for the running mean and
running variance computation. (default: :obj: |
0.1
|
affine
|
bool
|
If set to :obj: |
True
|
track_running_stats
|
bool
|
If set to :obj: |
True
|
allow_single_element
|
bool
|
If set to :obj: |
False
|
InstanceNorm
k3_node.layers.norm.InstanceNorm
Bases: Layer
Applies instance normalization over each individual example in a batch
of node features as described in the "Instance Normalization: The Missing
Ingredient for Fast Stylization" <https://arxiv.org/abs/1607.06450>_
paper.
.. math:: \mathbf{x}^{\prime}_i = \frac{\mathbf{x} - \textrm{E}[\mathbf{x}]}{\sqrt{\textrm{Var}[\mathbf{x}] + \epsilon}} \odot \gamma + \beta
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Size of each input sample. |
required |
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
momentum
|
float
|
The value used for the running mean and
running variance computation. (default: :obj: |
0.1
|
affine
|
bool
|
If set to :obj: |
False
|
track_running_stats
|
bool
|
If set to :obj: |
False
|
LayerNorm
k3_node.layers.norm.LayerNorm
Bases: Layer
Applies layer normalization over each individual example in a batch
of features as described in the "Layer Normalization"
<https://arxiv.org/abs/1607.06450>_ paper.
.. math:: \mathbf{x}^{\prime}_i = \frac{\mathbf{x} - \textrm{E}[\mathbf{x}]}{\sqrt{\textrm{Var}[\mathbf{x}] + \epsilon}} \odot \gamma + \beta
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Size of each input sample. |
required |
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
affine
|
bool
|
If set to :obj: |
True
|
mode
|
str
|
The normalization mode to use for layer
normalization (:obj: |
'graph'
|
HeteroBatchNorm
k3_node.layers.norm.HeteroBatchNorm
Bases: Layer
Applies batch normalization over a batch of heterogeneous features as
described in the "Batch Normalization: Accelerating Deep Network Training
by Reducing Internal Covariate Shift" <https://arxiv.org/abs/1502.03167>_
paper.
Compared to :class:BatchNorm, :class:HeteroBatchNorm applies
normalization individually for each node or edge type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Size of each input sample. |
required |
num_types
|
int
|
The number of types. |
required |
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
momentum
|
float
|
The value used for the running mean and
running variance computation. (default: :obj: |
0.1
|
affine
|
bool
|
If set to :obj: |
True
|
track_running_stats
|
bool
|
If set to :obj: |
True
|
HeteroLayerNorm
k3_node.layers.norm.HeteroLayerNorm
Bases: Layer
Applies layer normalization over each individual example in a batch
of heterogeneous features as described in the "Layer Normalization"
<https://arxiv.org/abs/1607.06450>_ paper.
Compared to :class:LayerNorm, :class:HeteroLayerNorm applies
normalization individually for each node or edge type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Size of each input sample. |
required |
num_types
|
int
|
The number of types. |
required |
eps
|
float
|
A value added to the denominator for numerical
stability. (default: :obj: |
1e-05
|
affine
|
bool
|
If set to :obj: |
True
|
mode
|
str
|
The normalization mode to use for layer
normalization (:obj: |
'node'
|