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

fix(ml): handle pylint warning #113

Merged
merged 1 commit into from
Nov 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,17 @@
def cs_example(n_epochs=200):
hg2d = HugeGraph2DGL()
graph = hg2d.convert_graph(vertex_label="CORA_vertex", edge_label="CORA_edge")
if args.model == "mlp":
model = MLP(
in_dim=graph.ndata["feat"].shape[1],
hid_dim=64,
out_dim=graph.ndata["label"].unique().shape[0],
num_layers=3,
dropout=0.4,
)
elif args.model == "linear":
model = MLPLinear(
in_dim=graph.ndata["feat"].shape[1],
out_dim=graph.ndata["label"].unique().shape[0],
)
else:
raise NotImplementedError(f"Model {args.model} is not supported.")
model = MLP(
in_dim=graph.ndata["feat"].shape[1],
hid_dim=64,
out_dim=graph.ndata["label"].unique().shape[0],
num_layers=3,
dropout=0.4,
)
node_clf_task = NodeClassify(graph, model)
node_clf_task.train(lr=0.005, weight_decay=0.0005, n_epochs=n_epochs, patience=200)
print(node_clf_task.evaluate())


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Base predictor(C&S)")
parser.add_argument("--model", type=str, default="mlp", choices=["mlp", "linear"])
args = parser.parse_args()
cs_example()
2 changes: 1 addition & 1 deletion hugegraph-ml/src/hugegraph_ml/examples/gatne_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def gatne_example(n_epochs=200):
20,
)
gatne_task = HeteroSampleEmbedGATNE(graph, model)
embs = gatne_task.train_and_embed(lr=0.005, n_epochs=n_epochs)
gatne_task.train_and_embed(lr=0.005, n_epochs=n_epochs)


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions hugegraph-ml/src/hugegraph_ml/models/agnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
DGL code: https://github.com/dmlc/dgl/blob/master/python/dgl/nn/pytorch/conv/agnnconv.py
"""

import dgl
import torch


from dgl.nn.pytorch.conv import AGNNConv
import torch.nn as nn
from torch import nn
import torch.nn.functional as F


Expand All @@ -40,7 +40,7 @@ def __init__(self, num_layers, in_dim, hid_dim, out_dim, dropout):

self.attention_layers = nn.ModuleList()
# 2-layer AGNN
for i in range(self.num_layers):
for _ in range(self.num_layers):
self.attention_layers.append(AGNNConv())

self.output_layer = nn.Linear(hid_dim, out_dim, bias=False)
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-ml/src/hugegraph_ml/models/appnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
DGL code: https://github.com/dmlc/dgl/tree/master/examples/pytorch/appnp
"""

import torch.nn as nn
from torch import nn

from dgl.nn.pytorch.conv import APPNPConv

Expand Down
2 changes: 2 additions & 0 deletions hugegraph-ml/src/hugegraph_ml/models/arma.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.right (c) 2024 by jinsong, All Rights Reserved.

# pylint: disable=E1101

"""
auto-regressive moving average (ARMA)

Expand Down
33 changes: 3 additions & 30 deletions hugegraph-ml/src/hugegraph_ml/models/bgnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1102,E0401,E0711,E0606,E0602

"""
Boost-GNN (BGNN)

Expand All @@ -28,7 +30,7 @@
import itertools
import time
from collections import defaultdict as ddict

import dgl
import numpy as np
import pandas as pd
import torch
Expand Down Expand Up @@ -650,35 +652,6 @@ def forward(self, graph, features):

return logits


def read_input(input_folder):
X = pd.read_csv(f"{input_folder}/X.csv")
y = pd.read_csv(f"{input_folder}/y.csv")

categorical_columns = []
if os.path.exists(f"{input_folder}/cat_features.txt"):
with open(f"{input_folder}/cat_features.txt") as f:
for line in f:
if line.strip():
categorical_columns.append(line.strip())

cat_features = None
if categorical_columns:
columns = X.columns
cat_features = np.where(columns.isin(categorical_columns))[0]

for col in list(columns[cat_features]):
X[col] = X[col].astype(str)

gs, _ = load_graphs(f"{input_folder}/graph.dgl")
graph = gs[0]

with open(f"{input_folder}/masks.json") as f:
masks = json.load(f)

return graph, X, y, cat_features, masks


def normalize_features(X, train_mask, val_mask, test_mask):
min_max_scaler = preprocessing.MinMaxScaler()
A = X.to_numpy(copy=True)
Expand Down
14 changes: 7 additions & 7 deletions hugegraph-ml/src/hugegraph_ml/models/bgrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.right (c) 2024 by jinsong, All Rights Reserved.

# pylint: disable=E1102

"""
Bootstrapped Graph Latents (BGRL)

Expand All @@ -26,16 +28,14 @@
"""

import copy

import dgl

import torch
from dgl.nn.pytorch.conv import GraphConv, SAGEConv
from torch import nn
from torch.nn import BatchNorm1d, Parameter
from torch.nn.init import ones_, zeros_
from dgl.transforms import Compose, DropEdge, FeatMask
from torch.nn import BatchNorm1d
from torch.nn.functional import cosine_similarity
import dgl
from dgl.nn.pytorch.conv import GraphConv
from dgl.transforms import Compose, DropEdge, FeatMask
import numpy as np

class MLP_Predictor(nn.Module):
r"""MLP used for predictor. The MLP has one hidden layer.
Expand Down
2 changes: 2 additions & 0 deletions hugegraph-ml/src/hugegraph_ml/models/care_gnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1101

"""
CAmouflage-REsistant GNN (CARE-GNN)

Expand Down
6 changes: 2 additions & 4 deletions hugegraph-ml/src/hugegraph_ml/models/cluster_gcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1101,E0401

