|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- "MAO\n",
- "\n",
- "--- This is a classification problem ---\n",
- "\n",
- "\n",
- "1. Loading dataset from file...\n",
- "\n",
- "2. Calculating gram matrices. This could take a while...\n",
- "\n",
- " None edge weight specified. Set all weight to 1.\n",
- "\n",
- "getting shortest paths: 68it [00:00, 629.46it/s]\n",
- "calculating kernels: 2346it [00:22, 102.31it/s]\n",
- "\n",
- " --- shortest path kernel matrix of size 68 built in 23.390946626663208 seconds ---\n",
- "\n",
- "the gram matrix with parameters {'edge_kernels': {'symb': <function deltakernel at 0x7f90ea71dae8>, 'nsymb': <function gaussiankernel at 0x7f90ea71d620>, 'mix': functools.partial(<function kernelproduct at 0x7f90ea71d6a8>, <function deltakernel at 0x7f90ea71dae8>, <function gaussiankernel at 0x7f90ea71d620>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f90ea71dae8>, 'nsymb': <function gaussiankernel at 0x7f90ea71d620>, 'mix': functools.partial(<function kernelproduct at 0x7f90ea71d6a8>, <function deltakernel at 0x7f90ea71dae8>, <function gaussiankernel at 0x7f90ea71d620>)}, 'n_jobs': 8} is: \n",
- "\n",
- "1 gram matrices are calculated, 0 of which are ignored.\n",
- "\n",
- "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
- "cross validation: 0%| | 0/30 [00:00<?, ?it/s]0 0\n",
- "params_in: {'C': 1e-10}\n",
- "0 1\n",
- "params_in: {'C': 3.1622776601683795e-10}\n",
- "0 2\n",
- "params_in: {'C': 1e-09}\n",
- "0 3\n",
- "params_in: {'C': 3.1622776601683795e-09}\n",
- "0 4\n",
- "params_in: {'C': 1e-08}\n",
- "0 5\n",
- "params_in: {'C': 3.162277660168379e-08}\n",
- "0 6\n",
- "params_in: {'C': 1e-07}\n",
- "0 7\n",
- "params_in: {'C': 3.162277660168379e-07}\n",
- "0 8\n",
- "params_in: {'C': 1e-06}\n",
- "0 9\n",
- "params_in: {'C': 3.162277660168379e-06}\n",
- "0 10\n",
- "params_in: {'C': 1e-05}\n",
- "0 11\n",
- "params_in: {'C': 3.1622776601683795e-05}\n",
- "0 12\n",
- "params_in: {'C': 0.0001}\n",
- "0 13\n",
- "params_in: {'C': 0.00031622776601683794}\n",
- "0 14\n",
- "params_in: {'C': 0.001}\n",
- "0 15\n",
- "params_in: {'C': 0.0031622776601683794}\n",
- "0 16\n",
- "params_in: {'C': 0.01}\n",
- "0 17\n",
- "params_in: {'C': 0.03162277660168379}\n",
- "0 18\n",
- "params_in: {'C': 0.1}\n",
- "0 19\n",
- "params_in: {'C': 0.31622776601683794}\n",
- "0 20\n",
- "params_in: {'C': 1.0}\n",
- "0 21\n",
- "params_in: {'C': 3.1622776601683795}\n",
- "0 22\n",
- "params_in: {'C': 10.0}\n",
- "0 23\n",
- "params_in: {'C': 31.622776601683793}\n",
- "0 24\n",
- "params_in: {'C': 100.0}\n",
- "0 25\n",
- "params_in: {'C': 316.22776601683796}\n",
- "0 26\n",
- "params_in: {'C': 1000.0}\n",
- "0 27\n",
- "params_in: {'C': 3162.2776601683795}\n",
- "0 28\n",
- "params_in: {'C': 10000.0}\n",
- "0 29\n",
- "params_in: {'C': 31622.776601683792}\n",
- "0 30\n",
- "params_in: {'C': 100000.0}\n",
- "0 31\n",
- "params_in: {'C': 316227.7660168379}\n",
- "0 32\n",
- "params_in: {'C': 1000000.0}\n",
- "0 33\n",
- "params_in: {'C': 3162277.6601683795}\n",
- "0 34\n",
- "params_in: {'C': 10000000.0}\n",
- "0 35\n",
- "params_in: {'C': 31622776.60168379}\n",
- "0 36\n",
- "params_in: {'C': 100000000.0}\n",
- "0 37\n",
- "params_in: {'C': 316227766.01683795}\n",
- "0 38\n",
- "params_in: {'C': 1000000000.0}\n",
- "0 39\n",
- "params_in: {'C': 3162277660.1683793}\n",
- "0 40\n",
- "params_in: {'C': 10000000000.0}\n",
- "val_pref: [[0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
- " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
- " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
- " 0.59285714 0.59285714 0.55952381 0.71666667 0.81666667 0.81666667\n",
- " 0.83571429 0.86666667 0.9 0.9 0.9 0.9\n",
- " 0.9 0.9 0.9 0.9 0.9 0.9\n",
- " 0.9 0.9 0.9 0.9 0.9 ]]\n",
- "test_pref: [[0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
- " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
- " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
- " 0.28571429 0.28571429 0.61428571 0.84285714 0.84285714 0.85714286\n",
- " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
- " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
- " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286]]\n",
- "cross validation: 100%|██████████| 30/30 [00:11<00:00, 2.75it/s]\n",
- "\n",
- "\n",
- "4. Getting final performance...\n",
- "val_pref: [0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
- " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
- " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
- " 0.59285714 0.59285714 0.55952381 0.71666667 0.81666667 0.81666667\n",
- " 0.83571429 0.86666667 0.9 0.9 0.9 0.9\n",
- " 0.9 0.9 0.9 0.9 0.9 0.9\n",
- " 0.9 0.9 0.9 0.9 0.9 ]\n",
- "test_pref: [0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
- " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
- " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
- " 0.28571429 0.28571429 0.61428571 0.84285714 0.84285714 0.85714286\n",
- " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
- " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
- " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286]\n",
- "average_val_scores: [[0.55301587 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587\n",
- " 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587\n",
- " 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587\n",
- " 0.55301587 0.55468254 0.61507937 0.71777778 0.78039683 0.80531746\n",
- " 0.86198413 0.89531746 0.89420635 0.87190476 0.85761905 0.85761905\n",
- " 0.85761905 0.85761905 0.85761905 0.85761905 0.85761905 0.85761905\n",
- " 0.85761905 0.85761905 0.85761905 0.85761905 0.85761905]]\n",
- "best_val_perf: 0.8953174603174604\n",
- "\n",
- "best_params_out: [{'edge_kernels': {'symb': <function deltakernel at 0x7f90ea71dae8>, 'nsymb': <function gaussiankernel at 0x7f90ea71d620>, 'mix': functools.partial(<function kernelproduct at 0x7f90ea71d6a8>, <function deltakernel at 0x7f90ea71dae8>, <function gaussiankernel at 0x7f90ea71d620>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f90ea71dae8>, 'nsymb': <function gaussiankernel at 0x7f90ea71d620>, 'mix': functools.partial(<function kernelproduct at 0x7f90ea71d6a8>, <function deltakernel at 0x7f90ea71dae8>, <function gaussiankernel at 0x7f90ea71d620>)}, 'n_jobs': 8}]\n",
- "best_params_in: [{'C': 316.22776601683796}]\n",
- "\n",
- "best_val_perf: 0.8953174603174604\n",
- "best_val_std: 0.029090007386146643\n",
- "(array([0]), array([25]))\n",
- "[0]\n",
- "[[0.5047619 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619\n",
- " 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619\n",
- " 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619\n",
- " 0.5047619 0.49761905 0.66 0.75857143 0.78857143 0.82857143\n",
- " 0.85285714 0.86380952 0.84428571 0.82190476 0.81571429 0.81571429\n",
- " 0.81571429 0.81571429 0.81571429 0.81571429 0.81571429 0.81571429\n",
- " 0.81571429 0.81571429 0.81571429 0.81571429 0.81571429]]\n",
- "final_performance: [0.8638095238095236]\n",
- "final_confidence: [0.10509426306201483]\n",
- "train_performance: [0.9857934904601572]\n",
- "train_std: [0.00730576290039335]\n",
- "\n",
- "time to calculate gram matrix with different hyper-params: 23.39±nans\n",
- "time to calculate best gram matrix: 23.39±nans\n",
- "total training time with all hyper-param choices: 34.88s\n",
- "\n",
- "\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:140: RuntimeWarning: Degrees of freedom <= 0 for slice\n",
- " keepdims=keepdims)\n",
- "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:132: RuntimeWarning: invalid value encountered in double_scalars\n",
- " ret = ret.dtype.type(ret / rcount)\n"
- ]
- }
- ],
- "source": [
- "#!/usr/bin/env python3\n",
- "# -*- coding: utf-8 -*-\n",
- "\"\"\"\n",
- "Created on Fri Sep 28 16:37:29 2018\n",
- "\n",
- "@author: ljia\n",
- "\"\"\"\n",
- "\n",
- "import functools\n",
- "from libs import *\n",
- "import multiprocessing\n",
- "\n",
- "from pygraph.kernels.structuralspKernel import structuralspkernel\n",
- "from pygraph.utils.kernels import deltakernel, gaussiankernel, kernelproduct\n",
- "\n",
- "dslist = [\n",
- "# {'name': 'Acyclic', 'dataset': '../datasets/acyclic/dataset_bps.ds',\n",
- "# 'task': 'regression'}, # node symb\n",
- "# {'name': 'Alkane', 'dataset': '../datasets/Alkane/dataset.ds', 'task': 'regression',\n",
- "# 'dataset_y': '../datasets/Alkane/dataset_boiling_point_names.txt', }, \n",
- "# # contains single node graph, node symb\n",
- " {'name': 'MAO', 'dataset': '../datasets/MAO/dataset.ds', }, # node/edge symb\n",
- "# {'name': 'PAH', 'dataset': '../datasets/PAH/dataset.ds', }, # unlabeled\n",
- "# {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG.mat',\n",
- "# 'extra_params': {'am_sp_al_nl_el': [0, 0, 3, 1, 2]}}, # node/edge symb\n",
- "# {'name': 'Letter-med', 'dataset': '../datasets/Letter-med/Letter-med_A.txt'},\n",
- " # node nsymb\n",
- "# {'name': 'ENZYMES', 'dataset': '../datasets/ENZYMES_txt/ENZYMES_A_sparse.txt'},\n",
- "# # node symb/nsymb\n",
- "# {'name': 'Mutagenicity', 'dataset': '../datasets/Mutagenicity/Mutagenicity_A.txt'},\n",
- "# # node/edge symb\n",
- "# {'name': 'D&D', 'dataset': '../datasets/D&D/DD.mat',\n",
- "# 'extra_params': {'am_sp_al_nl_el': [0, 1, 2, 1, -1]}}, # node symb\n",
- "\n",
- " # {'name': 'COIL-DEL', 'dataset': '../datasets/COIL-DEL/COIL-DEL_A.txt'}, # edge symb, node nsymb\n",
- " # # # {'name': 'BZR', 'dataset': '../datasets/BZR_txt/BZR_A_sparse.txt'}, # node symb/nsymb\n",
- " # # # {'name': 'COX2', 'dataset': '../datasets/COX2_txt/COX2_A_sparse.txt'}, # node symb/nsymb\n",
- " # {'name': 'Fingerprint', 'dataset': '../datasets/Fingerprint/Fingerprint_A.txt'},\n",
- " #\n",
- " # # {'name': 'DHFR', 'dataset': '../datasets/DHFR_txt/DHFR_A_sparse.txt'}, # node symb/nsymb\n",
- " # # {'name': 'SYNTHETIC', 'dataset': '../datasets/SYNTHETIC_txt/SYNTHETIC_A_sparse.txt'}, # node symb/nsymb\n",
- "# {'name': 'MSRC9', 'dataset': '../datasets/MSRC_9_txt/MSRC_9_A.txt'}, # node symb, missing values\n",
- "# {'name': 'MSRC21', 'dataset': '../datasets/MSRC_21_txt/MSRC_21_A.txt'}, # node symb, missing values\n",
- " # # {'name': 'FIRSTMM_DB', 'dataset': '../datasets/FIRSTMM_DB/FIRSTMM_DB_A.txt'}, # node symb/nsymb ,edge nsymb\n",
- "\n",
- " # # {'name': 'PROTEINS', 'dataset': '../datasets/PROTEINS_txt/PROTEINS_A_sparse.txt'}, # node symb/nsymb\n",
- " # # {'name': 'PROTEINS_full', 'dataset': '../datasets/PROTEINS_full_txt/PROTEINS_full_A_sparse.txt'}, # node symb/nsymb\n",
- " # # {'name': 'AIDS', 'dataset': '../datasets/AIDS/AIDS_A.txt'}, # node symb/nsymb, edge symb\n",
- " # {'name': 'NCI1', 'dataset': '../datasets/NCI1/NCI1.mat',\n",
- " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
- " # {'name': 'NCI109', 'dataset': '../datasets/NCI109/NCI109.mat',\n",
- " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
- " # {'name': 'NCI-HIV', 'dataset': '../datasets/NCI-HIV/AIDO99SD.sdf',\n",
- " # 'dataset_y': '../datasets/NCI-HIV/aids_conc_may04.txt',}, # node/edge symb\n",
- "\n",
- "# # not working below\n",
- "# {'name': 'PTC_FM', 'dataset': '../datasets/PTC/Train/FM.ds',},\n",
- " # {'name': 'PTC_FR', 'dataset': '../datasets/PTC/Train/FR.ds',},\n",
- " # {'name': 'PTC_MM', 'dataset': '../datasets/PTC/Train/MM.ds',},\n",
- " # {'name': 'PTC_MR', 'dataset': '../datasets/PTC/Train/MR.ds',},\n",
- "]\n",
- "estimator = structuralspkernel\n",
- "mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
- "param_grid_precomputed = {'node_kernels': \n",
- " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
- " 'edge_kernels': \n",
- " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}]}\n",
- "param_grid = [{'C': np.logspace(-10, 10, num=41, base=10)},\n",
- " {'alpha': np.logspace(-10, 10, num=41, base=10)}]\n",
- "\n",
- "for ds in dslist:\n",
- " print()\n",
- " print(ds['name'])\n",
- " model_selection_for_precomputed_kernel(\n",
- " ds['dataset'],\n",
- " estimator,\n",
- " param_grid_precomputed,\n",
- " (param_grid[1] if ('task' in ds and ds['task']\n",
- " == 'regression') else param_grid[0]),\n",
- " (ds['task'] if 'task' in ds else 'classification'),\n",
- " NUM_TRIALS=30,\n",
- " datafile_y=(ds['dataset_y'] if 'dataset_y' in ds else None),\n",
- " extra_params=(ds['extra_params'] if 'extra_params' in ds else None),\n",
- " ds_name=ds['name'],\n",
- " n_jobs=multiprocessing.cpu_count(),\n",
- " read_gm_from_file=False)\n",
- " print()"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.6"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|