Commit 785ee6af authored by ZHENQIN WU's avatar ZHENQIN WU
Browse files

Merge remote-tracking branch 'remotes/mine/GPGO' into MPNN

parents 24437fc5 56d66761
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ class GaussianProcessHyperparamOpt(HyperparamOpt):
                        search_range=4,
                        hp_invalid_list=[
                            'seed', 'nb_epoch', 'penalty_type', 'dropouts',
                            'bypass_dropouts', 'n_pair_feat'
                            'bypass_dropouts', 'n_pair_feat', 'fit_transformers'
                        ],
                        logdir=None):
    """Perform hyperparams search using a gaussian process assumption
@@ -97,7 +97,9 @@ class GaussianProcessHyperparamOpt(HyperparamOpt):
                        if hp_list_class[i] is list]

    # Number of parameters
    n_param = len(hp_list_single + sum([hp[1] for hp in hp_list_multiple]))
    n_param = len(hp_list_single)
    if len(hp_list_multiple) > 0:
      n_param = n_param + sum([hp[1] for hp in hp_list_multiple])
    # Range of optimization
    param_range = []
    for hp in hp_list_single:
+29 −1
Original line number Diff line number Diff line
@@ -14,8 +14,10 @@ import csv
import numpy as np
import tensorflow as tf
import deepchem
import pickle
from deepchem.molnet.run_benchmark_models import benchmark_classification, benchmark_regression
from deepchem.molnet.check_availability import CheckFeaturizer, CheckSplit
from deepchem.molnet.preset_hyper_parameters import hps


def run_benchmark(datasets,
@@ -26,6 +28,9 @@ def run_benchmark(datasets,
                  n_features=0,
                  out_path='.',
                  hyper_parameters=None,
                  hyper_param_search=False,
                  max_iter=20,
                  search_range=2,
                  test=False,
                  reload=True,
                  seed=123):
@@ -57,6 +62,13 @@ def run_benchmark(datasets,
      path of result file
  hyper_parameters: dict, optional (default=None)
      hyper parameters for designated model, None = use preset values
  hyper_param_search: bool, optional(default=False)
      whether to perform hyper parameter search, using gaussian process by default
  max_iter: int, optional(default=20)
      number of optimization trials
  search_range: int(float), optional(default=4)
      optimization on [initial values / search_range,
                       initial values * search_range]
  test: boolean, optional(default=False)
      whether to evaluate on test set
  reload: boolean, optional(default=True)
@@ -142,6 +154,21 @@ def run_benchmark(datasets,
    valid_score = {}
    test_score = {}

    if hyper_param_search:
      if hyper_parameters is None:
        hyper_parameters = hps[model]
      search_mode = deepchem.hyper.GaussianProcessHyperparamOpt(model)
      hyper_param_opt, _ = search_mode.hyperparam_search(
          hyper_parameters,
          train_dataset,
          valid_dataset,
          transformers,
          metric,
          n_features=n_features,
          n_tasks=len(tasks),
          max_iter=max_iter,
          search_range=search_range)
      hyper_parameters = hyper_param_opt
    if isinstance(model, str):
      if mode == 'classification':
        train_score, valid_score, test_score = benchmark_classification(
@@ -195,7 +222,8 @@ def run_benchmark(datasets,
        output_line.extend(
            ['time_for_running', time_finish_fitting - time_start_fitting])
        writer.writerow(output_line)

    with open(os.path.join(out_path, dataset + model + '.pkl'), 'w') as f:
      pickle.dump(hyper_parameters, f)

#
# Note by @XericZephyr. Reason why I spun off this function:
+26 −7
Original line number Diff line number Diff line
@@ -88,23 +88,42 @@ else:
  seed = 123

if len(splitters) == 0:
  splitters = ['index', 'random', 'scaffold']
  splitters = ['random']
if len(models) == 0:
  models = [
      'tf', 'tf_robust', 'logreg', 'graphconv', 'tf_regression',
      'tf_regression_ft', 'graphconvreg'
      'tf', 'tf_robust', 'logreg', 'graphconv', 'irv', 'tf_regression',
      'tf_regression_ft', 'graphconvreg', 'weave', 'weave_regression', 'dtnn'
  ]
  #irv, rf, rf_regression should be assigned manually
if len(datasets) == 0:
  datasets = [
      'bace_c', 'bace_r', 'bbbp', 'clearance', 'clintox', 'delaney', 'hiv',
      'hopv', 'lipo', 'muv', 'pdbbind', 'ppb', 'qm7b', 'qm8', 'qm9', 'sampl',
      'sider', 'tox21', 'toxcast'
      'clintox', 'delaney', 'lipo', 'qm7b', 'qm8', 'sampl',
      'sider', 'tox21', 'toxcast', 'muv'
  ]

metrics = {
    'qm7': [dc.metrics.Metric(dc.metrics.mean_absolute_error, np.mean, mode='regression')],
    'qm7b': [dc.metrics.Metric(dc.metrics.mean_absolute_error, np.mean, mode='regression')],
    'qm8': [dc.metrics.Metric(dc.metrics.mean_absolute_error, np.mean, mode='regression')],
    'qm9': [dc.metrics.Metric(dc.metrics.mean_absolute_error, np.mean, mode='regression')],
    'delaney': [dc.metrics.Metric(dc.metrics.rms_score, np.mean, mode='regression')],
    'sampl': [dc.metrics.Metric(dc.metrics.rms_score, np.mean, mode='regression')],
    'lipo': [dc.metrics.Metric(dc.metrics.rms_score, np.mean, mode='regression')],
    'pdbbind': [dc.metrics.Metric(dc.metrics.rms_score, np.mean, mode='regression')],
    'pcba': [dc.metrics.Metric(dc.metrics.prc_auc_score, np.mean, mode='classification')],
    'muv': [dc.metrics.Metric(dc.metrics.prc_auc_score, np.mean, mode='classification')],
    'hiv': [dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean, mode='classification')],
    'tox21': [dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean, mode='classification')],
    'toxcast': [dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean, mode='classification')],
    'sider': [dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean, mode='classification')],
    'clintox': [dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean, mode='classification')],
    'bace_c': [dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean, mode='classification')],
    'bbbp': [dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean, mode='classification')]
    }
for dataset in datasets:
  for split in splitters:
    for model in models:
      np.random.seed(seed)
      dc.molnet.run_benchmark(
          [dataset], str(model), split=split, test=test, seed=seed)
          [dataset], str(model), split=split, metric=metrics[dataset],
          hyper_param_search=True, test=False, seed=seed)