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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, 5316.42it/s]\n",
  27. "calculating kernels: 16836it [00:03, 4625.84it/s]\n",
  28. "\n",
  29. " --- shortest path kernel matrix of size 183 built in 3.8611345291137695 seconds ---\n",
  30. "\n",
  31. "the gram matrix with parameters {'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'n_jobs': 8} 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.71it/s]\n",
  39. "\n",
  40. "4. Getting final performance...\n",
  41. "best_params_out: [{'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'n_jobs': 8}]\n",
  42. "best_params_in: [{'alpha': 0.0031622776601683794}]\n",
  43. "\n",
  44. "best_val_perf: 12.673707811197355\n",
  45. "best_val_std: 0.8773195213759171\n",
  46. "final_performance: [12.972668262063593]\n",
  47. "final_confidence: [3.7642237202379087]\n",
  48. "train_performance: [3.934708519599526]\n",
  49. "train_std: [0.16225809646161615]\n",
  50. "\n",
  51. "time to calculate gram matrix with different hyper-params: 3.86±nans\n",
  52. "time to calculate best gram matrix: 3.86±nans\n",
  53. "total training time with all hyper-param choices: 7.74s\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. ]
  69. },
  70. {
  71. "name": "stderr",
  72. "output_type": "stream",
  73. "text": [
  74. "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:140: RuntimeWarning: Degrees of freedom <= 0 for slice\n",
  75. " keepdims=keepdims)\n",
  76. "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:132: RuntimeWarning: invalid value encountered in double_scalars\n",
  77. " ret = ret.dtype.type(ret / rcount)\n"
  78. ]
  79. },
  80. {
  81. "name": "stdout",
  82. "output_type": "stream",
  83. "text": [
  84. "getting shortest paths: 150it [00:00, 8822.07it/s]\n",
  85. "calculating kernels: 11325it [00:02, 5167.04it/s]\n",
  86. "\n",
  87. " --- shortest path kernel matrix of size 150 built in 2.394453525543213 seconds ---\n",
  88. "\n",
  89. "the gram matrix with parameters {'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'n_jobs': 8} is: \n",
  90. "\n",
  91. "\n",
  92. "\n",
  93. "1 gram matrices are calculated, 0 of which are ignored.\n",
  94. "\n",
  95. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  96. "cross validation: 30it [00:02, 10.78it/s]\n",
  97. "\n",
  98. "4. Getting final performance...\n",
  99. "best_params_out: [{'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'n_jobs': 8}]\n",
  100. "best_params_in: [{'alpha': 0.1}]\n",
  101. "\n",
  102. "best_val_perf: 11.082918177885857\n",
  103. "best_val_std: 0.3037589925734673\n",
  104. "final_performance: [7.8261546009779925]\n",
  105. "final_confidence: [1.59375970943081]\n",
  106. "train_performance: [7.988630946761633]\n",
  107. "train_std: [0.16054607648943253]\n",
  108. "\n",
  109. "time to calculate gram matrix with different hyper-params: 2.39±nans\n",
  110. "time to calculate best gram matrix: 2.39±nans\n",
  111. "total training time with all hyper-param choices: 5.49s\n",
  112. "\n",
  113. "\n",
  114. "\n",
  115. "MAO\n",
  116. "\n",
  117. "--- This is a classification problem ---\n",
  118. "\n",
  119. "\n",
  120. "1. Loading dataset from file...\n",
  121. "\n",
  122. "2. Calculating gram matrices. This could take a while...\n",
  123. "\n",
  124. " None edge weight specified. Set all weight to 1.\n",
  125. "\n",
  126. "getting shortest paths: 68it [00:00, 567.53it/s]\n",
  127. "calculating kernels: 2346it [00:14, 161.71it/s]\n",
  128. "\n",
  129. " --- shortest path kernel matrix of size 68 built in 14.833482265472412 seconds ---\n",
  130. "\n",
  131. "the gram matrix with parameters {'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'n_jobs': 8} is: \n",
  132. "\n",
  133. "\n",
  134. "\n",
  135. "1 gram matrices are calculated, 0 of which are ignored.\n",
  136. "\n",
  137. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  138. "cross validation: 30it [00:02, 13.38it/s]\n",
  139. "\n",
  140. "4. Getting final performance...\n",
  141. "best_params_out: [{'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'n_jobs': 8}]\n",
  142. "best_params_in: [{'C': 1000.0}]\n",
  143. "\n",
  144. "best_val_perf: 0.9084126984126983\n",
  145. "best_val_std: 0.027912022159840448\n",
  146. "final_performance: [0.9085714285714286]\n",
  147. "final_confidence: [0.0879511091875412]\n",
  148. "train_performance: [0.9679438832772166]\n",
  149. "train_std: [0.00754192133247499]\n",
  150. "\n",
  151. "time to calculate gram matrix with different hyper-params: 14.83±nans\n",
  152. "time to calculate best gram matrix: 14.83±nans\n",
  153. "total training time with all hyper-param choices: 17.42s\n",
  154. "\n",
  155. "\n",
  156. "\n",
  157. "PAH\n",
  158. "\n",
  159. "--- This is a classification problem ---\n",
  160. "\n",
  161. "\n",
  162. "1. Loading dataset from file...\n",
  163. "\n",
  164. "2. Calculating gram matrices. This could take a while...\n",
  165. "\n",
  166. " None edge weight specified. Set all weight to 1.\n",
  167. "\n",
  168. "getting shortest paths: 94it [00:00, 447.28it/s]\n",
  169. "calculating kernels: 4465it [01:04, 68.94it/s] \n",
  170. "\n",
  171. " --- shortest path kernel matrix of size 94 built in 65.20552921295166 seconds ---\n",
  172. "\n",
  173. "the gram matrix with parameters {'compute_method': 'naive', 'edge_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'node_kernels': {'symb': <function deltakernel at 0x7f470f0ad268>, 'nsymb': <function gaussiankernel at 0x7f470f0ad2f0>, 'mix': functools.partial(<function kernelproduct at 0x7f470f0ad400>, <function deltakernel at 0x7f470f0ad268>, <function gaussiankernel at 0x7f470f0ad2f0>)}, 'n_jobs': 8} is: \n",
  174. "\n",
  175. "\n",
  176. "\n",
  177. "1 gram matrices are calculated, 0 of which are ignored.\n",
  178. "\n",
  179. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  180. "cross validation: 0it [00:00, ?it/s]"
  181. ]
  182. }
  183. ],
  184. "source": [
  185. "import functools\n",
  186. "from libs import *\n",
  187. "import multiprocessing\n",
  188. "\n",
  189. "from pygraph.kernels.structuralspKernel import structuralspkernel\n",
  190. "from pygraph.utils.kernels import deltakernel, gaussiankernel, kernelproduct\n",
  191. "\n",
  192. "dslist = [\n",
  193. " {'name': 'Acyclic', 'dataset': '../datasets/acyclic/dataset_bps.ds',\n",
  194. " 'task': 'regression'}, # node symb\n",
  195. " {'name': 'Alkane', 'dataset': '../datasets/Alkane/dataset.ds', 'task': 'regression',\n",
  196. " 'dataset_y': '../datasets/Alkane/dataset_boiling_point_names.txt', }, \n",
  197. " # contains single node graph, node symb\n",
  198. " {'name': 'MAO', 'dataset': '../datasets/MAO/dataset.ds', }, # node/edge symb\n",
  199. " {'name': 'PAH', 'dataset': '../datasets/PAH/dataset.ds', }, # unlabeled\n",
  200. " {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG.mat',\n",
  201. " 'extra_params': {'am_sp_al_nl_el': [0, 0, 3, 1, 2]}}, # node/edge symb\n",
  202. " {'name': 'Letter-med', 'dataset': '../datasets/Letter-med/Letter-med_A.txt'},\n",
  203. " # node nsymb\n",
  204. " {'name': 'ENZYMES', 'dataset': '../datasets/ENZYMES_txt/ENZYMES_A_sparse.txt'},\n",
  205. " # node symb/nsymb\n",
  206. "# {'name': 'Mutagenicity', 'dataset': '../datasets/Mutagenicity/Mutagenicity_A.txt'},\n",
  207. "# # node/edge symb\n",
  208. "# {'name': 'D&D', 'dataset': '../datasets/D&D/DD.mat',\n",
  209. "# 'extra_params': {'am_sp_al_nl_el': [0, 1, 2, 1, -1]}}, # node symb\n",
  210. "\n",
  211. " # {'name': 'COIL-DEL', 'dataset': '../datasets/COIL-DEL/COIL-DEL_A.txt'}, # edge symb, node nsymb\n",
  212. " # # # {'name': 'BZR', 'dataset': '../datasets/BZR_txt/BZR_A_sparse.txt'}, # node symb/nsymb\n",
  213. " # # # {'name': 'COX2', 'dataset': '../datasets/COX2_txt/COX2_A_sparse.txt'}, # node symb/nsymb\n",
  214. " # {'name': 'Fingerprint', 'dataset': '../datasets/Fingerprint/Fingerprint_A.txt'},\n",
  215. " #\n",
  216. " # # {'name': 'DHFR', 'dataset': '../datasets/DHFR_txt/DHFR_A_sparse.txt'}, # node symb/nsymb\n",
  217. " # # {'name': 'SYNTHETIC', 'dataset': '../datasets/SYNTHETIC_txt/SYNTHETIC_A_sparse.txt'}, # node symb/nsymb\n",
  218. "# {'name': 'MSRC9', 'dataset': '../datasets/MSRC_9_txt/MSRC_9_A.txt'}, # node symb, missing values\n",
  219. "# {'name': 'MSRC21', 'dataset': '../datasets/MSRC_21_txt/MSRC_21_A.txt'}, # node symb, missing values\n",
  220. " # # {'name': 'FIRSTMM_DB', 'dataset': '../datasets/FIRSTMM_DB/FIRSTMM_DB_A.txt'}, # node symb/nsymb ,edge nsymb\n",
  221. "\n",
  222. " # # {'name': 'PROTEINS', 'dataset': '../datasets/PROTEINS_txt/PROTEINS_A_sparse.txt'}, # node symb/nsymb\n",
  223. " # # {'name': 'PROTEINS_full', 'dataset': '../datasets/PROTEINS_full_txt/PROTEINS_full_A_sparse.txt'}, # node symb/nsymb\n",
  224. " # # {'name': 'AIDS', 'dataset': '../datasets/AIDS/AIDS_A.txt'}, # node symb/nsymb, edge symb\n",
  225. " # {'name': 'NCI1', 'dataset': '../datasets/NCI1/NCI1.mat',\n",
  226. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  227. " # {'name': 'NCI109', 'dataset': '../datasets/NCI109/NCI109.mat',\n",
  228. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  229. " # {'name': 'NCI-HIV', 'dataset': '../datasets/NCI-HIV/AIDO99SD.sdf',\n",
  230. " # 'dataset_y': '../datasets/NCI-HIV/aids_conc_may04.txt',}, # node/edge symb\n",
  231. "\n",
  232. "# # not working below\n",
  233. "# {'name': 'PTC_FM', 'dataset': '../datasets/PTC/Train/FM.ds',},\n",
  234. " # {'name': 'PTC_FR', 'dataset': '../datasets/PTC/Train/FR.ds',},\n",
  235. " # {'name': 'PTC_MM', 'dataset': '../datasets/PTC/Train/MM.ds',},\n",
  236. " # {'name': 'PTC_MR', 'dataset': '../datasets/PTC/Train/MR.ds',},\n",
  237. "]\n",
  238. "estimator = structuralspkernel\n",
  239. "mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  240. "param_grid_precomputed = {'node_kernels': \n",
  241. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  242. " 'edge_kernels': \n",
  243. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  244. " 'compute_method': ['naive']}\n",
  245. "param_grid = [{'C': np.logspace(-10, 10, num=41, base=10)},\n",
  246. " {'alpha': np.logspace(-10, 10, num=41, base=10)}]\n",
  247. "\n",
  248. "for ds in dslist:\n",
  249. " print()\n",
  250. " print(ds['name'])\n",
  251. " model_selection_for_precomputed_kernel(\n",
  252. " ds['dataset'],\n",
  253. " estimator,\n",
  254. " param_grid_precomputed,\n",
  255. " (param_grid[1] if ('task' in ds and ds['task']\n",
  256. " == 'regression') else param_grid[0]),\n",
  257. " (ds['task'] if 'task' in ds else 'classification'),\n",
  258. " NUM_TRIALS=30,\n",
  259. " datafile_y=(ds['dataset_y'] if 'dataset_y' in ds else None),\n",
  260. " extra_params=(ds['extra_params'] if 'extra_params' in ds else None),\n",
  261. " ds_name=ds['name'],\n",
  262. " n_jobs=multiprocessing.cpu_count(),\n",
  263. " read_gm_from_file=False)\n",
  264. " print()"
  265. ]
  266. }
  267. ],
  268. "metadata": {
  269. "kernelspec": {
  270. "display_name": "Python 3",
  271. "language": "python",
  272. "name": "python3"
  273. },
  274. "language_info": {
  275. "codemirror_mode": {
  276. "name": "ipython",
  277. "version": 3
  278. },
  279. "file_extension": ".py",
  280. "mimetype": "text/x-python",
  281. "name": "python",
  282. "nbconvert_exporter": "python",
  283. "pygments_lexer": "ipython3",
  284. "version": "3.6.7"
  285. }
  286. },
  287. "nbformat": 4,
  288. "nbformat_minor": 2
  289. }

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