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

Add more example scripts #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions examples/gcn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions examples/gcn_featured_graph.jl
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

what's the error?

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)))
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
accuracy(x, y) = mean(onecold(softmax(cpu(model(x)))) .== onecold(cpu(y)))
accuracy(x, y) = mean(onecold(cpu(model(x))) .== onecold(cpu(y)))

Copy link
Member

Choose a reason for hiding this comment

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

onecold doesn't need normalized predictions



## 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))
47 changes: 47 additions & 0 deletions examples/gcn_gpu.jl
Original file line number Diff line number Diff line change
@@ -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))