Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding PairNorm support #418

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/layers/normalise.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Implementation of normalization layers for GraphNeuralNetworks

@doc raw"""
PairNorm(scale_value; [scale_individually])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PairNorm(scale_value; [scale_individually])
PairNorm(scale_value; scale_individually=false)


PairNorm layer from paper [PairNorm: Tackling Oversmoothing in GNNs](https://arxiv.org/abs/1909.12223)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PairNorm layer from paper [PairNorm: Tackling Oversmoothing in GNNs](https://arxiv.org/abs/1909.12223)
PairNorm layer from paper [PairNorm: Tackling Oversmoothing in GNNs](https://arxiv.org/abs/1909.12223).


Performs the operation(normalization)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Performs the operation(normalization)
Performs the operation

```math
\mathbf{x}_i^c &= \mathbf{x}_i - \frac{1}{n}
\sum_{i=1}^n \mathbf{x}_i \\

\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}}
Comment on lines +10 to +15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
\mathbf{x}_i^c &= \mathbf{x}_i - \frac{1}{n}
\sum_{i=1}^n \mathbf{x}_i \\
\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}}
\mathbf{x}_i^c &= \mathbf{x}_i - \frac{1}{n}
\sum_{i=1}^n \mathbf{x}_i \\
\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}}

```

The input to this layer is the output from GNN layers

Comment on lines +18 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The input to this layer is the output from GNN layers

# Arguments

- `scale_value`: Scaling factor `s` used in normalisation. Default `1.0`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `scale_value`: Scaling factor `s` used in normalisation. Default `1.0`
- `scale_value`: Scaling factor `s` used in normalisation. Default `1.0`.

- `scale_individually`: If set to `true`, will compute the scaling step as

```math
\mathbf{x}^{\prime}_i = s \cdot
\frac{\mathbf{x}_i^c}{{\| \mathbf{x}_i^c \|}_2}
```
Default `false`
Comment on lines +25 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```math
\mathbf{x}^{\prime}_i = s \cdot
\frac{\mathbf{x}_i^c}{{\| \mathbf{x}_i^c \|}_2}
```
Default `false`
```math
\mathbf{x}^{\prime}_i = s \cdot
\frac{\mathbf{x}_i^c}{{\| \mathbf{x}_i^c \|}_2}
```
Default `false`.


- `ϵ` : Small value added in the denominator for numerical stability. Default `1f-5`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `ϵ` : Small value added in the denominator for numerical stability. Default `1f-5`
- `ϵ` : Small value added in the denominator for numerical stability. Default `1f-5`.

This should be mentioned in the first line fo the docstring.


# Examples

```julia
# create data
s = [1,1,2,3]
t = [2,3,1,1]
g = GNNGraph(s, t)
x = randn(Float32, 3, g.num_nodes)
scale_value = 1.0

# create layer
l = GCNConv(3 => 5)
pn = PairNorm(scale_value)

# forward pass of GCN
y = l(g, x) # size: 5 × num_nodes

# forward pass of PairNorm
ȳ = pn(y)
```

"""
struct PairNorm{V, N}
scale_value::V
ϵ::N
scale_individually::Bool
end

@functor PairNorm

function PairNorm(scale_value::Real=1.0f0; scale_individually::Bool=false, eps::Real=1f-5, ϵ=nothing)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function PairNorm(scale_value::Real=1.0f0; scale_individually::Bool=false, eps::Real=1f-5, ϵ=nothing)
function PairNorm(scale_value::Real=1.0f0; scale_individually::Bool=false, eps::Real=1f-5)

ε = _greek_ascii_depwarn(ϵ => eps, :BatchNorm, "ϵ" => "eps")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ε = _greek_ascii_depwarn(ϵ => eps, :BatchNorm, "ϵ" => "eps")

return PairNorm(scale_value, ε, scale_individually)
end

function (PN::PairNorm)(x::AbstractArray)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function (PN::PairNorm)(x::AbstractArray)
function (pn::PairNorm)(x::AbstractArray)
eps = ofeltype(x, pn.ϵ)
s = ofeltype(pn.scale_value)

xm = mean(x, dims=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all dimensions are wrong here and belowe. The node dimension is the secnd dimension, the feature dimension is the first

Suggested change
xm = mean(x, dims=1)
xm = mean(x, dims=2)

x = x .- xm
if PN.scale_individually
return (PN.scale_value .* x) ./ (PN.ϵ .+ [norm(x[i,:]) for i in axes(x,1)])
else
return (PN.scale_value .* x) ./ (PN.ϵ + √(mean(sum(x.^2, dims=2))))
end
end

Base.show(io::IO, pn::PairNorm) = print(io, "PairNorm(", pn.scale_value, ")")
Loading