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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, 5308.25it/s]\n"
  27. ]
  28. },
  29. {
  30. "name": "stderr",
  31. "output_type": "stream",
  32. "text": [
  33. "../gklearn/kernels/randomWalkKernel.py:108: 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, 65408.89it/s]\n",
  42. "\n",
  43. " --- kernel matrix of random walk kernel of size 183 built in 0.4157981872558594 seconds ---\n",
  44. "\n",
  45. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.1, 'n_jobs': 8, 'verbose': True} 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, 5205.09it/s]\n",
  52. "calculating kernels: 16836it [00:00, 73715.56it/s]\n",
  53. "\n",
  54. " --- kernel matrix of random walk kernel of size 183 built in 0.36714887619018555 seconds ---\n",
  55. "\n",
  56. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.01, 'n_jobs': 8, 'verbose': True} 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, 5344.96it/s]\n",
  63. "calculating kernels: 16836it [00:00, 68817.65it/s]\n",
  64. "\n",
  65. " --- kernel matrix of random walk kernel of size 183 built in 0.3666379451751709 seconds ---\n",
  66. "\n",
  67. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.001, 'n_jobs': 8, 'verbose': True} 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, 5295.73it/s]\n",
  74. "calculating kernels: 16836it [00:00, 74865.49it/s]\n",
  75. "\n",
  76. " --- kernel matrix of random walk kernel of size 183 built in 0.36979222297668457 seconds ---\n",
  77. "\n",
  78. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 0.0001, 'n_jobs': 8, 'verbose': True} 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, 5040.80it/s]\n",
  85. "calculating kernels: 16836it [00:00, 70923.54it/s]\n",
  86. "\n",
  87. " --- kernel matrix of random walk kernel of size 183 built in 0.3692610263824463 seconds ---\n",
  88. "\n",
  89. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-05, 'n_jobs': 8, 'verbose': True} 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, 5326.60it/s]\n",
  96. "calculating kernels: 16836it [00:00, 73697.55it/s]\n",
  97. "\n",
  98. " --- kernel matrix of random walk kernel of size 183 built in 0.37317800521850586 seconds ---\n",
  99. "\n",
  100. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-06, 'n_jobs': 8, 'verbose': True} 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, 5705.98it/s]\n",
  107. "calculating kernels: 16836it [00:00, 64238.65it/s]\n",
  108. "\n",
  109. " --- kernel matrix of random walk kernel of size 183 built in 0.36565732955932617 seconds ---\n",
  110. "\n",
  111. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-07, 'n_jobs': 8, 'verbose': True} 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, 4833.15it/s]\n",
  118. "calculating kernels: 16836it [00:00, 69971.77it/s]\n",
  119. "\n",
  120. " --- kernel matrix of random walk kernel of size 183 built in 0.37798523902893066 seconds ---\n",
  121. "\n",
  122. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-08, 'n_jobs': 8, 'verbose': True} 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, 4170.94it/s]\n",
  129. "calculating kernels: 16836it [00:00, 64187.38it/s]\n",
  130. "\n",
  131. " --- kernel matrix of random walk kernel of size 183 built in 0.39433860778808594 seconds ---\n",
  132. "\n",
  133. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-09, 'n_jobs': 8, 'verbose': True} 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, 5273.43it/s]\n",
  140. "calculating kernels: 16836it [00:00, 69555.28it/s]\n",
  141. "\n",
  142. " --- kernel matrix of random walk kernel of size 183 built in 0.3833920955657959 seconds ---\n",
  143. "\n",
  144. "the gram matrix with parameters {'compute_method': 'sylvester', 'weight': 1e-10, 'n_jobs': 8, 'verbose': True} 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.11s/it]\n",
  152. "\n",
  153. "4. Getting final performance...\n",
  154. "best_params_out: [{'compute_method': 'sylvester', 'weight': 0.01, 'n_jobs': 8, 'verbose': True}]\n",
  155. "best_params_in: [{'alpha': 1e-10}]\n",
  156. "\n",
  157. "best_val_perf: 31.76835551233969\n",
  158. "best_val_std: 0.43269972907929183\n",
  159. "final_performance: [32.391882524496765]\n",
  160. "final_confidence: [2.6542337929023336]\n",
  161. "train_performance: [30.70127313658435]\n",
  162. "train_std: [0.31861204198126475]\n",
  163. "\n",
  164. "time to calculate gram matrix with different hyper-params: 0.38±0.02s\n",
  165. "time to calculate best gram matrix: 0.37±0.00s\n",
  166. "total training time with all hyper-param choices: 40.53s\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, 28950.24it/s]\n",
  179. "calculating kernels: 16836it [00:02, 6540.43it/s]\n",
  180. "\n",
  181. " --- kernel matrix of random walk kernel of size 183 built in 2.6675093173980713 seconds ---\n",
  182. "\n",
  183. "the gram matrix with parameters {'compute_method': 'conjugate', 'edge_kernels': {'symb': <function deltakernel at 0x7fe9b0a2d730>, 'nsymb': <function gaussiankernel at 0x7fe9b0a2d7b8>, 'mix': functools.partial(<function kernelproduct at 0x7fe9b0a2d8c8>, <function deltakernel at 0x7fe9b0a2d730>, <function gaussiankernel at 0x7fe9b0a2d7b8>)}, 'node_kernels': {'symb': <function deltakernel at 0x7fe9b0a2d730>, 'nsymb': <function gaussiankernel at 0x7fe9b0a2d7b8>, 'mix': functools.partial(<function kernelproduct at 0x7fe9b0a2d8c8>, <function deltakernel at 0x7fe9b0a2d730>, <function gaussiankernel at 0x7fe9b0a2d7b8>)}, 'weight': 0.1, 'n_jobs': 8, 'verbose': True} is: \n",
  184. "\n",
  185. "\n",
  186. "\n",
  187. " None edge weight specified. Set all weight to 1.\n",
  188. "\n",
  189. "reindex vertices: 100%|██████████| 183/183 [00:00<00:00, 28019.19it/s]\n",
  190. "calculating kernels: 16836it [00:02, 7963.48it/s]\n",
  191. "\n",
  192. " --- kernel matrix of random walk kernel of size 183 built in 2.2675061225891113 seconds ---\n",
  193. "\n",
  194. "the gram matrix with parameters {'compute_method': 'conjugate', 'edge_kernels': {'symb': <function deltakernel at 0x7fe9b0a2d730>, 'nsymb': <function gaussiankernel at 0x7fe9b0a2d7b8>, 'mix': functools.partial(<function kernelproduct at 0x7fe9b0a2d8c8>, <function deltakernel at 0x7fe9b0a2d730>, <function gaussiankernel at 0x7fe9b0a2d7b8>)}, 'node_kernels': {'symb': <function deltakernel at 0x7fe9b0a2d730>, 'nsymb': <function gaussiankernel at 0x7fe9b0a2d7b8>, 'mix': functools.partial(<function kernelproduct at 0x7fe9b0a2d8c8>, <function deltakernel at 0x7fe9b0a2d730>, <function gaussiankernel at 0x7fe9b0a2d7b8>)}, 'weight': 0.01, 'n_jobs': 8, 'verbose': True} is: \n",
  195. "\n",
  196. "\n",
  197. "\n",
  198. " None edge weight specified. Set all weight to 1.\n",
  199. "\n",
  200. "reindex vertices: 100%|██████████| 183/183 [00:00<00:00, 23036.63it/s]\n",
  201. "calculating kernels: 12801it [00:01, 8043.11it/s]"
  202. ]
  203. }
  204. ],
  205. "source": [
  206. "# %load_ext line_profiler\n",
  207. "# %matplotlib inline\n",
  208. "import functools\n",
  209. "from libs import *\n",
  210. "import multiprocessing\n",
  211. "\n",
  212. "from gklearn.kernels.randomWalkKernel import randomwalkkernel\n",
  213. "from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct\n",
  214. "\n",
  215. "import numpy as np\n",
  216. "\n",
  217. "\n",
  218. "dslist = [\n",
  219. " {'name': 'Acyclic', 'dataset': '../datasets/acyclic/dataset_bps.ds',\n",
  220. " 'task': 'regression'}, # node symb\n",
  221. " {'name': 'Alkane', 'dataset': '../datasets/Alkane/dataset.ds', 'task': 'regression',\n",
  222. " 'dataset_y': '../datasets/Alkane/dataset_boiling_point_names.txt'}, \n",
  223. " # contains single node graph, node symb\n",
  224. " {'name': 'MAO', 'dataset': '../datasets/MAO/dataset.ds'}, # node/edge symb\n",
  225. " {'name': 'PAH', 'dataset': '../datasets/PAH/dataset.ds'}, # unlabeled\n",
  226. " {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt'}, # node/edge symb\n",
  227. " {'name': 'Letter-med', 'dataset': '../datasets/Letter-med/Letter-med_A.txt'},\n",
  228. " # node nsymb\n",
  229. " {'name': 'ENZYMES', 'dataset': '../datasets/ENZYMES_txt/ENZYMES_A_sparse.txt'},\n",
  230. " # node symb/nsymb\n",
  231. "# {'name': 'Mutagenicity', 'dataset': '../datasets/Mutagenicity/Mutagenicity_A.txt'},\n",
  232. "# # node/edge symb\n",
  233. "# {'name': 'D&D', 'dataset': '../datasets/DD/DD_A.txt'}, # node symb\n",
  234. "\n",
  235. " # {'name': 'COIL-DEL', 'dataset': '../datasets/COIL-DEL/COIL-DEL_A.txt'}, # edge symb, node nsymb\n",
  236. " # # # {'name': 'BZR', 'dataset': '../datasets/BZR_txt/BZR_A_sparse.txt'}, # node symb/nsymb\n",
  237. " # # # {'name': 'COX2', 'dataset': '../datasets/COX2_txt/COX2_A_sparse.txt'}, # node symb/nsymb\n",
  238. " # {'name': 'Fingerprint', 'dataset': '../datasets/Fingerprint/Fingerprint_A.txt'},\n",
  239. " #\n",
  240. " # # {'name': 'DHFR', 'dataset': '../datasets/DHFR_txt/DHFR_A_sparse.txt'}, # node symb/nsymb\n",
  241. " # # {'name': 'SYNTHETIC', 'dataset': '../datasets/SYNTHETIC_txt/SYNTHETIC_A_sparse.txt'}, # node symb/nsymb\n",
  242. "# {'name': 'MSRC9', 'dataset': '../datasets/MSRC_9_txt/MSRC_9_A.txt'}, # node symb, missing values\n",
  243. "# {'name': 'MSRC21', 'dataset': '../datasets/MSRC_21_txt/MSRC_21_A.txt'}, # node symb, missing values\n",
  244. " # # {'name': 'FIRSTMM_DB', 'dataset': '../datasets/FIRSTMM_DB/FIRSTMM_DB_A.txt'}, # node symb/nsymb ,edge nsymb\n",
  245. "\n",
  246. " # # {'name': 'PROTEINS', 'dataset': '../datasets/PROTEINS_txt/PROTEINS_A_sparse.txt'}, # node symb/nsymb\n",
  247. " # # {'name': 'PROTEINS_full', 'dataset': '../datasets/PROTEINS_full_txt/PROTEINS_full_A_sparse.txt'}, # node symb/nsymb\n",
  248. " # # {'name': 'AIDS', 'dataset': '../datasets/AIDS/AIDS_A.txt'}, # node symb/nsymb, edge symb\n",
  249. " # {'name': 'NCI1', 'dataset': '../datasets/NCI1/NCI1.mat',\n",
  250. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  251. " # {'name': 'NCI109', 'dataset': '../datasets/NCI109/NCI109.mat',\n",
  252. " # 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  253. " # {'name': 'NCI-HIV', 'dataset': '../datasets/NCI-HIV/AIDO99SD.sdf',\n",
  254. " # 'dataset_y': '../datasets/NCI-HIV/aids_conc_may04.txt',}, # node/edge symb\n",
  255. "\n",
  256. "# # not working below\n",
  257. "# {'name': 'PTC_FM', 'dataset': '../datasets/PTC/Train/FM.ds',},\n",
  258. " # {'name': 'PTC_FR', 'dataset': '../datasets/PTC/Train/FR.ds',},\n",
  259. " # {'name': 'PTC_MM', 'dataset': '../datasets/PTC/Train/MM.ds',},\n",
  260. " # {'name': 'PTC_MR', 'dataset': '../datasets/PTC/Train/MR.ds',},\n",
  261. "]\n",
  262. "estimator = randomwalkkernel\n",
  263. "param_grid = [{'C': np.logspace(-10, 10, num=41, base=10)},\n",
  264. " {'alpha': np.logspace(-10, 10, num=41, base=10)}]\n",
  265. "\n",
  266. "for ds in dslist:\n",
  267. " print()\n",
  268. " print(ds['name'])\n",
  269. " for compute_method in ['sylvester', 'conjugate', 'fp', 'spectral']:\n",
  270. " if compute_method == 'sylvester':\n",
  271. " param_grid_precomputed = {'compute_method': ['sylvester'],\n",
  272. "# 'weight': np.linspace(0.01, 0.10, 10)}\n",
  273. " 'weight': np.logspace(-1, -10, num=10, base=10)}\n",
  274. " elif compute_method == 'conjugate':\n",
  275. " mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  276. " param_grid_precomputed = {'compute_method': ['conjugate'], \n",
  277. " 'node_kernels': \n",
  278. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  279. " 'edge_kernels': \n",
  280. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  281. " 'weight': np.logspace(-1, -10, num=10, base=10)}\n",
  282. " elif compute_method == 'fp':\n",
  283. " mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  284. " param_grid_precomputed = {'compute_method': ['fp'], \n",
  285. " 'node_kernels': \n",
  286. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  287. " 'edge_kernels': \n",
  288. " [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}],\n",
  289. " 'weight': np.logspace(-3, -10, num=8, base=10)}\n",
  290. " elif compute_method == 'spectral':\n",
  291. " param_grid_precomputed = {'compute_method': ['spectral'],\n",
  292. " 'weight': np.logspace(-1, -10, num=10, base=10),\n",
  293. " 'sub_kernel': ['geo', 'exp']}\n",
  294. " model_selection_for_precomputed_kernel(\n",
  295. " ds['dataset'],\n",
  296. " estimator,\n",
  297. " param_grid_precomputed,\n",
  298. " (param_grid[1] if ('task' in ds and ds['task']\n",
  299. " == 'regression') else param_grid[0]),\n",
  300. " (ds['task'] if 'task' in ds else 'classification'),\n",
  301. " NUM_TRIALS=30,\n",
  302. " datafile_y=(ds['dataset_y'] if 'dataset_y' in ds else None),\n",
  303. " extra_params=(ds['extra_params'] if 'extra_params' in ds else None),\n",
  304. " ds_name=ds['name'],\n",
  305. " n_jobs=multiprocessing.cpu_count(),\n",
  306. " read_gm_from_file=False,\n",
  307. " verbose=True)\n",
  308. " print()"
  309. ]
  310. }
  311. ],
  312. "metadata": {
  313. "kernelspec": {
  314. "display_name": "Python 3",
  315. "language": "python",
  316. "name": "python3"
  317. },
  318. "language_info": {
  319. "codemirror_mode": {
  320. "name": "ipython",
  321. "version": 3
  322. },
  323. "file_extension": ".py",
  324. "mimetype": "text/x-python",
  325. "name": "python",
  326. "nbconvert_exporter": "python",
  327. "pygments_lexer": "ipython3",
  328. "version": "3.6.7"
  329. }
  330. },
  331. "nbformat": 4,
  332. "nbformat_minor": 2
  333. }

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