From 55c3c6bd552b1c1baeac4907c04d50b1e3a3139c Mon Sep 17 00:00:00 2001 From: generall Date: Mon, 28 Jun 2021 14:41:12 +0800 Subject: [PATCH 1/3] add exmaple --- autogl/module/nas/space/base.py | 4 ++- configs/nodeclf_nas_darts_benchmark.yml | 42 +++++++++++++++++++++++++ configs/nodeclf_nas_enas_benchmark.yml | 42 +++++++++++++++++++++++++ configs/nodeclf_nas_macro_benchmark.yml | 40 +++++++++++++++++++++++ examples/test_nas.py | 22 +++++++++++++ 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 configs/nodeclf_nas_darts_benchmark.yml create mode 100644 configs/nodeclf_nas_enas_benchmark.yml create mode 100644 configs/nodeclf_nas_macro_benchmark.yml create mode 100644 examples/test_nas.py diff --git a/autogl/module/nas/space/base.py b/autogl/module/nas/space/base.py index a4c247c..433157d 100644 --- a/autogl/module/nas/space/base.py +++ b/autogl/module/nas/space/base.py @@ -89,6 +89,7 @@ class BoxModel(BaseModel): self.num_classes = self._model.output_dim self.params = {"num_class": self.num_classes, "features_num": self.num_features} self.device = device + self.selection = None def fix(self, selection): """ @@ -119,7 +120,8 @@ class BoxModel(BaseModel): ret_self = deepcopy(self) ret_self._model.instantiate() - apply_fixed_architecture(ret_self._model, ret_self.selection, verbose=False) + if ret_self.selection: + apply_fixed_architecture(ret_self._model, ret_self.selection, verbose=False) ret_self.to(self.device) return ret_self diff --git a/configs/nodeclf_nas_darts_benchmark.yml b/configs/nodeclf_nas_darts_benchmark.yml new file mode 100644 index 0000000..154bffa --- /dev/null +++ b/configs/nodeclf_nas_darts_benchmark.yml @@ -0,0 +1,42 @@ +ensemble: + name: null +feature: +- name: PYGNormalizeFeatures +hpo: + max_evals: 10 + name: random +nas: + space: + name: singlepath + hidden_dim: 64 + layer_number: 2 + dropout: 0.8 + ops: ['gcn', 'gat', 'linear'] + algorithm: + name: darts + num_epochs: 200 + estimator: + name: oneshot +models: [] +trainer: + hp_space: + - maxValue: 300 + minValue: 100 + parameterName: max_epoch + scalingType: LINEAR + type: INTEGER + - maxValue: 30 + minValue: 10 + parameterName: early_stopping_round + scalingType: LINEAR + type: INTEGER + - maxValue: 0.05 + minValue: 0.01 + parameterName: lr + scalingType: LOG + type: DOUBLE + - maxValue: 0.0005 + minValue: 5.0e-05 + parameterName: weight_decay + scalingType: LOG + type: DOUBLE diff --git a/configs/nodeclf_nas_enas_benchmark.yml b/configs/nodeclf_nas_enas_benchmark.yml new file mode 100644 index 0000000..59d0767 --- /dev/null +++ b/configs/nodeclf_nas_enas_benchmark.yml @@ -0,0 +1,42 @@ +ensemble: + name: null +feature: +- name: PYGNormalizeFeatures +hpo: + max_evals: 10 + name: random +nas: + space: + name: singlepath + hidden_dim: 64 + layer_number: 2 + dropout: 0.8 + ops: ['gcn', 'gat', 'linear'] + algorithm: + name: enas + num_epochs: 200 + estimator: + name: oneshot +models: [] +trainer: + hp_space: + - maxValue: 300 + minValue: 100 + parameterName: max_epoch + scalingType: LINEAR + type: INTEGER + - maxValue: 30 + minValue: 10 + parameterName: early_stopping_round + scalingType: LINEAR + type: INTEGER + - maxValue: 0.05 + minValue: 0.01 + parameterName: lr + scalingType: LOG + type: DOUBLE + - maxValue: 0.0005 + minValue: 5.0e-05 + parameterName: weight_decay + scalingType: LOG + type: DOUBLE diff --git a/configs/nodeclf_nas_macro_benchmark.yml b/configs/nodeclf_nas_macro_benchmark.yml new file mode 100644 index 0000000..f6e9fc2 --- /dev/null +++ b/configs/nodeclf_nas_macro_benchmark.yml @@ -0,0 +1,40 @@ +ensemble: + name: null +feature: +- name: PYGNormalizeFeatures +hpo: + max_evals: 10 + name: random +nas: + space: + name: graphnasmacro + hidden_dim: 64 + layer_number: 2 + algorithm: + name: rl + num_epochs: 200 + estimator: + name: scratch +models: [] +trainer: + hp_space: + - maxValue: 300 + minValue: 100 + parameterName: max_epoch + scalingType: LINEAR + type: INTEGER + - maxValue: 30 + minValue: 10 + parameterName: early_stopping_round + scalingType: LINEAR + type: INTEGER + - maxValue: 0.05 + minValue: 0.01 + parameterName: lr + scalingType: LOG + type: DOUBLE + - maxValue: 0.0005 + minValue: 5.0e-05 + parameterName: weight_decay + scalingType: LOG + type: DOUBLE diff --git a/examples/test_nas.py b/examples/test_nas.py new file mode 100644 index 0000000..57dad32 --- /dev/null +++ b/examples/test_nas.py @@ -0,0 +1,22 @@ +import sys +sys.path.append('../') +from autogl.datasets import build_dataset_from_name +from autogl.solver import AutoNodeClassifier +from autogl.module.train import Acc +from autogl.solver.utils import set_seed +import argparse + +if __name__ == '__main__': + set_seed(202106) + parser = argparse.ArgumentParser() + parser.add_argument('--config', type=str, default='../configs/nodeclf_nas_darts_benchmark.yml') + parser.add_argument('--dataset', choices=['cora', 'citeseer', 'pubmed'], default='cora', type=str) + + args = parser.parse_args() + + dataset = build_dataset_from_name('cora') + solver = AutoNodeClassifier.from_config(args.config) + solver.fit(dataset) + solver.get_leaderboard().show() + out = solver.predict_proba() + print('acc on dataset', Acc.evaluate(out, dataset[0].y[dataset[0].test_mask].detach().numpy())) From c19b31e62565b78b17f90dcad6de0242a53f35e7 Mon Sep 17 00:00:00 2001 From: generall Date: Mon, 5 Jul 2021 10:58:46 +0800 Subject: [PATCH 2/3] use graphnas instead of rl --- configs/nodeclf_nas_benchmark.yml | 2 +- configs/nodeclf_nas_macro_benchmark.yml | 2 +- examples/test_nas.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/configs/nodeclf_nas_benchmark.yml b/configs/nodeclf_nas_benchmark.yml index bb757c8..dd52a7f 100644 --- a/configs/nodeclf_nas_benchmark.yml +++ b/configs/nodeclf_nas_benchmark.yml @@ -11,7 +11,7 @@ nas: hidden_dim: 64 layer_number: 4 algorithm: - name: rl + name: graphnas num_epochs: 200 estimator: name: scratch diff --git a/configs/nodeclf_nas_macro_benchmark.yml b/configs/nodeclf_nas_macro_benchmark.yml index f6e9fc2..2ddb04f 100644 --- a/configs/nodeclf_nas_macro_benchmark.yml +++ b/configs/nodeclf_nas_macro_benchmark.yml @@ -11,7 +11,7 @@ nas: hidden_dim: 64 layer_number: 2 algorithm: - name: rl + name: graphnas num_epochs: 200 estimator: name: scratch diff --git a/examples/test_nas.py b/examples/test_nas.py index 57dad32..0211403 100644 --- a/examples/test_nas.py +++ b/examples/test_nas.py @@ -9,7 +9,7 @@ import argparse if __name__ == '__main__': set_seed(202106) parser = argparse.ArgumentParser() - parser.add_argument('--config', type=str, default='../configs/nodeclf_nas_darts_benchmark.yml') + parser.add_argument('--config', type=str, default='../configs/nodeclf_nas_macro_benchmark.yml') parser.add_argument('--dataset', choices=['cora', 'citeseer', 'pubmed'], default='cora', type=str) args = parser.parse_args() From 89be0a29228b3aa3d948b558596d680d85424012 Mon Sep 17 00:00:00 2001 From: generall Date: Fri, 9 Jul 2021 09:31:57 +0800 Subject: [PATCH 3/3] fix graphnasmacro oom bug --- autogl/module/nas/estimator/train_scratch.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/autogl/module/nas/estimator/train_scratch.py b/autogl/module/nas/estimator/train_scratch.py index 6767ca3..2077b7a 100644 --- a/autogl/module/nas/estimator/train_scratch.py +++ b/autogl/module/nas/estimator/train_scratch.py @@ -16,6 +16,7 @@ class TrainEstimator(BaseEstimator): """ def __init__(self, loss_f = "nll_loss", evaluation = [Acc()]): super().__init__(loss_f, evaluation) + self.evaluation = evaluation self.estimator=OneShotEstimator(self.loss_f, self.evaluation) def infer(self, model: BaseSpace, dataset, mask="train"): @@ -34,6 +35,14 @@ class TrainEstimator(BaseEstimator): feval=self.evaluation, loss=self.loss_f, lr_scheduler_type=None) - self.trainer.train(dataset) - with torch.no_grad(): - return self.estimator.infer(boxmodel.model, dataset, mask) + try: + self.trainer.train(dataset) + with torch.no_grad(): + return self.estimator.infer(boxmodel.model, dataset, mask) + except RuntimeError as e: + if "cuda" in str(e) or "CUDA" in str(e): + INF = 100 + fin = [-INF if eva.is_higher_better else INF for eva in self.evaluation] + return fin, 0 + else: + raise e