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_randomwalkkernel.ipynb 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5067.96it/s]\n"
  27. ]
  28. },
  29. {
  30. "name": "stderr",
  31. "output_type": "stream",
  32. "text": [
  33. "../pygraph/kernels/randomWalkKernel.py:104: UserWarning: All labels are ignored.\n",
  34. " warnings.warn('All labels are ignored.')\n"
  35. ]
  36. },
  37. {
  38. "name": "stdout",
  39. "output_type": "stream",
  40. "text": [
  41. "calculating kernels: 16836it [00:00, 76328.32it/s]\n",
  42. "\n",
  43. " --- kernel matrix of random walk kernel of size 183 built in 0.38956499099731445 seconds ---\n",
  44. "\n",
  45. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.1, 'n_jobs': 8} is: \n",
  46. "\n",
  47. "\n",
  48. "\n",
  49. " None edge weight specified. Set all weight to 1.\n",
  50. "\n",
  51. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5928.69it/s]\n",
  52. "calculating kernels: 16836it [00:00, 76159.65it/s]\n",
  53. "\n",
  54. " --- kernel matrix of random walk kernel of size 183 built in 0.3680381774902344 seconds ---\n",
  55. "\n",
  56. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.01, 'n_jobs': 8} is: \n",
  57. "\n",
  58. "\n",
  59. "\n",
  60. " None edge weight specified. Set all weight to 1.\n",
  61. "\n",
  62. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5599.99it/s]\n",
  63. "calculating kernels: 16836it [00:00, 68122.63it/s]\n",
  64. "\n",
  65. " --- kernel matrix of random walk kernel of size 183 built in 0.37236690521240234 seconds ---\n",
  66. "\n",
  67. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.001, 'n_jobs': 8} is: \n",
  68. "\n",
  69. "\n",
  70. "\n",
  71. " None edge weight specified. Set all weight to 1.\n",
  72. "\n",
  73. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5444.75it/s]\n",
  74. "calculating kernels: 16836it [00:00, 65554.92it/s]\n",
  75. "\n",
  76. " --- kernel matrix of random walk kernel of size 183 built in 0.37699007987976074 seconds ---\n",
  77. "\n",
  78. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.0001, 'n_jobs': 8} is: \n",
  79. "\n",
  80. "\n",
  81. "\n",
  82. " None edge weight specified. Set all weight to 1.\n",
  83. "\n",
  84. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 4451.65it/s]\n",
  85. "calculating kernels: 16836it [00:00, 64227.38it/s]\n",
  86. "\n",
  87. " --- kernel matrix of random walk kernel of size 183 built in 0.40868353843688965 seconds ---\n",
  88. "\n",
  89. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-05, 'n_jobs': 8} is: \n",
  90. "\n",
  91. "\n",
  92. "\n",
  93. " None edge weight specified. Set all weight to 1.\n",
  94. "\n",
  95. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5455.55it/s]\n",
  96. "calculating kernels: 16836it [00:00, 69253.03it/s]\n",
  97. "\n",
  98. " --- kernel matrix of random walk kernel of size 183 built in 0.4017300605773926 seconds ---\n",
  99. "\n",
  100. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-06, 'n_jobs': 8} is: \n",
  101. "\n",
  102. "\n",
  103. "\n",
  104. " None edge weight specified. Set all weight to 1.\n",
  105. "\n",
  106. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5691.60it/s]\n",
  107. "calculating kernels: 16836it [00:00, 67461.93it/s]\n",
  108. "\n",
  109. " --- kernel matrix of random walk kernel of size 183 built in 0.3712270259857178 seconds ---\n",
  110. "\n",
  111. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-07, 'n_jobs': 8} is: \n",
  112. "\n",
  113. "\n",
  114. "\n",
  115. " None edge weight specified. Set all weight to 1.\n",
  116. "\n",
  117. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5229.98it/s]\n",
  118. "calculating kernels: 16836it [00:00, 70363.33it/s]\n",
  119. "\n",
  120. " --- kernel matrix of random walk kernel of size 183 built in 0.37551283836364746 seconds ---\n",
  121. "\n",
  122. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-08, 'n_jobs': 8} is: \n",
  123. "\n",
  124. "\n",
  125. "\n",
  126. " None edge weight specified. Set all weight to 1.\n",
  127. "\n",
  128. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5023.84it/s]\n",
  129. "calculating kernels: 16836it [00:00, 71457.21it/s]\n",
  130. "\n",
  131. " --- kernel matrix of random walk kernel of size 183 built in 0.39424848556518555 seconds ---\n",
  132. "\n",
  133. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-09, 'n_jobs': 8} is: \n",
  134. "\n",
  135. "\n",
  136. "\n",
  137. " None edge weight specified. Set all weight to 1.\n",
  138. "\n",
  139. "compute adjacency matrices: 100%|██████████| 183/183 [00:00<00:00, 5359.14it/s]\n",
  140. "calculating kernels: 16836it [00:00, 73095.73it/s]\n",
  141. "\n",
  142. " --- kernel matrix of random walk kernel of size 183 built in 0.37821507453918457 seconds ---\n",
  143. "\n",
  144. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-10, 'n_jobs': 8} is: \n",
  145. "\n",
  146. "\n",
  147. "\n",
  148. "10 gram matrices are calculated, 0 of which are ignored.\n",
  149. "\n",
  150. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  151. "cross validation: 30it [00:33, 1.10s/it]\n",
  152. "\n",
  153. "4. Getting final performance...\n",
  154. "best_params_out: [{'compute_method': 'sylvester', 'weight': 0.01, 'n_jobs': 8}]\n",
  155. "best_params_in: [{'alpha': 3.1622776601683795e-10}]\n",
  156. "\n",
  157. "best_val_perf: 31.894498817348637\n",
  158. "best_val_std: 0.5235865101548381\n",
  159. "final_performance: [32.04579601256704]\n",
  160. "final_confidence: [3.9737533137212138]\n",
  161. "train_performance: [30.830572265896325]\n",
  162. "train_std: [0.44072573889937117]\n",
  163. "\n",
  164. "time to calculate gram matrix with different hyper-params: 0.38±0.01s\n",
  165. "time to calculate best gram matrix: 0.37±nans\n",
  166. "total training time with all hyper-param choices: 40.27s\n",
  167. "\n",
  168. "\n",
  169. "--- This is a regression problem ---\n",
  170. "\n",
  171. "\n",
  172. "1. Loading dataset from file...\n",
  173. "\n",
  174. "2. Calculating gram matrices. This could take a while...\n",
  175. "\n",
  176. " None edge weight specified. Set all weight to 1.\n",
  177. "\n",
  178. "reindex vertices: 100%|██████████| 183/183 [00:00<00:00, 26008.32it/s]\n"
  179. ]
  180. },
  181. {
  182. "name": "stderr",
  183. "output_type": "stream",
  184. "text": [
  185. "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:140: RuntimeWarning: Degrees of freedom <= 0 for slice\n",
  186. " keepdims=keepdims)\n",
  187. "/usr/local/lib/python3.6/dist-packages/numpy/core/_methods.py:132: RuntimeWarning: invalid value encountered in double_scalars\n",
  188. " ret = ret.dtype.type(ret / rcount)\n"
  189. ]
  190. },
  191. {
  192. "name": "stdout",
  193. "output_type": "stream",
  194. "text": [
  195. "calculating kernels: 12501it [00:01, 6173.52it/s]"
  196. ]
  197. }
  198. ],
  199. "source": [
  200. "# %load_ext line_profiler\n",
  201. "# %matplotlib inline\n",
  202. "import functools\n",
  203. "from libs import *\n",
  204. "import multiprocessing\n",
  205. "\n",
  206. "from pygraph.kernels.randomWalkKernel import randomwalkkernel\n",
  207. "from pygraph.utils.kernels import deltakernel, gaussiankernel, kernelproduct\n",
  208. "\n",
  209. "import numpy as np\n",
  210. "\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 = randomwalkkernel\n",
  259. "param_grid = [{'C': np.logspace(-10, 10, num=41, base=10)},\n",
  260. " {'alpha': np.logspace(-10, 10, num=41, base=10)}]\n",
  261. "\n",
  262. "for ds in dslist:\n",
  263. " print()\n",
  264. " print(ds['name'])\n",
  265. " for compute_method in ['sylvester', 'conjugate', 'fp', 'spectral']:\n",
  266. " if compute_method == 'sylvester':\n",
  267. " param_grid_precomputed = {'compute_method': ['sylvester'],\n",
  268. "# 'weight': np.linspace(0.01, 0.10, 10)}\n",
  269. " 'weight': np.logspace(-1, -10, num=10, base=10)}\n",
  270. " elif compute_method == 'conjugate':\n",
  271. " mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  272. " param_grid_precomputed = {'compute_method': ['conjugate'], \n",
  273. " 'node_kernels': \n",
  274. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  275. " 'edge_kernels': \n",
  276. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  277. " 'weight': np.logspace(-1, -10, num=10, base=10)}\n",
  278. " elif compute_method == 'fp':\n",
  279. " mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  280. " param_grid_precomputed = {'compute_method': ['fp'], \n",
  281. " 'node_kernels': \n",
  282. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  283. " 'edge_kernels': \n",
  284. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  285. " 'weight': np.logspace(-3, -10, num=8, base=10)}\n",
  286. " elif compute_method == 'spectral':\n",
  287. " param_grid_precomputed = {'compute_method': ['spectral'],\n",
  288. " 'weight': np.logspace(-1, -10, num=10, base=10),\n",
  289. " 'sub_kernel': ['geo', 'exp']}\n",
  290. " model_selection_for_precomputed_kernel(\n",
  291. " ds['dataset'],\n",
  292. " estimator,\n",
  293. " param_grid_precomputed,\n",
  294. " (param_grid[1] if ('task' in ds and ds['task']\n",
  295. " == 'regression') else param_grid[0]),\n",
  296. " (ds['task'] if 'task' in ds else 'classification'),\n",
  297. " NUM_TRIALS=30,\n",
  298. " datafile_y=(ds['dataset_y'] if 'dataset_y' in ds else None),\n",
  299. " extra_params=(ds['extra_params'] if 'extra_params' in ds else None),\n",
  300. " ds_name=ds['name'],\n",
  301. " n_jobs=multiprocessing.cpu_count(),\n",
  302. " read_gm_from_file=False)\n",
  303. " print()"
  304. ]
  305. }
  306. ],
  307. "metadata": {
  308. "kernelspec": {
  309. "display_name": "Python 3",
  310. "language": "python",
  311. "name": "python3"
  312. },
  313. "language_info": {
  314. "codemirror_mode": {
  315. "name": "ipython",
  316. "version": 3
  317. },
  318. "file_extension": ".py",
  319. "mimetype": "text/x-python",
  320. "name": "python",
  321. "nbconvert_exporter": "python",
  322. "pygments_lexer": "ipython3",
  323. "version": "3.6.7"
  324. }
  325. },
  326. "nbformat": 4,
  327. "nbformat_minor": 2
  328. }

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