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_spkernel.ipynb 14 kB

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

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