From 43f4989b4f84323283366110e964976559120783 Mon Sep 17 00:00:00 2001 From: natsukium Date: Fri, 22 Mar 2019 10:27:21 +0900 Subject: [PATCH] Fix examples --- examples/own_dataset/train_own_dataset.py | 65 +---------------------- examples/qm9/train_qm9.py | 61 +-------------------- examples/tox21/predictor.py | 57 -------------------- examples/tox21/train_tox21.py | 4 +- 4 files changed, 5 insertions(+), 182 deletions(-) delete mode 100644 examples/tox21/predictor.py diff --git a/examples/own_dataset/train_own_dataset.py b/examples/own_dataset/train_own_dataset.py index 61061f89..94b8ed6f 100644 --- a/examples/own_dataset/train_own_dataset.py +++ b/examples/own_dataset/train_own_dataset.py @@ -21,9 +21,8 @@ from chainer_chemistry.dataset.parsers import CSVFileParser from chainer_chemistry.dataset.preprocessors import preprocess_method_dict from chainer_chemistry.datasets import NumpyTupleDataset -from chainer_chemistry.models import ( - MLP, NFP, GGNN, SchNet, WeaveNet, RSGCN, RelGCN, RelGAT, Regressor) -from chainer_chemistry.models.prediction import GraphConvPredictor +from chainer_chemistry.models import Regressor +from chainer_chemistry.models.prediction import set_up_predictor class MeanAbsError(object): @@ -95,66 +94,6 @@ def __call__(self, x0, x1): return numpy.mean(numpy.absolute(diff), axis=0)[0] -def set_up_predictor(method, n_unit, conv_layers, class_num): - """Sets up the graph convolution network predictor. - - Args: - method: Method name. Currently, the supported ones are `nfp`, `ggnn`, - `schnet`, `weavenet` and `rsgcn`. - n_unit: Number of hidden units. - conv_layers: Number of convolutional layers for the graph convolution - network. - class_num: Number of output classes. - - Returns: - An instance of the selected predictor. - """ - - predictor = None - mlp = MLP(out_dim=class_num, hidden_dim=n_unit) - - if method == 'nfp': - print('Training an NFP predictor...') - nfp = NFP(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers) - predictor = GraphConvPredictor(nfp, mlp) - elif method == 'ggnn': - print('Training a GGNN predictor...') - ggnn = GGNN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers) - predictor = GraphConvPredictor(ggnn, mlp) - elif method == 'schnet': - print('Training an SchNet predictor...') - schnet = SchNet(out_dim=class_num, hidden_dim=n_unit, - n_layers=conv_layers) - predictor = GraphConvPredictor(schnet, None) - elif method == 'weavenet': - print('Training a WeaveNet predictor...') - n_atom = 20 - n_sub_layer = 1 - weave_channels = [50] * conv_layers - - weavenet = WeaveNet(weave_channels=weave_channels, hidden_dim=n_unit, - n_sub_layer=n_sub_layer, n_atom=n_atom) - predictor = GraphConvPredictor(weavenet, mlp) - elif method == 'rsgcn': - print('Training an RSGCN predictor...') - rsgcn = RSGCN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers) - predictor = GraphConvPredictor(rsgcn, mlp) - elif method == 'relgcn': - print('Training an RelGCN predictor...') - num_edge_type = 4 - relgcn = RelGCN(out_channels=n_unit, num_edge_type=num_edge_type, - scale_adj=True) - predictor = GraphConvPredictor(relgcn, mlp) - elif method == 'relgat': - print('Training an RelGAT predictor...') - relgat = RelGAT(out_dim=n_unit, hidden_dim=n_unit, - n_layers=conv_layers) - predictor = GraphConvPredictor(relgat, mlp) - else: - raise ValueError('[ERROR] Invalid method: {}'.format(method)) - return predictor - - def parse_arguments(): # Lists of supported preprocessing methods/models. method_list = ['nfp', 'ggnn', 'schnet', 'weavenet', 'rsgcn', 'relgcn', diff --git a/examples/qm9/train_qm9.py b/examples/qm9/train_qm9.py index 236e8035..c743d1fe 100644 --- a/examples/qm9/train_qm9.py +++ b/examples/qm9/train_qm9.py @@ -17,10 +17,8 @@ from chainer_chemistry import datasets as D from chainer_chemistry.datasets import NumpyTupleDataset from chainer_chemistry.links.scaler.standard_scaler import StandardScaler -from chainer_chemistry.models import ( - MLP, NFP, GGNN, SchNet, WeaveNet, RSGCN, RelGCN, RelGAT) from chainer_chemistry.models.prediction import Regressor -from chainer_chemistry.models.prediction import GraphConvPredictor +from chainer_chemistry.models.prediction import set_up_predictor class MeanAbsError(object): @@ -98,63 +96,6 @@ def parse_arguments(): return parser.parse_args() -def set_up_predictor(method, n_unit, conv_layers, class_num, scaler): - """Sets up the predictor, consisting of a graph convolution network and - a multilayer perceptron. - - Args: - method (str): Method name. - n_unit (int): Number of hidden units. - conv_layers (int): Number of convolutional layers for the graph - convolution network. - class_num (int): Number of output classes. - Returns: - predictor (chainer.Chain): An instance of the selected predictor. - """ - mlp = MLP(out_dim=class_num, hidden_dim=n_unit) - - if method == 'nfp': - print('Training an NFP predictor...') - nfp = NFP(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers) - predictor = GraphConvPredictor(nfp, mlp, scaler) - elif method == 'ggnn': - print('Training a GGNN predictor...') - ggnn = GGNN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers) - predictor = GraphConvPredictor(ggnn, mlp, scaler) - elif method == 'schnet': - print('Training an SchNet predictor...') - schnet = SchNet(out_dim=class_num, hidden_dim=n_unit, - n_layers=conv_layers) - predictor = GraphConvPredictor(schnet, None, scaler) - elif method == 'weavenet': - print('Training a WeaveNet predictor...') - n_atom = 20 - n_sub_layer = 1 - weave_channels = [50] * conv_layers - - weavenet = WeaveNet(weave_channels=weave_channels, hidden_dim=n_unit, - n_sub_layer=n_sub_layer, n_atom=n_atom) - predictor = GraphConvPredictor(weavenet, mlp, scaler) - elif method == 'rsgcn': - print('Training an RSGCN predictor...') - rsgcn = RSGCN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers) - predictor = GraphConvPredictor(rsgcn, mlp, scaler) - elif method == 'relgcn': - print('Use Relational GCN predictor...') - num_edge_type = 4 - relgcn = RelGCN(out_channels=n_unit, num_edge_type=num_edge_type, - scale_adj=True) - predictor = GraphConvPredictor(relgcn, mlp, scaler) - elif method == 'relgat': - print('Train Relational GAT predictor...') - relgat = RelGAT(out_dim=n_unit, hidden_dim=n_unit, - n_layers=conv_layers) - predictor = GraphConvPredictor(relgat, mlp, scaler) - else: - raise ValueError('[ERROR] Invalid method: {}'.format(method)) - return predictor - - def main(): # Parse the arguments. args = parse_arguments() diff --git a/examples/tox21/predictor.py b/examples/tox21/predictor.py deleted file mode 100644 index 239c7f48..00000000 --- a/examples/tox21/predictor.py +++ /dev/null @@ -1,57 +0,0 @@ -from chainer_chemistry.models import GGNN -from chainer_chemistry.models import MLP -from chainer_chemistry.models import NFP -from chainer_chemistry.models import RSGCN -from chainer_chemistry.models import SchNet -from chainer_chemistry.models import WeaveNet -from chainer_chemistry.models import RelGCN -from chainer_chemistry.models import RelGAT -from chainer_chemistry.models.prediction import GraphConvPredictor - - -def build_predictor(method, n_unit, conv_layers, class_num): - if method == 'nfp': - print('Use NFP predictor...') - predictor = GraphConvPredictor( - NFP(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers), - MLP(out_dim=class_num, hidden_dim=n_unit)) - elif method == 'ggnn': - print('Use GGNN predictor...') - predictor = GraphConvPredictor( - GGNN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers), - MLP(out_dim=class_num, hidden_dim=n_unit)) - elif method == 'schnet': - print('Use SchNet predictor...') - # MLP layer is not necessary for SchNet - predictor = GraphConvPredictor( - SchNet(out_dim=class_num, hidden_dim=n_unit, n_layers=conv_layers, - readout_hidden_dim=n_unit), None) - elif method == 'weavenet': - print('Use WeaveNet predictor...') - n_atom = 20 - n_sub_layer = 1 - weave_channels = [50] * conv_layers - predictor = GraphConvPredictor( - WeaveNet(weave_channels=weave_channels, hidden_dim=n_unit, - n_sub_layer=n_sub_layer, n_atom=n_atom), - MLP(out_dim=class_num, hidden_dim=n_unit)) - elif method == 'rsgcn': - print('Use RSGCN predictor...') - predictor = GraphConvPredictor( - RSGCN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers), - MLP(out_dim=class_num, hidden_dim=n_unit)) - elif method == 'relgcn': - print('Use Relational GCN predictor...') - num_edge_type = 4 - predictor = GraphConvPredictor( - RelGCN(out_channels=n_unit, num_edge_type=num_edge_type, - scale_adj=True), - MLP(out_dim=class_num, hidden_dim=n_unit)) - elif method == 'relgat': - print('Use GAT predictor...') - predictor = GraphConvPredictor( - RelGAT(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers), - MLP(out_dim=class_num, hidden_dim=n_unit)) - else: - raise ValueError('[ERROR] Invalid predictor: method={}'.format(method)) - return predictor diff --git a/examples/tox21/train_tox21.py b/examples/tox21/train_tox21.py index 90ba2239..922ff19d 100644 --- a/examples/tox21/train_tox21.py +++ b/examples/tox21/train_tox21.py @@ -20,10 +20,10 @@ from chainer_chemistry import datasets as D from chainer_chemistry.iterators.balanced_serial_iterator import BalancedSerialIterator # NOQA from chainer_chemistry.models.prediction import Classifier +from chainer_chemistry.models.prediction import set_up_predictor from chainer_chemistry.training.extensions import ROCAUCEvaluator # NOQA import data -import predictor # Disable errors by RDKit occurred in preprocessing Tox21 dataset. lg = RDLogger.logger() @@ -94,7 +94,7 @@ def main(): train, val, _ = data.load_dataset(method, labels, num_data=args.num_data) # Network - predictor_ = predictor.build_predictor( + predictor_ = set_up_predictor( method, args.unit_num, args.conv_layers, class_num) iterator_type = args.iterator_type