From c779fc589d5e86409ae7dec344e927280fbc2eed Mon Sep 17 00:00:00 2001 From: abieler Date: Thu, 15 Jul 2021 14:51:22 +0200 Subject: [PATCH] Add more example scripts --- examples/gcn.jl | 17 ++++++------ examples/gcn_featured_graph.jl | 50 ++++++++++++++++++++++++++++++++++ examples/gcn_gpu.jl | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 examples/gcn_featured_graph.jl create mode 100644 examples/gcn_gpu.jl diff --git a/examples/gcn.jl b/examples/gcn.jl index ecf641558..acd4399bd 100644 --- a/examples/gcn.jl +++ b/examples/gcn.jl @@ -23,15 +23,16 @@ target_catg = 7 epochs = 100 ## Preprocessing data -train_X = Matrix{Float32}(features) |> gpu # dim: num_features * num_nodes -train_y = Matrix{Float32}(labels) |> gpu # dim: target_catg * num_nodes -adj_mat = Matrix{Float32}(adjacency_matrix(g)) |> gpu - +train_X = Matrix{Float32}(features) # dim: num_features * num_nodes +train_y = Matrix{Float32}(labels) # dim: target_catg * num_nodes +adj_mat = Matrix{Float32}(adjacency_matrix(g)) + +model = Chain( + GCNConv(adj_mat, num_features=>hidden, relu), + Dropout(0.5), + GCNConv(adj_mat, hidden=>target_catg), +) ## Model -model = Chain(GCNConv(adj_mat, num_features=>hidden, relu), - Dropout(0.5), - GCNConv(adj_mat, hidden=>target_catg), - ) |> gpu ## Loss loss(x, y) = logitcrossentropy(model(x), y) diff --git a/examples/gcn_featured_graph.jl b/examples/gcn_featured_graph.jl new file mode 100644 index 000000000..4c87f7b80 --- /dev/null +++ b/examples/gcn_featured_graph.jl @@ -0,0 +1,50 @@ +using GeometricFlux +using Flux +using Flux: onehotbatch, onecold, logitcrossentropy, throttle +using Flux: @epochs +using JLD2 +using Statistics +using SparseArrays +using LightGraphs.SimpleGraphs +using LightGraphs: adjacency_matrix +using CUDA +using Random + +Random.seed!([0x6044b4da, 0xd873e4f9, 0x59d90c0a, 0xde01aa81]) + +@load "data/cora_features.jld2" features +@load "data/cora_labels.jld2" labels +@load "data/cora_graph.jld2" g + +num_nodes = 2708 +num_features = 1433 +hidden = 16 +target_catg = 7 +epochs = 5 + +## Preprocessing data +train_X = Matrix{Float32}(features) # dim: num_features * num_nodes +train_y = Matrix{Float32}(labels) # dim: target_catg * num_nodes +adj_mat = Matrix{Float32}(adjacency_matrix(g)) + +## Model +model = Chain( + GCNConv(num_features=>hidden, relu), + # Dropout(0.5), --> does not work + GCNConv(hidden=>target_catg, relu), + FeatureSelector(:node) +) + +## Loss +loss(x, y) = logitcrossentropy(model(x), y) +accuracy(x, y) = mean(onecold(softmax(cpu(model(x)))) .== onecold(cpu(y))) + + +## Training +ps = Flux.params(model) +fg = FeaturedGraph(adj_mat, nf=train_X) +train_data = [(fg, train_y)] +opt = ADAM(0.01) +evalcb() = @show(accuracy(fg, train_y)) + +@epochs epochs Flux.train!(loss, ps, train_data, opt, cb=throttle(evalcb, 10)) diff --git a/examples/gcn_gpu.jl b/examples/gcn_gpu.jl new file mode 100644 index 000000000..9a95dd5f5 --- /dev/null +++ b/examples/gcn_gpu.jl @@ -0,0 +1,47 @@ +using GeometricFlux +using Flux +using Flux: onehotbatch, onecold, logitcrossentropy, throttle +using Flux: @epochs +using JLD2 +using Statistics +using SparseArrays +using LightGraphs.SimpleGraphs +using LightGraphs: adjacency_matrix +using CUDA +using Random + +Random.seed!([0x6044b4da, 0xd873e4f9, 0x59d90c0a, 0xde01aa81]) + +@load "data/cora_features.jld2" features +@load "data/cora_labels.jld2" labels +@load "data/cora_graph.jld2" g + +num_nodes = 2708 +num_features = 1433 +hidden = 16 +target_catg = 7 +epochs = 100 + +## Preprocessing data +train_X = Matrix{Float32}(features) |> gpu # dim: num_features * num_nodes +train_y = Matrix{Float32}(labels) |> gpu # dim: target_catg * num_nodes +adj_mat = Matrix{Float32}(adjacency_matrix(g)) |> gpu + +## Model +model = Chain(GCNConv(adj_mat, num_features=>hidden, relu), + Dropout(0.5), + GCNConv(adj_mat, hidden=>target_catg), + ) |> gpu + +## Loss +loss(x, y) = logitcrossentropy(model(x), y) +accuracy(x, y) = mean(onecold(softmax(cpu(model(x)))) .== onecold(cpu(y))) + + +## Training +ps = Flux.params(model) +train_data = [(train_X, train_y)] +opt = ADAM(0.01) +evalcb() = @show(accuracy(train_X, train_y)) + +@epochs epochs Flux.train!(loss, ps, train_data, opt, cb=throttle(evalcb, 10))