You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

run_structuralspkernel.ipynb 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "scrolled": false
  8. },
  9. "outputs": [
  10. {
  11. "name": "stdout",
  12. "output_type": "stream",
  13. "text": [
  14. "\n",
  15. "MAO\n",
  16. "\n",
  17. "--- This is a classification problem ---\n",
  18. "\n",
  19. "\n",
  20. "1. Loading dataset from file...\n",
  21. "\n",
  22. "2. Calculating gram matrices. This could take a while...\n",
  23. "\n",
  24. " None edge weight specified. Set all weight to 1.\n",
  25. "\n",
  26. "getting shortest paths: 68it [00:00, 629.46it/s]\n",
  27. "calculating kernels: 2346it [00:22, 102.31it/s]\n",
  28. "\n",
  29. " --- shortest path kernel matrix of size 68 built in 23.390946626663208 seconds ---\n",
  30. "\n",
  31. "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",
  32. "\n",
  33. "1 gram matrices are calculated, 0 of which are ignored.\n",
  34. "\n",
  35. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  36. "cross validation: 0%| | 0/30 [00:00<?, ?it/s]0 0\n",
  37. "params_in: {'C': 1e-10}\n",
  38. "0 1\n",
  39. "params_in: {'C': 3.1622776601683795e-10}\n",
  40. "0 2\n",
  41. "params_in: {'C': 1e-09}\n",
  42. "0 3\n",
  43. "params_in: {'C': 3.1622776601683795e-09}\n",
  44. "0 4\n",
  45. "params_in: {'C': 1e-08}\n",
  46. "0 5\n",
  47. "params_in: {'C': 3.162277660168379e-08}\n",
  48. "0 6\n",
  49. "params_in: {'C': 1e-07}\n",
  50. "0 7\n",
  51. "params_in: {'C': 3.162277660168379e-07}\n",
  52. "0 8\n",
  53. "params_in: {'C': 1e-06}\n",
  54. "0 9\n",
  55. "params_in: {'C': 3.162277660168379e-06}\n",
  56. "0 10\n",
  57. "params_in: {'C': 1e-05}\n",
  58. "0 11\n",
  59. "params_in: {'C': 3.1622776601683795e-05}\n",
  60. "0 12\n",
  61. "params_in: {'C': 0.0001}\n",
  62. "0 13\n",
  63. "params_in: {'C': 0.00031622776601683794}\n",
  64. "0 14\n",
  65. "params_in: {'C': 0.001}\n",
  66. "0 15\n",
  67. "params_in: {'C': 0.0031622776601683794}\n",
  68. "0 16\n",
  69. "params_in: {'C': 0.01}\n",
  70. "0 17\n",
  71. "params_in: {'C': 0.03162277660168379}\n",
  72. "0 18\n",
  73. "params_in: {'C': 0.1}\n",
  74. "0 19\n",
  75. "params_in: {'C': 0.31622776601683794}\n",
  76. "0 20\n",
  77. "params_in: {'C': 1.0}\n",
  78. "0 21\n",
  79. "params_in: {'C': 3.1622776601683795}\n",
  80. "0 22\n",
  81. "params_in: {'C': 10.0}\n",
  82. "0 23\n",
  83. "params_in: {'C': 31.622776601683793}\n",
  84. "0 24\n",
  85. "params_in: {'C': 100.0}\n",
  86. "0 25\n",
  87. "params_in: {'C': 316.22776601683796}\n",
  88. "0 26\n",
  89. "params_in: {'C': 1000.0}\n",
  90. "0 27\n",
  91. "params_in: {'C': 3162.2776601683795}\n",
  92. "0 28\n",
  93. "params_in: {'C': 10000.0}\n",
  94. "0 29\n",
  95. "params_in: {'C': 31622.776601683792}\n",
  96. "0 30\n",
  97. "params_in: {'C': 100000.0}\n",
  98. "0 31\n",
  99. "params_in: {'C': 316227.7660168379}\n",
  100. "0 32\n",
  101. "params_in: {'C': 1000000.0}\n",
  102. "0 33\n",
  103. "params_in: {'C': 3162277.6601683795}\n",
  104. "0 34\n",
  105. "params_in: {'C': 10000000.0}\n",
  106. "0 35\n",
  107. "params_in: {'C': 31622776.60168379}\n",
  108. "0 36\n",
  109. "params_in: {'C': 100000000.0}\n",
  110. "0 37\n",
  111. "params_in: {'C': 316227766.01683795}\n",
  112. "0 38\n",
  113. "params_in: {'C': 1000000000.0}\n",
  114. "0 39\n",
  115. "params_in: {'C': 3162277660.1683793}\n",
  116. "0 40\n",
  117. "params_in: {'C': 10000000000.0}\n",
  118. "val_pref: [[0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
  119. " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
  120. " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
  121. " 0.59285714 0.59285714 0.55952381 0.71666667 0.81666667 0.81666667\n",
  122. " 0.83571429 0.86666667 0.9 0.9 0.9 0.9\n",
  123. " 0.9 0.9 0.9 0.9 0.9 0.9\n",
  124. " 0.9 0.9 0.9 0.9 0.9 ]]\n",
  125. "test_pref: [[0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
  126. " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
  127. " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
  128. " 0.28571429 0.28571429 0.61428571 0.84285714 0.84285714 0.85714286\n",
  129. " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
  130. " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
  131. " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286]]\n",
  132. "cross validation: 100%|██████████| 30/30 [00:11<00:00, 2.75it/s]\n",
  133. "\n",
  134. "\n",
  135. "4. Getting final performance...\n",
  136. "val_pref: [0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
  137. " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
  138. " 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714 0.59285714\n",
  139. " 0.59285714 0.59285714 0.55952381 0.71666667 0.81666667 0.81666667\n",
  140. " 0.83571429 0.86666667 0.9 0.9 0.9 0.9\n",
  141. " 0.9 0.9 0.9 0.9 0.9 0.9\n",
  142. " 0.9 0.9 0.9 0.9 0.9 ]\n",
  143. "test_pref: [0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
  144. " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
  145. " 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429 0.28571429\n",
  146. " 0.28571429 0.28571429 0.61428571 0.84285714 0.84285714 0.85714286\n",
  147. " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
  148. " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286\n",
  149. " 0.85714286 0.85714286 0.85714286 0.85714286 0.85714286]\n",
  150. "average_val_scores: [[0.55301587 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587\n",
  151. " 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587\n",
  152. " 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587 0.55301587\n",
  153. " 0.55301587 0.55468254 0.61507937 0.71777778 0.78039683 0.80531746\n",
  154. " 0.86198413 0.89531746 0.89420635 0.87190476 0.85761905 0.85761905\n",
  155. " 0.85761905 0.85761905 0.85761905 0.85761905 0.85761905 0.85761905\n",
  156. " 0.85761905 0.85761905 0.85761905 0.85761905 0.85761905]]\n",
  157. "best_val_perf: 0.8953174603174604\n",
  158. "\n",
  159. "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",
  160. "best_params_in: [{'C': 316.22776601683796}]\n",
  161. "\n",
  162. "best_val_perf: 0.8953174603174604\n",
  163. "best_val_std: 0.029090007386146643\n",
  164. "(array([0]), array([25]))\n",
  165. "[0]\n",
  166. "[[0.5047619 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619\n",
  167. " 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619\n",
  168. " 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619 0.5047619\n",
  169. " 0.5047619 0.49761905 0.66 0.75857143 0.78857143 0.82857143\n",
  170. " 0.85285714 0.86380952 0.84428571 0.82190476 0.81571429 0.81571429\n",
  171. " 0.81571429 0.81571429 0.81571429 0.81571429 0.81571429 0.81571429\n",
  172. " 0.81571429 0.81571429 0.81571429 0.81571429 0.81571429]]\n",
  173. "final_performance: [0.8638095238095236]\n",
  174. "final_confidence: [0.10509426306201483]\n",
  175. "train_performance: [0.9857934904601572]\n",
  176. "train_std: [0.00730576290039335]\n",
  177. "\n",
  178. "time to calculate gram matrix with different hyper-params: 23.39±nans\n",
  179. "time to calculate best gram matrix: 23.39±nans\n",
  180. "total training time with all hyper-param choices: 34.88s\n",
  181. "\n",
  182. "\n"
  183. ]
  184. },
  185. {
  186. "name": "stderr",
  187. "output_type": "stream",
  188. "text": [
  189. "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:140: RuntimeWarning: Degrees of freedom <= 0 for slice\n",
  190. " keepdims=keepdims)\n",
  191. "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:132: RuntimeWarning: invalid value encountered in double_scalars\n",
  192. " ret = ret.dtype.type(ret / rcount)\n"
  193. ]
  194. }
  195. ],
  196. "source": [
  197. "#!/usr/bin/env python3\n",
  198. "# -*- coding: utf-8 -*-\n",
  199. "\"\"\"\n",
  200. "Created on Fri Sep 28 16:37:29 2018\n",
  201. "\n",
  202. "@author: ljia\n",
  203. "\"\"\"\n",
  204. "\n",
  205. "import functools\n",
  206. "from libs import *\n",
  207. "import multiprocessing\n",
  208. "\n",
  209. "from pygraph.kernels.structuralspKernel import structuralspkernel\n",
  210. "from pygraph.utils.kernels import deltakernel, gaussiankernel, kernelproduct\n",
  211. "\n",
  212. "dslist = [\n",
  213. "# {'name': 'Acyclic', 'dataset': '../datasets/acyclic/dataset_bps.ds',\n",
  214. "# 'task': 'regression'}, # node symb\n",
  215. "# {'name': 'Alkane', 'dataset': '../datasets/Alkane/dataset.ds', 'task': 'regression',\n",
  216. "# 'dataset_y': '../datasets/Alkane/dataset_boiling_point_names.txt', }, \n",
  217. "# # contains single node graph, node symb\n",
  218. " {'name': 'MAO', 'dataset': '../datasets/MAO/dataset.ds', }, # node/edge symb\n",
  219. "# {'name': 'PAH', 'dataset': '../datasets/PAH/dataset.ds', }, # unlabeled\n",
  220. "# {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG.mat',\n",
  221. "# 'extra_params': {'am_sp_al_nl_el': [0, 0, 3, 1, 2]}}, # node/edge symb\n",
  222. "# {'name': 'Letter-med', 'dataset': '../datasets/Letter-med/Letter-med_A.txt'},\n",
  223. " # node nsymb\n",
  224. "# {'name': 'ENZYMES', 'dataset': '../datasets/ENZYMES_txt/ENZYMES_A_sparse.txt'},\n",
  225. "# # node symb/nsymb\n",
  226. "# {'name': 'Mutagenicity', 'dataset': '../datasets/Mutagenicity/Mutagenicity_A.txt'},\n",
  227. "# # node/edge symb\n",
  228. "# {'name': 'D&D', 'dataset': '../datasets/D&D/DD.mat',\n",
  229. "# 'extra_params': {'am_sp_al_nl_el': [0, 1, 2, 1, -1]}}, # node symb\n",
  230. "\n",
  231. " # {'name': 'COIL-DEL', 'dataset': '../datasets/COIL-DEL/COIL-DEL_A.txt'}, # edge symb, node nsymb\n",
  232. " # # # {'name': 'BZR', 'dataset': '../datasets/BZR_txt/BZR_A_sparse.txt'}, # node symb/nsymb\n",
  233. " # # # {'name': 'COX2', 'dataset': '../datasets/COX2_txt/COX2_A_sparse.txt'}, # node symb/nsymb\n",
  234. " # {'name': 'Fingerprint', 'dataset': '../datasets/Fingerprint/Fingerprint_A.txt'},\n",
  235. " #\n",
  236. " # # {'name': 'DHFR', 'dataset': '../datasets/DHFR_txt/DHFR_A_sparse.txt'}, # node symb/nsymb\n",
  237. " # # {'name': 'SYNTHETIC', 'dataset': '../datasets/SYNTHETIC_txt/SYNTHETIC_A_sparse.txt'}, # node symb/nsymb\n",
  238. "# {'name': 'MSRC9', 'dataset': '../datasets/MSRC_9_txt/MSRC_9_A.txt'}, # node symb, missing values\n",
  239. "# {'name': 'MSRC21', 'dataset': '../datasets/MSRC_21_txt/MSRC_21_A.txt'}, # node symb, missing values\n",
  240. " # # {'name': 'FIRSTMM_DB', 'dataset': '../datasets/FIRSTMM_DB/FIRSTMM_DB_A.txt'}, # node symb/nsymb ,edge nsymb\n",
  241. "\n",
  242. " # # {'name': 'PROTEINS', 'dataset': '../datasets/PROTEINS_txt/PROTEINS_A_sparse.txt'}, # node symb/nsymb\n",
  243. " # # {'name': 'PROTEINS_full', 'dataset': '../datasets/PROTEINS_full_txt/PROTEINS_full_A_sparse.txt'}, # node symb/nsymb\n",
  244. " # # {'name': 'AIDS', 'dataset': '../datasets/AIDS/AIDS_A.txt'}, # node symb/nsymb, edge symb\n",
  245. " # {'name': 'NCI1', 'dataset': '../datasets/NCI1/NCI1.mat',\n",
  246. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  247. " # {'name': 'NCI109', 'dataset': '../datasets/NCI109/NCI109.mat',\n",
  248. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  249. " # {'name': 'NCI-HIV', 'dataset': '../datasets/NCI-HIV/AIDO99SD.sdf',\n",
  250. " # 'dataset_y': '../datasets/NCI-HIV/aids_conc_may04.txt',}, # node/edge symb\n",
  251. "\n",
  252. "# # not working below\n",
  253. "# {'name': 'PTC_FM', 'dataset': '../datasets/PTC/Train/FM.ds',},\n",
  254. " # {'name': 'PTC_FR', 'dataset': '../datasets/PTC/Train/FR.ds',},\n",
  255. " # {'name': 'PTC_MM', 'dataset': '../datasets/PTC/Train/MM.ds',},\n",
  256. " # {'name': 'PTC_MR', 'dataset': '../datasets/PTC/Train/MR.ds',},\n",
  257. "]\n",
  258. "estimator = structuralspkernel\n",
  259. "mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  260. "param_grid_precomputed = {'node_kernels': \n",
  261. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  262. " 'edge_kernels': \n",
  263. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}]}\n",
  264. "param_grid = [{'C': np.logspace(-10, 10, num=41, base=10)},\n",
  265. " {'alpha': np.logspace(-10, 10, num=41, base=10)}]\n",
  266. "\n",
  267. "for ds in dslist:\n",
  268. " print()\n",
  269. " print(ds['name'])\n",
  270. " model_selection_for_precomputed_kernel(\n",
  271. " ds['dataset'],\n",
  272. " estimator,\n",
  273. " param_grid_precomputed,\n",
  274. " (param_grid[1] if ('task' in ds and ds['task']\n",
  275. " == 'regression') else param_grid[0]),\n",
  276. " (ds['task'] if 'task' in ds else 'classification'),\n",
  277. " NUM_TRIALS=30,\n",
  278. " datafile_y=(ds['dataset_y'] if 'dataset_y' in ds else None),\n",
  279. " extra_params=(ds['extra_params'] if 'extra_params' in ds else None),\n",
  280. " ds_name=ds['name'],\n",
  281. " n_jobs=multiprocessing.cpu_count(),\n",
  282. " read_gm_from_file=False)\n",
  283. " print()"
  284. ]
  285. }
  286. ],
  287. "metadata": {
  288. "kernelspec": {
  289. "display_name": "Python 3",
  290. "language": "python",
  291. "name": "python3"
  292. },
  293. "language_info": {
  294. "codemirror_mode": {
  295. "name": "ipython",
  296. "version": 3
  297. },
  298. "file_extension": ".py",
  299. "mimetype": "text/x-python",
  300. "name": "python",
  301. "nbconvert_exporter": "python",
  302. "pygments_lexer": "ipython3",
  303. "version": "3.6.6"
  304. }
  305. },
  306. "nbformat": 4,
  307. "nbformat_minor": 2
  308. }

A Python package for graph kernels, graph edit distances and graph pre-image problem.