Skip to content

Commit 7387bab

Browse files
fixes
1 parent cef511b commit 7387bab

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/featuredgraph.jl

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -298,29 +298,32 @@ function LightGraphs.laplacian_matrix(fg::FeaturedGraph, T::DataType=Int; dir::S
298298
end
299299

300300
"""
301-
normalized_laplacian(fg, T=Float32; selfloop=false, dir=:out)
301+
normalized_laplacian(fg, T=Float32; add_self_loops=false, dir=:out)
302302
303303
Normalized Laplacian matrix of graph `g`.
304304
305305
# Arguments
306306
307307
- `fg`: A `FeaturedGraph`.
308308
- `T`: result element type.
309-
- `selfloop`: adding self loop while calculating the matrix.
309+
- `add_self_loops`: add self-loops while calculating the matrix.
310310
- `dir`: the edge directionality considered (:out, :in, :both).
311311
"""
312-
function normalized_laplacian(fg::FeaturedGraph, T::DataType=Float32; selfloop::Bool=false, dir::Symbol=:out)
312+
function normalized_laplacian(fg::FeaturedGraph, T::DataType=Float32;
313+
add_self_loops::Bool=false, dir::Symbol=:out)
314+
= normalized_adjacency(fg, T; dir, add_self_loops)
315+
return I -
316+
end
317+
318+
function normalized_adjacency(fg::FeaturedGraph, T::DataType=Float32;
319+
add_self_loops::Bool=false, dir::Symbol=:out)
313320
A = adjacency_matrix(fg, T; dir=dir)
314-
sz = size(A)
315-
@assert sz[1] == sz[2]
316-
if selfloop
317-
A += I - Diagonal(A)
318-
else
319-
A -= Diagonal(A)
321+
if add_self_loops
322+
A += I
320323
end
321324
degs = vec(sum(A; dims=2))
322325
inv_sqrtD = Diagonal(inv.(sqrt.(degs)))
323-
return I - inv_sqrtD * A * inv_sqrtD
326+
return inv_sqrtD * A * inv_sqrtD
324327
end
325328

326329
@doc raw"""
@@ -350,18 +353,23 @@ _eigmax(A) = KrylovKit.eigsolve(Symmetric(A), 1, :LR)[1][1] # also eigs(A, x0, n
350353
# https://discourse.julialang.org/t/cuda-eigenvalues-of-a-sparse-matrix/46851/5
351354

352355
"""
353-
add_self_loops(fg::FeaturedGraph)
356+
add_self_loops(fg::FeaturedGraph; add_to_existing=true)
354357
355358
Return a featured graph with the same features as `fg`
356359
but also adding edges connecting the nodes to themselves.
360+
361+
If `add_to_existing=true`, nodes with already existing
362+
self-loops will obtain a second self-loop.
357363
"""
358-
function add_self_loops(fg::FeaturedGraph{<:COO_T})
364+
function add_self_loops(fg::FeaturedGraph{<:COO_T}; add_to_existing=true)
359365
s, t = edge_index(fg)
360366
@assert edge_feature(fg) === nothing
361367
@assert edge_weight(fg) === nothing
362-
mask_old_loops = s .!= t
363-
s = s[mask_old_loops]
364-
t = t[mask_old_loops]
368+
if !add_to_existing
369+
mask_old_loops = s .!= t
370+
s = s[mask_old_loops]
371+
t = t[mask_old_loops]
372+
end
365373
n = fg.num_nodes
366374
nodes = convert(typeof(s), [1:n;])
367375
s = [s; nodes]
@@ -371,11 +379,16 @@ function add_self_loops(fg::FeaturedGraph{<:COO_T})
371379
node_feature(fg), edge_feature(fg), global_feature(fg))
372380
end
373381

374-
function add_self_loops(fg::FeaturedGraph{<:ADJMAT_T})
382+
function add_self_loops(fg::FeaturedGraph{<:ADJMAT_T}; add_to_existing=true)
375383
A = graph(fg)
376384
@assert edge_feature(fg) === nothing
377-
nold = sum(Diagonal(A)) |> Int
378-
A = A - Diagonal(A) + I
385+
if add_to_existing
386+
nold = 0
387+
A += I
388+
else
389+
nold = sum(Diagonal(A)) |> Int
390+
A += I - Diagonal(A)
391+
end
379392
num_edges = fg.num_edges - nold + fg.num_nodes
380393
FeaturedGraph(A, fg.num_nodes, num_edges,
381394
node_feature(fg), edge_feature(fg), global_feature(fg))
@@ -396,6 +409,7 @@ function remove_self_loops(fg::FeaturedGraph{<:COO_T})
396409
end
397410

398411
@non_differentiable normalized_laplacian(x...)
412+
@non_differentiable normalized_adjacency(x...)
399413
@non_differentiable scaled_laplacian(x...)
400414
@non_differentiable adjacency_matrix(x...)
401415
@non_differentiable adjacency_list(x...)

src/layers/conv.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ end
3535
## therefore fallback to message passing framework on gpu for the time being
3636

3737
function (l::GCNConv)(fg::FeaturedGraph, x::AbstractMatrix)
38-
= normalized_laplacian(fg, eltype(x); selfloop=true)
39-
l.σ.(l.weight * x * .+ l.bias)
38+
= normalized_adjacency(fg, eltype(x); dir=:out, add_self_loops=true)
39+
l.σ.(l.weight * x * .+ l.bias)
4040
end
4141

4242
message(l::GCNConv, xi, xj) = xj
4343
update(l::GCNConv, m, x) = m
4444

4545
function (l::GCNConv)(fg::FeaturedGraph, x::CuMatrix)
46-
fg = add_self_loops(fg)
46+
fg = add_self_loops(fg; add_to_existing=true)
4747
T = eltype(l.weight)
4848
# cout = sqrt.(degree(fg, dir=:out))
4949
cin = 1 ./ reshape(sqrt.(T.(degree(fg, dir=:in))), 1, :)

0 commit comments

Comments
 (0)