def __init__( self, hidden_dim: _typ.Optional[int] = 64, layer_number: _typ.Optional[int] = 2, dropout: _typ.Optional[float] = 0.9, input_dim: _typ.Optional[int] = None, output_dim: _typ.Optional[int] = None, ops_type=0, gnn_ops: _typ.Sequence[_typ.Union[str, _typ.Any]] = None, act_op="tanh", head=8, agg_ops=['add', 'attn'], # agg_ops=['attn'], ): super().__init__() from autogl.backend import DependentBackend assert not DependentBackend.is_dgl(), "Now AutoAttend only support pyg" self.layer_number = layer_number self.hidden_dim = hidden_dim self.input_dim = input_dim self.output_dim = output_dim self.gnn_ops = gnn_ops self.dropout = dropout self.act_op = act_op self.ops_type = ops_type self.head = head self.agg_ops = agg_ops
HPO Module for tuning hyper parameters """ import time import numpy as np from tqdm import trange from . import register_hpo from .base import BaseHPOptimizer, TimeTooLimitedError import random from .autone_file import utils from ..feature import NetLSD as SgNetLSD from autogl.backend import DependentBackend _isdgl = DependentBackend.is_dgl() if _isdgl: import dgl else: from torch_geometric.data import InMemoryDataset from torch_geometric.data import GraphSAINTRandomWalkSampler class _MyDataset(InMemoryDataset): def __init__(self, datalist) -> None: super().__init__() self.data, self.slices = self.collate(datalist) @register_hpo("autone") class AutoNE(BaseHPOptimizer): """
from .base_encoder import BaseEncoderMaintainer, AutoHomogeneousEncoderMaintainer from .encoder_registry import EncoderUniversalRegistry from autogl.backend import DependentBackend if DependentBackend.is_pyg(): from ._pyg import (GCNEncoderMaintainer, GATEncoderMaintainer, GINEncoderMaintainer, SAGEEncoderMaintainer) else: from ._dgl import (GCNEncoderMaintainer, GATEncoderMaintainer, GINEncoderMaintainer, SAGEEncoderMaintainer, AutoTopKMaintainer) __all__ = [ "BaseEncoderMaintainer", "EncoderUniversalRegistry", "AutoHomogeneousEncoderMaintainer", "GCNEncoderMaintainer", "GATEncoderMaintainer", "GINEncoderMaintainer", "SAGEEncoderMaintainer", ] if DependentBackend.is_dgl(): __all__.append("AutoTopKMaintainer")
args = parser.parse_args() if torch.cuda.is_available(): torch.cuda.set_device(args.device) seed = args.seed # set random seed random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False dataset = build_dataset_from_name(args.dataset) label = dataset[0].nodes.data["y" if DependentBackend.is_pyg( ) else "label"] num_classes = len(np.unique(label.numpy())) configs = yaml.load(open(args.configs, "r").read(), Loader=yaml.FullLoader) configs["hpo"]["name"] = args.hpo configs["hpo"]["max_evals"] = args.max_eval autoClassifier = AutoNodeClassifier.from_config(configs) # train if args.dataset in ["cora", "citeseer", "pubmed"]: autoClassifier.fit(dataset, time_limit=3600, evaluation_method=[Acc]) else: autoClassifier.fit( dataset, time_limit=3600, evaluation_method=[Acc],
Test file for nas on node classification AUTOGL_BACKEND=pyg python test/nas/node_classification.py AUTOGL_BACKEND=dgl python test/nas/node_classification.py TODO: make it a unit test file to test all the possible combinations """ import os import logging logging.basicConfig(level=logging.INFO) from autogl.backend import DependentBackend if DependentBackend.is_dgl(): from autogl.module.model.dgl import BaseAutoModel from dgl.data import CoraGraphDataset elif DependentBackend.is_pyg(): from torch_geometric.datasets import Planetoid from autogl.module.model.pyg import BaseAutoModel from autogl.datasets import build_dataset_from_name import torch import torch.nn.functional as F from autogl.module.nas.space.single_path import SinglePathNodeClassificationSpace from autogl.module.nas.space.graph_nas import GraphNasNodeClassificationSpace from autogl.module.nas.space.graph_nas_macro import GraphNasMacroNodeClassificationSpace from autogl.module.nas.estimator.one_shot import OneShotEstimator from autogl.module.nas.space.autoattend import AutoAttendNodeClassificationSpace from autogl.module.nas.backend import bk_feat, bk_label from autogl.module.nas.algorithm import Darts, RL, GraphNasRL, Enas, RandomSearch, Spos
# set random seed random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False dataset = build_dataset_from_name(args.dataset) # split the edges for dataset dataset = split_edges(dataset, 0.8, 0.05) # add self-loop if DependentBackend.is_dgl(): import dgl # add self loop to 0 data = list(dataset[0]) data[0] = dgl.add_self_loop(data[0]) dataset = [data] configs = yaml.load(open(args.configs, "r").read(), Loader=yaml.FullLoader) configs["hpo"]["name"] = args.hpo configs["hpo"]["max_evals"] = args.max_eval autoClassifier = AutoLinkPredictor.from_config(configs) # train autoClassifier.fit(dataset, time_limit=3600, evaluation_method=[Auc],
""" Example of graph classification on given datasets. This version use random split to only show the usage of AutoGraphClassifier. Refer to `graph_cv.py` for cross validation evaluation of the whole system following paper `A Fair Comparison of Graph Neural Networks for Graph Classification` """ import random import torch import numpy as np from autogl.datasets import build_dataset_from_name, utils from autogl.solver import AutoGraphClassifier from autogl.module import Acc from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter from autogl.backend import DependentBackend if DependentBackend.is_pyg(): from autogl.datasets.utils.conversion import to_pyg_dataset as convert_dataset else: from autogl.datasets.utils.conversion import to_dgl_dataset as convert_dataset backend = DependentBackend.get_backend_name() if __name__ == "__main__": parser = ArgumentParser("auto graph classification", formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument( "--dataset", default="mutag", type=str, help="graph classification dataset", choices=["mutag", "imdb-b", "imdb-m", "proteins", "collab"], )
import os os.environ["AUTOGL_BACKEND"] = "dgl" from autogl.datasets import build_dataset_from_name from autogl.solver import AutoNodeClassifier from autogl.module.train import NodeClassificationFullTrainer from autogl.backend import DependentBackend key = "y" if DependentBackend.is_pyg() else "label" cora = build_dataset_from_name("cora") solver = AutoNodeClassifier(graph_models=("gin", ), default_trainer=NodeClassificationFullTrainer( decoder=None, init=False, max_epoch=200, early_stopping_round=201, lr=0.01, weight_decay=0.0, ), hpo_module=None, device="auto") solver.fit(cora, evaluation_method=["acc"]) result = solver.predict(cora) print((result == cora[0].nodes.data[key][ cora[0].nodes.data["test_mask"]].cpu().numpy()).astype('float').mean())
from autogl.datasets import build_dataset_from_name from autogl.solver import AutoNodeClassifier from autogl.solver.utils import set_seed import argparse from autogl.backend import DependentBackend if __name__ == '__main__': set_seed(202106) parser = argparse.ArgumentParser() parser.add_argument('--config', type=str, default='../configs/nodeclf_nas_macro_benchmark2.yml') parser.add_argument('--dataset', choices=['cora', 'citeseer', 'pubmed'], default='cora', type=str) args = parser.parse_args() dataset = build_dataset_from_name(args.dataset) label = dataset[0].nodes.data["y" if DependentBackend.is_pyg( ) else "label"][dataset[0].nodes.data["test_mask"]].cpu().numpy() solver = AutoNodeClassifier.from_config(args.config) solver.fit(dataset) solver.get_leaderboard().show() acc = solver.evaluate(metric="acc") print('acc on dataset', acc)
from autogl.datasets import build_dataset_from_name from autogl.solver import AutoLinkPredictor from autogl.backend import DependentBackend import numpy as np import scipy.sparse as sp if DependentBackend.is_pyg(): from torch_geometric.utils import train_test_split_edges from autogl.datasets.utils.conversion._to_pyg_dataset import to_pyg_dataset as convert_dataset def split_edges(dataset, train, val): for i in range(len(dataset)): dataset[i] = train_test_split_edges(dataset[i], val, 1 - train - val) return dataset else: import dgl from autogl.datasets.utils.conversion._to_dgl_dataset import to_dgl_dataset as convert_dataset def split_train_test(g, train, val): u, v = g.edges() eids = np.arange(g.number_of_edges()) eids = np.random.permutation(eids) test_size = int(len(eids) * (1 - train - val)) val_size = int(len(eids) * val) train_size = g.number_of_edges() - test_size - val_size test_pos_u, test_pos_v = u[eids[:test_size]], v[eids[:test_size]] val_pos_u, val_pos_v = u[eids[test_size : test_size + val_size]], v[eids[test_size : test_size + val_size]] train_pos_u, train_pos_v = u[eids[test_size + val_size:]], v[eids[test_size + val_size:]] # Find all negative edges and split them for training and testing adj = sp.coo_matrix((np.ones(len(u)), (u.numpy(), v.numpy()))) adj_neg = 1 - adj.todense() - np.eye(g.number_of_nodes())