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 12 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "metadata": {
  7. "scrolled": false
  8. },
  9. "outputs": [
  10. {
  11. "name": "stdout",
  12. "output_type": "stream",
  13. "text": [
  14. "\n",
  15. "Acyclic\n",
  16. "\n",
  17. "--- This is a regression 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: 183it [00:00, 5323.35it/s]\n",
  27. "calculating kernels: 16836it [00:02, 5980.75it/s]\n",
  28. "\n",
  29. " --- shortest path kernel matrix of size 183 built in 3.0884954929351807 seconds ---\n",
  30. "\n",
  31. "the gram matrix with parameters {'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'n_jobs': 8, 'verbose': True} is: \n",
  32. "\n",
  33. "\n",
  34. "\n",
  35. "1 gram matrices are calculated, 0 of which are ignored.\n",
  36. "\n",
  37. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  38. "cross validation: 30it [00:03, 8.90it/s]\n",
  39. "\n",
  40. "4. Getting final performance...\n",
  41. "best_params_out: [{'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'n_jobs': 8, 'verbose': True}]\n",
  42. "best_params_in: [{'alpha': 0.001}]\n",
  43. "\n",
  44. "best_val_perf: 12.857015647214508\n",
  45. "best_val_std: 0.8860388066269581\n",
  46. "final_performance: [12.157314781928168]\n",
  47. "final_confidence: [2.5739406086892296]\n",
  48. "train_performance: [3.773093745028789]\n",
  49. "train_std: [0.12430822644728814]\n",
  50. "\n",
  51. "time to calculate gram matrix with different hyper-params: 3.09±0.00s\n",
  52. "time to calculate best gram matrix: 3.09±0.00s\n",
  53. "total training time with all hyper-param choices: 6.84s\n",
  54. "\n",
  55. "\n",
  56. "\n",
  57. "Alkane\n",
  58. "\n",
  59. "--- This is a regression problem ---\n",
  60. "\n",
  61. "\n",
  62. "1. Loading dataset from file...\n",
  63. "\n",
  64. "2. Calculating gram matrices. This could take a while...\n",
  65. "\n",
  66. " None edge weight specified. Set all weight to 1.\n",
  67. "\n",
  68. "getting shortest paths: 150it [00:00, 5191.83it/s]\n",
  69. "calculating kernels: 11325it [00:01, 7143.18it/s]\n",
  70. "\n",
  71. " --- shortest path kernel matrix of size 150 built in 1.7898523807525635 seconds ---\n",
  72. "\n",
  73. "the gram matrix with parameters {'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'n_jobs': 8, 'verbose': True} is: \n",
  74. "\n",
  75. "\n",
  76. "\n",
  77. "1 gram matrices are calculated, 0 of which are ignored.\n",
  78. "\n",
  79. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  80. "cross validation: 30it [00:02, 10.59it/s]\n",
  81. "\n",
  82. "4. Getting final performance...\n",
  83. "best_params_out: [{'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7ff5ffc0c268>, 'nsymb': <function gaussiankernel at 0x7ff5ffc0c2f0>, 'mix': functools.partial(<function kernelproduct at 0x7ff5ffc0c400>, <function deltakernel at 0x7ff5ffc0c268>, <function gaussiankernel at 0x7ff5ffc0c2f0>)}, 'n_jobs': 8, 'verbose': True}]\n",
  84. "best_params_in: [{'alpha': 0.1}]\n",
  85. "\n",
  86. "best_val_perf: 11.040598123045763\n",
  87. "best_val_std: 0.31492017111536147\n",
  88. "final_performance: [8.138193149138093]\n",
  89. "final_confidence: [1.6238744767195439]\n",
  90. "train_performance: [7.9412913127748235]\n",
  91. "train_std: [0.18726339675217385]\n",
  92. "\n",
  93. "time to calculate gram matrix with different hyper-params: 1.79±0.00s\n",
  94. "time to calculate best gram matrix: 1.79±0.00s\n",
  95. "total training time with all hyper-param choices: 5.00s\n",
  96. "\n",
  97. "\n",
  98. "\n",
  99. "MAO\n",
  100. "\n",
  101. "--- This is a classification problem ---\n",
  102. "\n",
  103. "\n",
  104. "1. Loading dataset from file...\n",
  105. "\n",
  106. "2. Calculating gram matrices. This could take a while...\n",
  107. "\n",
  108. " None edge weight specified. Set all weight to 1.\n",
  109. "\n",
  110. "getting shortest paths: 68it [00:00, 536.19it/s]\n",
  111. "calculating kernels: 0it [00:00, ?it/s]"
  112. ]
  113. }
  114. ],
  115. "source": [
  116. "import functools\n",
  117. "from libs import *\n",
  118. "import multiprocessing\n",
  119. "\n",
  120. "from gklearn.kernels.structuralspKernel import structuralspkernel\n",
  121. "from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct\n",
  122. "\n",
  123. "dslist = [\n",
  124. " {'name': 'Acyclic', 'dataset': '../datasets/acyclic/dataset_bps.ds',\n",
  125. " 'task': 'regression'}, # node symb\n",
  126. " {'name': 'Alkane', 'dataset': '../datasets/Alkane/dataset.ds', 'task': 'regression',\n",
  127. " 'dataset_y': '../datasets/Alkane/dataset_boiling_point_names.txt'}, \n",
  128. " # contains single node graph, node symb\n",
  129. " {'name': 'MAO', 'dataset': '../datasets/MAO/dataset.ds'}, # node/edge symb\n",
  130. " {'name': 'PAH', 'dataset': '../datasets/PAH/dataset.ds'}, # unlabeled\n",
  131. " {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt'}, # node/edge symb\n",
  132. " {'name': 'Letter-med', 'dataset': '../datasets/Letter-med/Letter-med_A.txt'},\n",
  133. " # node nsymb\n",
  134. " {'name': 'ENZYMES', 'dataset': '../datasets/ENZYMES_txt/ENZYMES_A_sparse.txt'},\n",
  135. " # node symb/nsymb\n",
  136. "# {'name': 'Mutagenicity', 'dataset': '../datasets/Mutagenicity/Mutagenicity_A.txt'},\n",
  137. "# # node/edge symb\n",
  138. "# {'name': 'D&D', 'dataset': '../datasets/DD/DD_A.txt'}, # node symb\n",
  139. "\n",
  140. " # {'name': 'COIL-DEL', 'dataset': '../datasets/COIL-DEL/COIL-DEL_A.txt'}, # edge symb, node nsymb\n",
  141. " # # # {'name': 'BZR', 'dataset': '../datasets/BZR_txt/BZR_A_sparse.txt'}, # node symb/nsymb\n",
  142. " # # # {'name': 'COX2', 'dataset': '../datasets/COX2_txt/COX2_A_sparse.txt'}, # node symb/nsymb\n",
  143. " # {'name': 'Fingerprint', 'dataset': '../datasets/Fingerprint/Fingerprint_A.txt'},\n",
  144. " #\n",
  145. " # # {'name': 'DHFR', 'dataset': '../datasets/DHFR_txt/DHFR_A_sparse.txt'}, # node symb/nsymb\n",
  146. " # # {'name': 'SYNTHETIC', 'dataset': '../datasets/SYNTHETIC_txt/SYNTHETIC_A_sparse.txt'}, # node symb/nsymb\n",
  147. "# {'name': 'MSRC9', 'dataset': '../datasets/MSRC_9_txt/MSRC_9_A.txt'}, # node symb, missing values\n",
  148. "# {'name': 'MSRC21', 'dataset': '../datasets/MSRC_21_txt/MSRC_21_A.txt'}, # node symb, missing values\n",
  149. " # # {'name': 'FIRSTMM_DB', 'dataset': '../datasets/FIRSTMM_DB/FIRSTMM_DB_A.txt'}, # node symb/nsymb ,edge nsymb\n",
  150. "\n",
  151. " # # {'name': 'PROTEINS', 'dataset': '../datasets/PROTEINS_txt/PROTEINS_A_sparse.txt'}, # node symb/nsymb\n",
  152. " # # {'name': 'PROTEINS_full', 'dataset': '../datasets/PROTEINS_full_txt/PROTEINS_full_A_sparse.txt'}, # node symb/nsymb\n",
  153. " # # {'name': 'AIDS', 'dataset': '../datasets/AIDS/AIDS_A.txt'}, # node symb/nsymb, edge symb\n",
  154. " # {'name': 'NCI1', 'dataset': '../datasets/NCI1/NCI1.mat',\n",
  155. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  156. " # {'name': 'NCI109', 'dataset': '../datasets/NCI109/NCI109.mat',\n",
  157. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  158. " # {'name': 'NCI-HIV', 'dataset': '../datasets/NCI-HIV/AIDO99SD.sdf',\n",
  159. " # 'dataset_y': '../datasets/NCI-HIV/aids_conc_may04.txt',}, # node/edge symb\n",
  160. "\n",
  161. "# # not working below\n",
  162. "# {'name': 'PTC_FM', 'dataset': '../datasets/PTC/Train/FM.ds',},\n",
  163. " # {'name': 'PTC_FR', 'dataset': '../datasets/PTC/Train/FR.ds',},\n",
  164. " # {'name': 'PTC_MM', 'dataset': '../datasets/PTC/Train/MM.ds',},\n",
  165. " # {'name': 'PTC_MR', 'dataset': '../datasets/PTC/Train/MR.ds',},\n",
  166. "]\n",
  167. "estimator = structuralspkernel\n",
  168. "mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  169. "param_grid_precomputed = {'node_kernels': \n",
  170. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  171. " 'edge_kernels': \n",
  172. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  173. " 'compute_method': ['naive']}\n",
  174. "param_grid = [{'C': np.logspace(-10, 10, num=41, base=10)},\n",
  175. " {'alpha': np.logspace(-10, 10, num=41, base=10)}]\n",
  176. "\n",
  177. "for ds in dslist:\n",
  178. " print()\n",
  179. " print(ds['name'])\n",
  180. " model_selection_for_precomputed_kernel(\n",
  181. " ds['dataset'],\n",
  182. " estimator,\n",
  183. " param_grid_precomputed,\n",
  184. " (param_grid[1] if ('task' in ds and ds['task']\n",
  185. " == 'regression') else param_grid[0]),\n",
  186. " (ds['task'] if 'task' in ds else 'classification'),\n",
  187. " NUM_TRIALS=30,\n",
  188. " datafile_y=(ds['dataset_y'] if 'dataset_y' in ds else None),\n",
  189. " extra_params=(ds['extra_params'] if 'extra_params' in ds else None),\n",
  190. " ds_name=ds['name'],\n",
  191. " n_jobs=multiprocessing.cpu_count(),\n",
  192. " read_gm_from_file=False,\n",
  193. " verbose=True)\n",
  194. " print()"
  195. ]
  196. }
  197. ],
  198. "metadata": {
  199. "kernelspec": {
  200. "display_name": "Python 3",
  201. "language": "python",
  202. "name": "python3"
  203. },
  204. "language_info": {
  205. "codemirror_mode": {
  206. "name": "ipython",
  207. "version": 3
  208. },
  209. "file_extension": ".py",
  210. "mimetype": "text/x-python",
  211. "name": "python",
  212. "nbconvert_exporter": "python",
  213. "pygments_lexer": "ipython3",
  214. "version": "3.6.7"
  215. }
  216. },
  217. "nbformat": 4,
  218. "nbformat_minor": 2
  219. }

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