"""
Cluster-GCN

Expand All @@ -25,13 +27,9 @@
DGL code: https://github.com/dmlc/dgl/tree/master/examples/pytorch/cluster_gcn
"""

import dgl
import dgl.nn as dglnn
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchmetrics.functional as MF


class SAGE(nn.Module):
def __init__(self, in_feats, n_hidden, n_classes):
Expand Down
7 changes: 3 additions & 4 deletions hugegraph-ml/src/hugegraph_ml/models/correct_and_smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1101,E1102

"""
Correct and Smooth (C&S)

Expand All @@ -27,7 +29,7 @@

import dgl.function as fn
import torch
import torch.nn as nn
from torch import nn
import torch.nn.functional as F


Expand Down Expand Up @@ -98,8 +100,6 @@ class LabelPropagation(nn.Module):

Description
-----------
Introduced in `Learning from Labeled and Unlabeled Data with Label Propagation <https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.14.3864&rep=rep1&type=pdf>`_

.. math::
\mathbf{Y}^{\prime} = \alpha \cdot \mathbf{D}^{-1/2} \mathbf{A}
\mathbf{D}^{-1/2} \mathbf{Y} + (1 - \alpha) \mathbf{Y},
Expand Down Expand Up @@ -166,7 +166,6 @@ class CorrectAndSmooth(nn.Module):

Description
-----------
Introduced in `Combining Label Propagation and Simple Models Out-performs Graph Neural Networks <https://arxiv.org/abs/2010.13993>`_

Parameters
----------
Expand Down
6 changes: 3 additions & 3 deletions hugegraph-ml/src/hugegraph_ml/models/dagnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1101

"""
Deep Adaptive Graph Neural Network (DAGNN)

Expand All @@ -26,12 +28,10 @@
"""

import dgl.function as fn

import numpy as np
import torch
from torch import nn
from torch.nn import functional as F, Parameter
import random



class DAGNNConv(nn.Module):
Expand Down
17 changes: 3 additions & 14 deletions hugegraph-ml/src/hugegraph_ml/models/deepergcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.



"""
DeeperGCN

Expand All @@ -30,17 +32,15 @@
import torch.nn.functional as F
from dgl.nn.functional import edge_softmax
from dgl.nn.pytorch.glob import AvgPooling
from ogb.graphproppred.mol_encoder import AtomEncoder, BondEncoder
import torch

# pylint: disable=E1101,E0401

class DeeperGCN(nn.Module):
r"""

Description
-----------
Introduced in "DeeperGCN: All You Need to Train Deeper GCNs <https://arxiv.org/abs/2006.07739>"

Parameters
----------
node_feat_dim: int
Expand Down Expand Up @@ -137,8 +137,6 @@ class GENConv(nn.Module):

Description
-----------
Generalized Message Aggregator was introduced in "DeeperGCN: All You Need to Train Deeper GCNs <https://arxiv.org/abs/2006.07739>"

Parameters
----------
in_dim: int
Expand Down Expand Up @@ -244,13 +242,6 @@ def forward(self, g, node_feats, edge_feats):


class MLP(nn.Sequential):
r"""

Description
-----------
From equation (5) in "DeeperGCN: All You Need to Train Deeper GCNs <https://arxiv.org/abs/2006.07739>"
"""

def __init__(self, channels, act="relu", dropout=0.0, bias=True):
layers = []

Expand All @@ -269,8 +260,6 @@ class MessageNorm(nn.Module):

Description
-----------
Message normalization was introduced in "DeeperGCN: All You Need to Train Deeper GCNs <https://arxiv.org/abs/2006.07739>"

Parameters
----------
learn_scale: bool
Expand Down
4 changes: 3 additions & 1 deletion hugegraph-ml/src/hugegraph_ml/models/gatne.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.right (c) 2024 by jinsong, All Rights Reserved.

# pylint: disable=E1101

"""
General Attributed Multiplex HeTerogeneous Network Embedding (GATNE)

Expand All @@ -33,7 +35,7 @@

import numpy as np
import torch
import torch.nn as nn
from torch import nn
import torch.nn.functional as F
from numpy import random
from torch.nn.parameter import Parameter
Expand Down
4 changes: 3 additions & 1 deletion hugegraph-ml/src/hugegraph_ml/models/pgnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1101,E0401

"""
Position-aware Graph Neural Networks (P-GNN)

Expand All @@ -27,7 +29,7 @@

import dgl.function as fn
import torch
import torch.nn as nn
from torch import nn
import torch.nn.functional as F
import multiprocessing as mp
import random
Expand Down
2 changes: 2 additions & 0 deletions hugegraph-ml/src/hugegraph_ml/models/seal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E0401

"""
SEAL

Expand Down
7 changes: 4 additions & 3 deletions hugegraph-ml/src/hugegraph_ml/tasks/fraud_detector_caregnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E0401

import torch
from dgl import DGLGraph
from torch import nn
from sklearn.metrics import recall_score, roc_auc_score
from torch.nn.functional import softmax

from dgl import DGLGraph
from sklearn.metrics import recall_score, roc_auc_score

class DetectorCaregnn:
def __init__(self, graph: DGLGraph, model: nn.Module):
Expand Down
4 changes: 2 additions & 2 deletions hugegraph-ml/src/tests/test_examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ def test_arma_example(self):

def test_bgnn_example(self):
try:
bgnn_example(n_epochs=self.test_n_epochs)
bgnn_example()
except ValueError:
self.fail("model bgnn example failed")

def test_bgrl_example(self):
try:
bgrl_example(n_epochs=self.test_n_epochs)
bgrl_example(n_epochs_embed=self.test_n_epochs, n_epochs_clf=self.test_n_epochs)
except ValueError:
self.fail("model bgrl example failed")

Expand Down
Loading