Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dgl
Commits
704bcaf6
Unverified
Commit
704bcaf6
authored
Feb 19, 2023
by
Hongzhi (Steve), Chen
Committed by
GitHub
Feb 19, 2023
Browse files
examples (#5323)
Co-authored-by:
Ubuntu
<
ubuntu@ip-172-31-28-63.ap-northeast-1.compute.internal
>
parent
6bc82161
Changes
332
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
74 additions
and
74 deletions
+74
-74
examples/pytorch/metapath2vec/test.py
examples/pytorch/metapath2vec/test.py
+27
-15
examples/pytorch/mixhop/main.py
examples/pytorch/mixhop/main.py
+4
-5
examples/pytorch/model_zoo/geometric/coarsening.py
examples/pytorch/model_zoo/geometric/coarsening.py
+0
-3
examples/pytorch/model_zoo/geometric/mnist.py
examples/pytorch/model_zoo/geometric/mnist.py
+5
-5
examples/pytorch/multigpu/multi_gpu_graph_prediction.py
examples/pytorch/multigpu/multi_gpu_graph_prediction.py
+5
-5
examples/pytorch/multigpu/multi_gpu_node_classification.py
examples/pytorch/multigpu/multi_gpu_node_classification.py
+4
-4
examples/pytorch/mvgrl/graph/dataset.py
examples/pytorch/mvgrl/graph/dataset.py
+3
-4
examples/pytorch/mvgrl/graph/main.py
examples/pytorch/mvgrl/graph/main.py
+2
-3
examples/pytorch/mvgrl/graph/model.py
examples/pytorch/mvgrl/graph/model.py
+1
-1
examples/pytorch/mvgrl/graph/utils.py
examples/pytorch/mvgrl/graph/utils.py
+0
-2
examples/pytorch/mvgrl/node/dataset.py
examples/pytorch/mvgrl/node/dataset.py
+3
-4
examples/pytorch/mvgrl/node/main.py
examples/pytorch/mvgrl/node/main.py
+1
-1
examples/pytorch/mvgrl/node/main_sample.py
examples/pytorch/mvgrl/node/main_sample.py
+3
-3
examples/pytorch/node2vec/main.py
examples/pytorch/node2vec/main.py
+2
-3
examples/pytorch/node2vec/model.py
examples/pytorch/node2vec/model.py
+2
-3
examples/pytorch/node2vec/utils.py
examples/pytorch/node2vec/utils.py
+0
-1
examples/pytorch/ogb/cluster-gat/main.py
examples/pytorch/ogb/cluster-gat/main.py
+3
-3
examples/pytorch/ogb/cluster-gat/partition_utils.py
examples/pytorch/ogb/cluster-gat/partition_utils.py
+2
-2
examples/pytorch/ogb/cluster-sage/main.py
examples/pytorch/ogb/cluster-sage/main.py
+5
-5
examples/pytorch/ogb/cluster-sage/partition_utils.py
examples/pytorch/ogb/cluster-sage/partition_utils.py
+2
-2
No files found.
examples/pytorch/metapath2vec/test.py
View file @
704bcaf6
...
...
@@ -3,7 +3,7 @@ from sklearn.linear_model import LogisticRegression
from
sklearn.metrics
import
f1_score
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
venue_count
=
133
author_count
=
246678
experiment_times
=
1
...
...
@@ -25,7 +25,7 @@ if __name__ == "__main__":
file
.
readline
()
print
(
"read line by line"
)
for
line
in
file
:
embed
=
line
.
strip
().
split
(
' '
)
embed
=
line
.
strip
().
split
(
" "
)
if
embed
[
0
]
in
check_venue
:
venue_embed_dict
[
embed
[
0
]]
=
[]
for
i
in
range
(
1
,
len
(
embed
),
1
):
...
...
@@ -34,7 +34,7 @@ if __name__ == "__main__":
author_embed_dict
[
embed
[
0
]]
=
[]
for
j
in
range
(
1
,
len
(
embed
),
1
):
author_embed_dict
[
embed
[
0
]].
append
(
float
(
embed
[
j
]))
#get venue embeddings
#
get venue embeddings
print
(
"reading finished"
)
venues
=
list
(
venue_embed_dict
.
keys
())
authors
=
list
(
author_embed_dict
.
keys
())
...
...
@@ -68,11 +68,11 @@ if __name__ == "__main__":
# split data into training and testing
print
(
"splitting"
)
venue_split
=
int
(
venue_count
*
percent
)
venue_training
=
venue_embedding
[:
venue_split
,:]
venue_testing
=
venue_embedding
[
venue_split
:,:]
venue_training
=
venue_embedding
[:
venue_split
,
:]
venue_testing
=
venue_embedding
[
venue_split
:,
:]
author_split
=
int
(
author_count
*
percent
)
author_training
=
author_embedding
[:
author_split
,:]
author_testing
=
author_embedding
[
author_split
:,:]
author_training
=
author_embedding
[:
author_split
,
:]
author_testing
=
author_embedding
[
author_split
:,
:]
# split label into training and testing
venue_label
=
[]
venue_true
=
[]
...
...
@@ -94,15 +94,27 @@ if __name__ == "__main__":
author_true
=
np
.
array
(
author_true
)
file
.
close
()
print
(
"beging predicting"
)
clf_venue
=
LogisticRegression
(
random_state
=
0
,
solver
=
"lbfgs"
,
multi_class
=
"multinomial"
).
fit
(
venue_training
,
venue_label
)
clf_venue
=
LogisticRegression
(
random_state
=
0
,
solver
=
"lbfgs"
,
multi_class
=
"multinomial"
).
fit
(
venue_training
,
venue_label
)
y_pred_venue
=
clf_venue
.
predict
(
venue_testing
)
clf_author
=
LogisticRegression
(
random_state
=
0
,
solver
=
"lbfgs"
,
multi_class
=
"multinomial"
).
fit
(
author_training
,
author_label
)
clf_author
=
LogisticRegression
(
random_state
=
0
,
solver
=
"lbfgs"
,
multi_class
=
"multinomial"
).
fit
(
author_training
,
author_label
)
y_pred_author
=
clf_author
.
predict
(
author_testing
)
macro_average_venue
+=
f1_score
(
venue_true
,
y_pred_venue
,
average
=
"macro"
)
micro_average_venue
+=
f1_score
(
venue_true
,
y_pred_venue
,
average
=
"micro"
)
macro_average_author
+=
f1_score
(
author_true
,
y_pred_author
,
average
=
"macro"
)
micro_average_author
+=
f1_score
(
author_true
,
y_pred_author
,
average
=
"micro"
)
print
(
macro_average_venue
/
float
(
experiment_times
))
print
(
micro_average_venue
/
float
(
experiment_times
))
macro_average_venue
+=
f1_score
(
venue_true
,
y_pred_venue
,
average
=
"macro"
)
micro_average_venue
+=
f1_score
(
venue_true
,
y_pred_venue
,
average
=
"micro"
)
macro_average_author
+=
f1_score
(
author_true
,
y_pred_author
,
average
=
"macro"
)
micro_average_author
+=
f1_score
(
author_true
,
y_pred_author
,
average
=
"micro"
)
print
(
macro_average_venue
/
float
(
experiment_times
))
print
(
micro_average_venue
/
float
(
experiment_times
))
print
(
macro_average_author
/
float
(
experiment_times
))
print
(
micro_average_author
/
float
(
experiment_times
))
examples/pytorch/mixhop/main.py
View file @
704bcaf6
...
...
@@ -4,15 +4,15 @@ import argparse
import
copy
import
random
import
dgl
import
dgl.function
as
fn
import
numpy
as
np
import
torch
import
torch.nn
as
nn
import
torch.optim
as
optim
from
tqdm
import
trange
import
dgl
import
dgl.function
as
fn
from
dgl.data
import
CiteseerGraphDataset
,
CoraGraphDataset
,
PubmedGraphDataset
from
tqdm
import
trange
class
MixHopConv
(
nn
.
Module
):
...
...
@@ -83,7 +83,6 @@ class MixHopConv(nn.Module):
max_j
=
max
(
self
.
p
)
+
1
outputs
=
[]
for
j
in
range
(
max_j
):
if
j
in
self
.
p
:
output
=
self
.
weights
[
str
(
j
)](
feats
)
outputs
.
append
(
output
)
...
...
examples/pytorch/model_zoo/geometric/coarsening.py
View file @
704bcaf6
...
...
@@ -104,7 +104,6 @@ def HEM(W, levels, rid=None):
print
(
"Heavy Edge Matching coarsening with Xavier version"
)
for
_
in
range
(
levels
):
# CHOOSE THE WEIGHTS FOR THE PAIRING
# weights = ones(N,1) # metis weights
weights
=
degree
# graclus weights
...
...
@@ -186,7 +185,6 @@ def HEM_one_level(rr, cc, vv, rid, weights):
if
marked
[
nid
]:
tval
=
0.0
else
:
# First approach
if
2
==
1
:
tval
=
vv
[
rs
+
jj
]
*
(
...
...
@@ -230,7 +228,6 @@ def compute_perm(parents):
indices
.
append
(
list
(
range
(
M_last
)))
for
parent
in
parents
[::
-
1
]:
# Fake nodes go after real ones.
pool_singeltons
=
len
(
parent
)
...
...
examples/pytorch/model_zoo/geometric/mnist.py
View file @
704bcaf6
import
argparse
import
time
import
dgl
import
networkx
as
nx
import
numpy
as
np
import
torch
...
...
@@ -8,14 +10,12 @@ import torch.nn as nn
import
torch.nn.functional
as
F
from
coarsening
import
coarsen
from
coordinate
import
get_coordinates
,
z2polar
from
grid_graph
import
grid_graph
from
torch.utils.data
import
DataLoader
from
torchvision
import
datasets
,
transforms
import
dgl
from
dgl.data
import
load_data
,
register_data_args
from
dgl.nn.pytorch.conv
import
ChebConv
,
GMMConv
from
dgl.nn.pytorch.glob
import
MaxPooling
from
grid_graph
import
grid_graph
from
torch.utils.data
import
DataLoader
from
torchvision
import
datasets
,
transforms
argparser
=
argparse
.
ArgumentParser
(
"MNIST"
)
argparser
.
add_argument
(
...
...
examples/pytorch/multigpu/multi_gpu_graph_prediction.py
View file @
704bcaf6
import
argparse
import
dgl
import
dgl.nn
as
dglnn
import
torch
import
torch.distributed
as
dist
import
torch.nn
as
nn
import
torch.nn.functional
as
F
import
torch.optim
as
optim
from
dgl.data
import
AsGraphPredDataset
from
dgl.dataloading
import
GraphDataLoader
from
ogb.graphproppred
import
DglGraphPropPredDataset
,
Evaluator
from
ogb.graphproppred.mol_encoder
import
AtomEncoder
,
BondEncoder
from
tqdm
import
tqdm
import
dgl
import
dgl.nn
as
dglnn
from
dgl.data
import
AsGraphPredDataset
from
dgl.dataloading
import
GraphDataLoader
class
MLP
(
nn
.
Module
):
def
__init__
(
self
,
in_feats
):
...
...
examples/pytorch/multigpu/multi_gpu_node_classification.py
View file @
704bcaf6
import
argparse
import
os
import
dgl.nn
as
dglnn
import
torch
import
torch.distributed
as
dist
import
torch.multiprocessing
as
mp
...
...
@@ -8,10 +10,6 @@ import torch.nn as nn
import
torch.nn.functional
as
F
import
torchmetrics.functional
as
MF
import
tqdm
from
ogb.nodeproppred
import
DglNodePropPredDataset
from
torch.nn.parallel
import
DistributedDataParallel
import
dgl.nn
as
dglnn
from
dgl.data
import
AsNodePredDataset
from
dgl.dataloading
import
(
DataLoader
,
...
...
@@ -19,6 +17,8 @@ from dgl.dataloading import (
NeighborSampler
,
)
from
dgl.multiprocessing
import
shared_tensor
from
ogb.nodeproppred
import
DglNodePropPredDataset
from
torch.nn.parallel
import
DistributedDataParallel
class
SAGE
(
nn
.
Module
):
...
...
examples/pytorch/mvgrl/graph/dataset.py
View file @
704bcaf6
...
...
@@ -3,13 +3,13 @@ import os
import
re
from
collections
import
Counter
import
dgl
import
networkx
as
nx
import
numpy
as
np
import
torch
as
th
from
scipy.linalg
import
fractional_matrix_power
,
inv
import
dgl
from
dgl.data
import
DGLDataset
from
scipy.linalg
import
fractional_matrix_power
,
inv
""" Compute Personalized Page Ranking"""
...
...
@@ -137,7 +137,6 @@ def process(dataset):
def
load
(
dataset
):
basedir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
datadir
=
os
.
path
.
join
(
basedir
,
"data"
,
dataset
)
...
...
examples/pytorch/mvgrl/graph/main.py
View file @
704bcaf6
import
argparse
import
warnings
import
dgl
import
torch
as
th
from
dataset
import
load
import
dgl
from
dgl.dataloading
import
GraphDataLoader
warnings
.
filterwarnings
(
"ignore"
)
...
...
@@ -66,7 +66,6 @@ def collate(samples):
if
__name__
==
"__main__"
:
# Step 1: Prepare data =================================================================== #
dataset
=
load
(
args
.
dataname
)
...
...
examples/pytorch/mvgrl/graph/model.py
View file @
704bcaf6
import
torch
as
th
import
torch.nn
as
nn
from
utils
import
local_global_loss_
from
dgl.nn.pytorch
import
GraphConv
from
dgl.nn.pytorch.glob
import
SumPooling
from
utils
import
local_global_loss_
class
MLP
(
nn
.
Module
):
...
...
examples/pytorch/mvgrl/graph/utils.py
View file @
704bcaf6
...
...
@@ -61,7 +61,6 @@ def get_negative_expectation(q_samples, average=True):
def
local_global_loss_
(
l_enc
,
g_enc
,
graph_id
):
num_graphs
=
g_enc
.
shape
[
0
]
num_nodes
=
l_enc
.
shape
[
0
]
...
...
@@ -71,7 +70,6 @@ def local_global_loss_(l_enc, g_enc, graph_id):
neg_mask
=
th
.
ones
((
num_nodes
,
num_graphs
)).
to
(
device
)
for
nodeidx
,
graphidx
in
enumerate
(
graph_id
):
pos_mask
[
nodeidx
][
graphidx
]
=
1.0
neg_mask
[
nodeidx
][
graphidx
]
=
0.0
...
...
examples/pytorch/mvgrl/node/dataset.py
View file @
704bcaf6
""" Code adapted from https://github.com/kavehhassani/mvgrl """
import
dgl
import
networkx
as
nx
import
numpy
as
np
import
scipy.sparse
as
sp
import
torch
as
th
from
scipy.linalg
import
fractional_matrix_power
,
inv
from
sklearn.preprocessing
import
MinMaxScaler
import
dgl
from
dgl.data
import
CiteseerGraphDataset
,
CoraGraphDataset
,
PubmedGraphDataset
from
dgl.nn
import
APPNPConv
from
scipy.linalg
import
fractional_matrix_power
,
inv
from
sklearn.preprocessing
import
MinMaxScaler
def
preprocess_features
(
features
):
...
...
examples/pytorch/mvgrl/node/main.py
View file @
704bcaf6
...
...
@@ -8,7 +8,7 @@ import torch.nn as nn
warnings
.
filterwarnings
(
"ignore"
)
from
dataset
import
process_dataset
from
model
import
MVGRL
,
LogReg
from
model
import
LogReg
,
MVGRL
parser
=
argparse
.
ArgumentParser
(
description
=
"mvgrl"
)
...
...
examples/pytorch/mvgrl/node/main_sample.py
View file @
704bcaf6
...
...
@@ -2,16 +2,16 @@ import argparse
import
random
import
warnings
import
dgl
import
numpy
as
np
import
torch
as
th
import
torch.nn
as
nn
import
dgl
warnings
.
filterwarnings
(
"ignore"
)
from
dataset
import
process_dataset
,
process_dataset_appnp
from
model
import
MVGRL
,
LogReg
from
model
import
LogReg
,
MVGRL
parser
=
argparse
.
ArgumentParser
(
description
=
"mvgrl"
)
...
...
examples/pytorch/node2vec/main.py
View file @
704bcaf6
import
time
from
dgl.sampling
import
node2vec_random_walk
from
model
import
Node2vecModel
from
utils
import
load_graph
,
parse_arguments
from
dgl.sampling
import
node2vec_random_walk
def
time_randomwalk
(
graph
,
args
):
"""
...
...
@@ -49,7 +49,6 @@ def train_node2vec(graph, eval_set, args):
if
__name__
==
"__main__"
:
args
=
parse_arguments
()
graph
,
eval_set
=
load_graph
(
args
.
dataset
)
...
...
examples/pytorch/node2vec/model.py
View file @
704bcaf6
import
torch
import
torch.nn
as
nn
from
sklearn.linear_model
import
LogisticRegression
from
torch.utils.data
import
DataLoader
from
dgl.sampling
import
node2vec_random_walk
from
sklearn.linear_model
import
LogisticRegression
from
torch.utils.data
import
DataLoader
class
Node2vec
(
nn
.
Module
):
...
...
@@ -255,7 +255,6 @@ class Node2vecModel(object):
eval_steps
=-
1
,
device
=
"cpu"
,
):
self
.
model
=
Node2vec
(
g
,
embedding_dim
,
...
...
examples/pytorch/node2vec/utils.py
View file @
704bcaf6
...
...
@@ -23,7 +23,6 @@ def load_graph(name):
eval_set
=
[(
nodes_train
,
y_train
),
(
nodes_val
,
y_val
)]
elif
name
.
startswith
(
"ogbn"
):
dataset
=
DglNodePropPredDataset
(
name
)
graph
,
y
=
dataset
[
0
]
split_nodes
=
dataset
.
get_idx_split
()
...
...
examples/pytorch/ogb/cluster-gat/main.py
View file @
704bcaf6
...
...
@@ -2,6 +2,9 @@ import argparse
import
time
from
functools
import
partial
import
dgl
import
dgl.nn.pytorch
as
dglnn
import
numpy
as
np
import
torch
as
th
import
torch.nn
as
nn
...
...
@@ -12,9 +15,6 @@ from ogb.nodeproppred import DglNodePropPredDataset
from
sampler
import
ClusterIter
,
subgraph_collate_fn
from
torch.utils.data
import
DataLoader
import
dgl
import
dgl.nn.pytorch
as
dglnn
class
GAT
(
nn
.
Module
):
def
__init__
(
...
...
examples/pytorch/ogb/cluster-gat/partition_utils.py
View file @
704bcaf6
from
time
import
time
import
numpy
as
np
import
dgl
import
numpy
as
np
from
dgl
import
backend
as
F
from
dgl.transforms
import
metis_partition
...
...
examples/pytorch/ogb/cluster-sage/main.py
View file @
704bcaf6
...
...
@@ -3,6 +3,10 @@ import time
import
traceback
from
functools
import
partial
import
dgl
import
dgl.function
as
fn
import
dgl.nn.pytorch
as
dglnn
import
numpy
as
np
import
torch
as
th
import
torch.multiprocessing
as
mp
...
...
@@ -10,15 +14,11 @@ import torch.nn as nn
import
torch.nn.functional
as
F
import
torch.optim
as
optim
import
tqdm
from
dgl.data
import
RedditDataset
from
ogb.nodeproppred
import
DglNodePropPredDataset
from
sampler
import
ClusterIter
,
subgraph_collate_fn
from
torch.utils.data
import
DataLoader
import
dgl
import
dgl.function
as
fn
import
dgl.nn.pytorch
as
dglnn
from
dgl.data
import
RedditDataset
#### Neighbor sampler
...
...
examples/pytorch/ogb/cluster-sage/partition_utils.py
View file @
704bcaf6
from
time
import
time
import
numpy
as
np
import
dgl
import
numpy
as
np
from
dgl
import
backend
as
F
from
dgl.transforms
import
metis_partition
...
...
Prev
1
…
7
8
9
10
11
12
13
14
15
…
17
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment