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
62 additions
and
59 deletions
+62
-59
examples/pytorch/arma/citation.py
examples/pytorch/arma/citation.py
+2
-2
examples/pytorch/arma/model.py
examples/pytorch/arma/model.py
+2
-2
examples/pytorch/bgnn/BGNN.py
examples/pytorch/bgnn/BGNN.py
+0
-1
examples/pytorch/bgnn/run.py
examples/pytorch/bgnn/run.py
+9
-7
examples/pytorch/bgrl/eval_function.py
examples/pytorch/bgrl/eval_function.py
+2
-3
examples/pytorch/bgrl/main.py
examples/pytorch/bgrl/main.py
+14
-7
examples/pytorch/bgrl/model.py
examples/pytorch/bgrl/model.py
+3
-3
examples/pytorch/bgrl/utils.py
examples/pytorch/bgrl/utils.py
+8
-3
examples/pytorch/capsule/DGLDigitCapsule.py
examples/pytorch/capsule/DGLDigitCapsule.py
+2
-3
examples/pytorch/capsule/DGLRoutingLayer.py
examples/pytorch/capsule/DGLRoutingLayer.py
+1
-2
examples/pytorch/capsule/simple_routing.py
examples/pytorch/capsule/simple_routing.py
+1
-2
examples/pytorch/caregnn/main.py
examples/pytorch/caregnn/main.py
+2
-2
examples/pytorch/caregnn/main_sampling.py
examples/pytorch/caregnn/main_sampling.py
+3
-3
examples/pytorch/caregnn/model.py
examples/pytorch/caregnn/model.py
+1
-2
examples/pytorch/caregnn/model_sampling.py
examples/pytorch/caregnn/model_sampling.py
+2
-3
examples/pytorch/cluster_gcn/cluster_gcn.py
examples/pytorch/cluster_gcn/cluster_gcn.py
+3
-3
examples/pytorch/compGCN/data_loader.py
examples/pytorch/compGCN/data_loader.py
+2
-2
examples/pytorch/compGCN/main.py
examples/pytorch/compGCN/main.py
+2
-3
examples/pytorch/compGCN/models.py
examples/pytorch/compGCN/models.py
+2
-4
examples/pytorch/compGCN/utils.py
examples/pytorch/compGCN/utils.py
+1
-2
No files found.
examples/pytorch/arma/citation.py
View file @
704bcaf6
...
@@ -7,10 +7,10 @@ import numpy as np
...
@@ -7,10 +7,10 @@ import numpy as np
import
torch
import
torch
import
torch.nn
as
nn
import
torch.nn
as
nn
import
torch.optim
as
optim
import
torch.optim
as
optim
from
model
import
ARMA4NC
from
tqdm
import
trange
from
dgl.data
import
CiteseerGraphDataset
,
CoraGraphDataset
,
PubmedGraphDataset
from
dgl.data
import
CiteseerGraphDataset
,
CoraGraphDataset
,
PubmedGraphDataset
from
model
import
ARMA4NC
from
tqdm
import
trange
def
main
(
args
):
def
main
(
args
):
...
...
examples/pytorch/arma/model.py
View file @
704bcaf6
import
math
import
math
import
dgl.function
as
fn
import
torch
import
torch
import
torch.nn
as
nn
import
torch.nn
as
nn
import
torch.nn.functional
as
F
import
torch.nn.functional
as
F
import
dgl.function
as
fn
def
glorot
(
tensor
):
def
glorot
(
tensor
):
if
tensor
is
not
None
:
if
tensor
is
not
None
:
...
...
examples/pytorch/bgnn/BGNN.py
View file @
704bcaf6
...
@@ -188,7 +188,6 @@ class BGNNPredictor:
...
@@ -188,7 +188,6 @@ class BGNNPredictor:
def
init_optimizer
(
def
init_optimizer
(
self
,
node_features
,
optimize_node_features
,
learning_rate
self
,
node_features
,
optimize_node_features
,
learning_rate
):
):
params
=
[
self
.
model
.
parameters
()]
params
=
[
self
.
model
.
parameters
()]
if
optimize_node_features
:
if
optimize_node_features
:
params
.
append
([
node_features
])
params
.
append
([
node_features
])
...
...
examples/pytorch/bgnn/run.py
View file @
704bcaf6
...
@@ -7,15 +7,17 @@ import torch
...
@@ -7,15 +7,17 @@ import torch
import
torch.nn.functional
as
F
import
torch.nn.functional
as
F
from
BGNN
import
BGNNPredictor
from
BGNN
import
BGNNPredictor
from
category_encoders
import
CatBoostEncoder
from
category_encoders
import
CatBoostEncoder
from
sklearn
import
preprocessing
from
torch.nn
import
ELU
,
Dropout
,
Linear
,
ReLU
,
Sequential
from
dgl.data.utils
import
load_graphs
from
dgl.data.utils
import
load_graphs
from
dgl.nn.pytorch
import
AGNNConv
as
AGNNConvDGL
from
dgl.nn.pytorch
import
(
from
dgl.nn.pytorch
import
APPNPConv
AGNNConv
as
AGNNConvDGL
,
from
dgl.nn.pytorch
import
ChebConv
as
ChebConvDGL
APPNPConv
,
from
dgl.nn.pytorch
import
GATConv
as
GATConvDGL
ChebConv
as
ChebConvDGL
,
from
dgl.nn.pytorch
import
GraphConv
GATConv
as
GATConvDGL
,
GraphConv
,
)
from
sklearn
import
preprocessing
from
torch.nn
import
Dropout
,
ELU
,
Linear
,
ReLU
,
Sequential
class
GNNModelDGL
(
torch
.
nn
.
Module
):
class
GNNModelDGL
(
torch
.
nn
.
Module
):
...
...
examples/pytorch/bgrl/eval_function.py
View file @
704bcaf6
...
@@ -2,10 +2,9 @@ import numpy as np
...
@@ -2,10 +2,9 @@ import numpy as np
import
torch
import
torch
from
sklearn
import
metrics
from
sklearn
import
metrics
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.model_selection
import
(
GridSearchCV
,
ShuffleSplit
,
from
sklearn.model_selection
import
GridSearchCV
,
ShuffleSplit
,
train_test_split
train_test_split
)
from
sklearn.multiclass
import
OneVsRestClassifier
from
sklearn.multiclass
import
OneVsRestClassifier
from
sklearn.preprocessing
import
OneHotEncoder
,
normalize
from
sklearn.preprocessing
import
normalize
,
OneHotEncoder
def
fit_logistic_regression
(
X
,
y
,
data_random_seed
=
1
,
repeat
=
1
):
def
fit_logistic_regression
(
X
,
y
,
data_random_seed
=
1
,
repeat
=
1
):
...
...
examples/pytorch/bgrl/main.py
View file @
704bcaf6
...
@@ -2,20 +2,27 @@ import copy
...
@@ -2,20 +2,27 @@ import copy
import
os
import
os
import
warnings
import
warnings
import
dgl
import
numpy
as
np
import
numpy
as
np
import
torch
import
torch
from
eval_function
import
(
fit_logistic_regression
,
from
eval_function
import
(
fit_logistic_regression_preset_splits
,
fit_logistic_regression
,
fit_ppi_linear
)
fit_logistic_regression_preset_splits
,
from
model
import
(
BGRL
,
GCN
,
GraphSAGE_GCN
,
MLP_Predictor
,
fit_ppi_linear
,
compute_representations
)
)
from
model
import
(
BGRL
,
compute_representations
,
GCN
,
GraphSAGE_GCN
,
MLP_Predictor
,
)
from
torch.nn.functional
import
cosine_similarity
from
torch.nn.functional
import
cosine_similarity
from
torch.optim
import
AdamW
from
torch.optim
import
AdamW
from
tqdm
import
tqdm
from
tqdm
import
tqdm
from
utils
import
CosineDecayScheduler
,
get_dataset
,
get_graph_drop_transform
from
utils
import
CosineDecayScheduler
,
get_dataset
,
get_graph_drop_transform
import
dgl
warnings
.
filterwarnings
(
"ignore"
)
warnings
.
filterwarnings
(
"ignore"
)
...
...
examples/pytorch/bgrl/model.py
View file @
704bcaf6
import
copy
import
copy
import
dgl
import
torch
import
torch
from
dgl.nn.pytorch.conv
import
GraphConv
,
SAGEConv
from
torch
import
nn
from
torch
import
nn
from
torch.nn
import
BatchNorm1d
,
Parameter
from
torch.nn
import
BatchNorm1d
,
Parameter
from
torch.nn.init
import
ones_
,
zeros_
from
torch.nn.init
import
ones_
,
zeros_
import
dgl
from
dgl.nn.pytorch.conv
import
GraphConv
,
SAGEConv
class
LayerNorm
(
nn
.
Module
):
class
LayerNorm
(
nn
.
Module
):
def
__init__
(
self
,
in_channels
,
eps
=
1e-5
,
affine
=
True
):
def
__init__
(
self
,
in_channels
,
eps
=
1e-5
,
affine
=
True
):
...
...
examples/pytorch/bgrl/utils.py
View file @
704bcaf6
...
@@ -3,9 +3,14 @@ import copy
...
@@ -3,9 +3,14 @@ import copy
import
numpy
as
np
import
numpy
as
np
import
torch
import
torch
from
dgl.data
import
(
AmazonCoBuyComputerDataset
,
AmazonCoBuyPhotoDataset
,
from
dgl.data
import
(
CoauthorCSDataset
,
CoauthorPhysicsDataset
,
PPIDataset
,
AmazonCoBuyComputerDataset
,
WikiCSDataset
)
AmazonCoBuyPhotoDataset
,
CoauthorCSDataset
,
CoauthorPhysicsDataset
,
PPIDataset
,
WikiCSDataset
,
)
from
dgl.dataloading
import
GraphDataLoader
from
dgl.dataloading
import
GraphDataLoader
from
dgl.transforms
import
Compose
,
DropEdge
,
FeatMask
,
RowFeatNormalizer
from
dgl.transforms
import
Compose
,
DropEdge
,
FeatMask
,
RowFeatNormalizer
...
...
examples/pytorch/capsule/DGLDigitCapsule.py
View file @
704bcaf6
import
dgl
import
dgl.function
as
fn
import
torch
import
torch
from
DGLRoutingLayer
import
DGLRoutingLayer
from
DGLRoutingLayer
import
DGLRoutingLayer
from
torch
import
nn
from
torch
import
nn
from
torch.nn
import
functional
as
F
from
torch.nn
import
functional
as
F
import
dgl
import
dgl.function
as
fn
class
DGLDigitCapsuleLayer
(
nn
.
Module
):
class
DGLDigitCapsuleLayer
(
nn
.
Module
):
def
__init__
(
def
__init__
(
...
...
examples/pytorch/capsule/DGLRoutingLayer.py
View file @
704bcaf6
import
dgl
import
torch
as
th
import
torch
as
th
import
torch.nn
as
nn
import
torch.nn
as
nn
import
torch.nn.functional
as
F
import
torch.nn.functional
as
F
import
dgl
class
DGLRoutingLayer
(
nn
.
Module
):
class
DGLRoutingLayer
(
nn
.
Module
):
def
__init__
(
self
,
in_nodes
,
out_nodes
,
f_size
,
batch_size
=
0
,
device
=
"cpu"
):
def
__init__
(
self
,
in_nodes
,
out_nodes
,
f_size
,
batch_size
=
0
,
device
=
"cpu"
):
...
...
examples/pytorch/capsule/simple_routing.py
View file @
704bcaf6
import
dgl
import
torch
as
th
import
torch
as
th
import
torch.nn
as
nn
import
torch.nn
as
nn
from
DGLRoutingLayer
import
DGLRoutingLayer
from
DGLRoutingLayer
import
DGLRoutingLayer
from
torch.nn
import
functional
as
F
from
torch.nn
import
functional
as
F
import
dgl
g
=
dgl
.
DGLGraph
()
g
=
dgl
.
DGLGraph
()
g
.
graph_data
=
{}
g
.
graph_data
=
{}
...
...
examples/pytorch/caregnn/main.py
View file @
704bcaf6
import
argparse
import
argparse
import
dgl
import
torch
as
th
import
torch
as
th
import
torch.optim
as
optim
import
torch.optim
as
optim
from
model
import
CAREGNN
from
model
import
CAREGNN
...
@@ -7,8 +9,6 @@ from sklearn.metrics import recall_score, roc_auc_score
...
@@ -7,8 +9,6 @@ from sklearn.metrics import recall_score, roc_auc_score
from
torch.nn.functional
import
softmax
from
torch.nn.functional
import
softmax
from
utils
import
EarlyStopping
from
utils
import
EarlyStopping
import
dgl
def
main
(
args
):
def
main
(
args
):
# Step 1: Prepare graph data and retrieve train/validation/test index ============================= #
# Step 1: Prepare graph data and retrieve train/validation/test index ============================= #
...
...
examples/pytorch/caregnn/main_sampling.py
View file @
704bcaf6
import
argparse
import
argparse
import
dgl
import
torch
as
th
import
torch
as
th
import
torch.optim
as
optim
import
torch.optim
as
optim
from
model_sampling
import
CAREGNN
,
CARESampler
,
_l1_dist
from
model_sampling
import
_l1_dist
,
CAREGNN
,
CARESampler
from
sklearn.metrics
import
recall_score
,
roc_auc_score
from
sklearn.metrics
import
recall_score
,
roc_auc_score
from
torch.nn.functional
import
softmax
from
torch.nn.functional
import
softmax
from
utils
import
EarlyStopping
from
utils
import
EarlyStopping
import
dgl
def
evaluate
(
model
,
loss_fn
,
dataloader
,
device
=
"cpu"
):
def
evaluate
(
model
,
loss_fn
,
dataloader
,
device
=
"cpu"
):
loss
=
0
loss
=
0
...
...
examples/pytorch/caregnn/model.py
View file @
704bcaf6
import
dgl.function
as
fn
import
numpy
as
np
import
numpy
as
np
import
torch
as
th
import
torch
as
th
import
torch.nn
as
nn
import
torch.nn
as
nn
import
dgl.function
as
fn
class
CAREConv
(
nn
.
Module
):
class
CAREConv
(
nn
.
Module
):
"""One layer of CARE-GNN."""
"""One layer of CARE-GNN."""
...
...
examples/pytorch/caregnn/model_sampling.py
View file @
704bcaf6
import
dgl
import
dgl.function
as
fn
import
numpy
as
np
import
numpy
as
np
import
torch
as
th
import
torch
as
th
import
torch.nn
as
nn
import
torch.nn
as
nn
import
dgl
import
dgl.function
as
fn
def
_l1_dist
(
edges
):
def
_l1_dist
(
edges
):
# formula 2
# formula 2
...
...
examples/pytorch/cluster_gcn/cluster_gcn.py
View file @
704bcaf6
import
time
import
time
import
dgl
import
dgl.nn
as
dglnn
import
numpy
as
np
import
numpy
as
np
import
torch
import
torch
import
torch.nn
as
nn
import
torch.nn
as
nn
...
@@ -7,9 +10,6 @@ import torch.nn.functional as F
...
@@ -7,9 +10,6 @@ import torch.nn.functional as F
import
torchmetrics.functional
as
MF
import
torchmetrics.functional
as
MF
from
ogb.nodeproppred
import
DglNodePropPredDataset
from
ogb.nodeproppred
import
DglNodePropPredDataset
import
dgl
import
dgl.nn
as
dglnn
class
SAGE
(
nn
.
Module
):
class
SAGE
(
nn
.
Module
):
def
__init__
(
self
,
in_feats
,
n_hidden
,
n_classes
):
def
__init__
(
self
,
in_feats
,
n_hidden
,
n_classes
):
...
...
examples/pytorch/compGCN/data_loader.py
View file @
704bcaf6
from
collections
import
defaultdict
as
ddict
from
collections
import
defaultdict
as
ddict
import
dgl
import
numpy
as
np
import
numpy
as
np
import
torch
import
torch
from
ordered_set
import
OrderedSet
from
ordered_set
import
OrderedSet
from
torch.utils.data
import
DataLoader
,
Dataset
from
torch.utils.data
import
DataLoader
,
Dataset
import
dgl
class
TrainDataset
(
Dataset
):
class
TrainDataset
(
Dataset
):
"""
"""
...
...
examples/pytorch/compGCN/main.py
View file @
704bcaf6
import
argparse
import
argparse
from
time
import
time
from
time
import
time
import
dgl.function
as
fn
import
numpy
as
np
import
numpy
as
np
import
torch
as
th
import
torch
as
th
import
torch.nn
as
nn
import
torch.nn
as
nn
...
@@ -10,8 +12,6 @@ from data_loader import Data
...
@@ -10,8 +12,6 @@ from data_loader import Data
from
models
import
CompGCN_ConvE
from
models
import
CompGCN_ConvE
from
utils
import
in_out_norm
from
utils
import
in_out_norm
import
dgl.function
as
fn
# predict the tail for (head, rel, -1) or head for (-1, rel, tail)
# predict the tail for (head, rel, -1) or head for (-1, rel, tail)
def
predict
(
model
,
graph
,
device
,
data_iter
,
split
=
"valid"
,
mode
=
"tail"
):
def
predict
(
model
,
graph
,
device
,
data_iter
,
split
=
"valid"
,
mode
=
"tail"
):
...
@@ -96,7 +96,6 @@ def evaluate(model, graph, device, data_iter, split="valid"):
...
@@ -96,7 +96,6 @@ def evaluate(model, graph, device, data_iter, split="valid"):
def
main
(
args
):
def
main
(
args
):
# Step 1: Prepare graph data and retrieve train/validation/test index ============================= #
# Step 1: Prepare graph data and retrieve train/validation/test index ============================= #
# check cuda
# check cuda
if
args
.
gpu
>=
0
and
th
.
cuda
.
is_available
():
if
args
.
gpu
>=
0
and
th
.
cuda
.
is_available
():
...
...
examples/pytorch/compGCN/models.py
View file @
704bcaf6
import
dgl
import
dgl.function
as
fn
import
torch
as
th
import
torch
as
th
import
torch.nn
as
nn
import
torch.nn
as
nn
import
torch.nn.functional
as
F
import
torch.nn.functional
as
F
import
torch.optim
as
optim
import
torch.optim
as
optim
from
utils
import
ccorr
from
utils
import
ccorr
import
dgl
import
dgl.function
as
fn
class
CompGraphConv
(
nn
.
Module
):
class
CompGraphConv
(
nn
.
Module
):
"""One layer of CompGCN."""
"""One layer of CompGCN."""
...
@@ -41,7 +40,6 @@ class CompGraphConv(nn.Module):
...
@@ -41,7 +40,6 @@ class CompGraphConv(nn.Module):
nn
.
init
.
xavier_normal_
(
self
.
loop_rel
)
nn
.
init
.
xavier_normal_
(
self
.
loop_rel
)
def
forward
(
self
,
g
,
n_in_feats
,
r_feats
):
def
forward
(
self
,
g
,
n_in_feats
,
r_feats
):
with
g
.
local_scope
():
with
g
.
local_scope
():
# Assign values to source nodes. In a homogeneous graph, this is equal to
# Assign values to source nodes. In a homogeneous graph, this is equal to
# assigning them to all nodes.
# assigning them to all nodes.
...
...
examples/pytorch/compGCN/utils.py
View file @
704bcaf6
...
@@ -2,9 +2,8 @@
...
@@ -2,9 +2,8 @@
# <https://github.com/malllabiisc/CompGCN/blob/master/helper.py>.
# <https://github.com/malllabiisc/CompGCN/blob/master/helper.py>.
# It implements the operation of circular convolution in the ccorr function and an additional in_out_norm function for norm computation.
# It implements the operation of circular convolution in the ccorr function and an additional in_out_norm function for norm computation.
import
torch
as
th
import
dgl
import
dgl
import
torch
as
th
def
com_mult
(
a
,
b
):
def
com_mult
(
a
,
b
):
...
...
Prev
1
2
3
4
5
6
7
…
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