diff --git a/GNNLux/src/layers/conv.jl b/GNNLux/src/layers/conv.jl index f7b8ee925..050fc18a8 100644 --- a/GNNLux/src/layers/conv.jl +++ b/GNNLux/src/layers/conv.jl @@ -825,7 +825,7 @@ and the attention coefficients will be calculated as - `heads`: Number attention heads. Default `1`. - `concat`: Concatenate layer output or not. If not, layer output is averaged over the heads. Default `true`. - `negative_slope`: The parameter of LeakyReLU.Default `0.2`. -- `init_weight`: Weights' initializer. Default `glorot_uniform`. +- `init_weight`: Initializer for the projections `W`, `W_e` and the attention vector `a`, called as `init_weight(rng, dims...)`. Default `glorot_uniform`. - `init_bias`: Bias initializer. Default `zeros32`. - `use_bias`: Add learnable bias. Default `true`. - `add_self_loops`: Add self loops to the graph before performing the convolution. Default `true`. @@ -885,8 +885,8 @@ function GATConv(ch::Pair{NTuple{2, Int}, Int}, σ = identity; @assert ein==0 "Using edge features and setting add_self_loops=true at the same time is not yet supported." end - dense_x = Dense(in => out * heads, use_bias = false) - dense_e = ein > 0 ? Dense(ein => out * heads, use_bias = false) : nothing + dense_x = Dense(in => out * heads; use_bias = false, init_weight) + dense_e = ein > 0 ? Dense(ein => out * heads; use_bias = false, init_weight) : nothing negative_slope = convert(Float32, negative_slope) return GATConv(dense_x, dense_e, init_weight, init_bias, use_bias, σ, negative_slope, ch, heads, concat, add_self_loops, dropout) @@ -898,7 +898,7 @@ LuxCore.outputsize(l::GATConv) = (l.concat ? l.channel[2]*l.heads : l.channel[2] function LuxCore.initialparameters(rng::AbstractRNG, l::GATConv) (in, ein), out = l.channel dense_x = LuxCore.initialparameters(rng, l.dense_x) - a = l.init_weight(ein > 0 ? 3out : 2out, l.heads) + a = l.init_weight(rng, ein > 0 ? 3out : 2out, l.heads) ps = (; dense_x, a) if ein > 0 ps = (ps..., dense_e = LuxCore.initialparameters(rng, l.dense_e)) @@ -963,7 +963,7 @@ and the attention coefficients will be calculated as - `negative_slope`: The parameter of LeakyReLU.Default `0.2`. - `add_self_loops`: Add self loops to the graph before performing the convolution. Default `true`. - `dropout`: Dropout probability on the normalized attention coefficient. Default `0.0`. -- `init_weight`: Weights' initializer. Default `glorot_uniform`. +- `init_weight`: Initializer for the projections `W_1`, `W_2`, `W_3` and the attention vector `a`, called as `init_weight(rng, dims...)`. Default `glorot_uniform`. - `init_bias`: Bias initializer. Default `zeros32`. - `use_bias`: Add learnable bias. Default `true`. @@ -1055,7 +1055,7 @@ function LuxCore.initialparameters(rng::AbstractRNG, l::GATv2Conv) (in, ein), out = l.channel dense_i = LuxCore.initialparameters(rng, l.dense_i) dense_j = LuxCore.initialparameters(rng, l.dense_j) - a = l.init_weight(out, l.heads) + a = l.init_weight(rng, out, l.heads) ps = (; dense_i, dense_j, a) if ein > 0 ps = (ps..., dense_e = LuxCore.initialparameters(rng, l.dense_e)) diff --git a/GNNLux/test/layers/conv.jl b/GNNLux/test/layers/conv.jl index 4e997ecb1..49c857446 100644 --- a/GNNLux/test/layers/conv.jl +++ b/GNNLux/test/layers/conv.jl @@ -1,3 +1,21 @@ +@testitem "GAT weight initialization" begin + using Lux, Random + + for layer in (GATConv, GATv2Conv) + l = layer(3 => 4; heads = 2) + ps, _ = Lux.setup(Xoshiro(0), l) + @test ps.a == first(Lux.setup(Xoshiro(0), l)).a + @test ps.a != first(Lux.setup(Xoshiro(1), l)).a + end + + init_weight(rng::AbstractRNG, dims...) = ones(Float32, dims...) + l = GATConv((3, 2) => 4; heads = 2, add_self_loops = false, init_weight) + ps, _ = Lux.setup(Xoshiro(0), l) + @test all(isone, ps.a) + @test all(isone, ps.dense_x.weight) + @test all(isone, ps.dense_e.weight) +end + @testitem "layers/conv" setup=[TestModuleLux] begin using .TestModuleLux