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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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, 11704.68it/s]\n",
  27. "calculating kernels: 16836it [00:00, 17085.14it/s]\n",
  28. "\n",
  29. " --- shortest path kernel matrix of size 183 built in 1.2640743255615234 seconds ---\n",
  30. "\n",
  31. "the gram matrix with parameters {'node_kernels': {'symb': <function deltakernel at 0x7fe84734e598>, 'nsymb': <function gaussiankernel at 0x7fe84734e620>, 'mix': functools.partial(<function kernelproduct at 0x7fe84734e730>, <function deltakernel at 0x7fe84734e598>, <function gaussiankernel at 0x7fe84734e620>)}, '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.84it/s]\n",
  39. "\n",
  40. "4. Getting final performance...\n",
  41. "best_params_out: [{'node_kernels': {'symb': <function deltakernel at 0x7fe84734e598>, 'nsymb': <function gaussiankernel at 0x7fe84734e620>, 'mix': functools.partial(<function kernelproduct at 0x7fe84734e730>, <function deltakernel at 0x7fe84734e598>, <function gaussiankernel at 0x7fe84734e620>)}, 'n_jobs': 8, 'verbose': True}]\n",
  42. "best_params_in: [{'alpha': 1e-10}]\n",
  43. "\n",
  44. "best_val_perf: 9.687399048018559\n",
  45. "best_val_std: 0.654180946161292\n",
  46. "final_performance: [9.411656660355659]\n",
  47. "final_confidence: [2.500437167823725]\n",
  48. "train_performance: [6.168480355249007]\n",
  49. "train_std: [0.2541557651056269]\n",
  50. "\n",
  51. "time to calculate gram matrix with different hyper-params: 1.26±0.00s\n",
  52. "time to calculate best gram matrix: 1.26±0.00s\n",
  53. "total training time with all hyper-param choices: 5.09s\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. "getting sp graphs: 149it [00:00, 7096.72it/s]\n",
  72. "calculating kernels: 11175it [00:00, 19504.73it/s]\n",
  73. "\n",
  74. " --- shortest path kernel matrix of size 149 built in 0.7957959175109863 seconds ---\n",
  75. "\n",
  76. "the gram matrix with parameters {'node_kernels': {'symb': <function deltakernel at 0x7fe84734e598>, 'nsymb': <function gaussiankernel at 0x7fe84734e620>, 'mix': functools.partial(<function kernelproduct at 0x7fe84734e730>, <function deltakernel at 0x7fe84734e598>, <function gaussiankernel at 0x7fe84734e620>)}, 'n_jobs': 8, 'verbose': True} is: \n",
  77. "\n",
  78. "\n",
  79. "\n",
  80. "1 gram matrices are calculated, 0 of which are ignored.\n",
  81. "\n",
  82. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  83. "cross validation: 30it [00:02, 10.74it/s]\n",
  84. "\n",
  85. "4. Getting final performance...\n",
  86. "best_params_out: [{'node_kernels': {'symb': <function deltakernel at 0x7fe84734e598>, 'nsymb': <function gaussiankernel at 0x7fe84734e620>, 'mix': functools.partial(<function kernelproduct at 0x7fe84734e730>, <function deltakernel at 0x7fe84734e598>, <function gaussiankernel at 0x7fe84734e620>)}, 'n_jobs': 8, 'verbose': True}]\n",
  87. "best_params_in: [{'alpha': 1e-05}]\n",
  88. "\n",
  89. "best_val_perf: 8.745832519261795\n",
  90. "best_val_std: 0.26293501071192543\n",
  91. "final_performance: [7.942686332248635]\n",
  92. "final_confidence: [1.617779657027359]\n",
  93. "train_performance: [7.860965083396337]\n",
  94. "train_std: [0.16888913664254188]\n",
  95. "\n",
  96. "time to calculate gram matrix with different hyper-params: 0.80±0.00s\n",
  97. "time to calculate best gram matrix: 0.80±0.00s\n",
  98. "total training time with all hyper-param choices: 3.90s\n",
  99. "\n",
  100. "\n",
  101. "\n",
  102. "MAO\n",
  103. "\n",
  104. "--- This is a classification problem ---\n",
  105. "\n",
  106. "\n",
  107. "1. Loading dataset from file...\n",
  108. "\n",
  109. "2. Calculating gram matrices. This could take a while...\n",
  110. "\n",
  111. " None edge weight specified. Set all weight to 1.\n",
  112. "\n",
  113. "getting sp graphs: 68it [00:00, 2292.58it/s]\n",
  114. "calculating kernels: 2346it [00:02, 873.39it/s]\n",
  115. "\n",
  116. " --- shortest path kernel matrix of size 68 built in 2.986046075820923 seconds ---\n",
  117. "\n",
  118. "the gram matrix with parameters {'node_kernels': {'symb': <function deltakernel at 0x7fe84734e598>, 'nsymb': <function gaussiankernel at 0x7fe84734e620>, 'mix': functools.partial(<function kernelproduct at 0x7fe84734e730>, <function deltakernel at 0x7fe84734e598>, <function gaussiankernel at 0x7fe84734e620>)}, 'n_jobs': 8, 'verbose': True} is: \n",
  119. "\n",
  120. "\n",
  121. "\n",
  122. "1 gram matrices are calculated, 0 of which are ignored.\n",
  123. "\n",
  124. "3. Fitting and predicting using nested cross validation. This could really take a while...\n",
  125. "cross validation: 30it [00:02, 11.85it/s]\n",
  126. "\n",
  127. "4. Getting final performance...\n",
  128. "best_params_out: [{'node_kernels': {'symb': <function deltakernel at 0x7fe84734e598>, 'nsymb': <function gaussiankernel at 0x7fe84734e620>, 'mix': functools.partial(<function kernelproduct at 0x7fe84734e730>, <function deltakernel at 0x7fe84734e598>, <function gaussiankernel at 0x7fe84734e620>)}, 'n_jobs': 8, 'verbose': True}]\n",
  129. "best_params_in: [{'C': 3162.2776601683795}]\n",
  130. "\n",
  131. "best_val_perf: 0.8780158730158729\n",
  132. "best_val_std: 0.028162670831398482\n",
  133. "final_performance: [0.8714285714285714]\n",
  134. "final_confidence: [0.09446318571439967]\n",
  135. "train_performance: [0.9740729517396185]\n",
  136. "train_std: [0.007872630412568218]\n",
  137. "\n",
  138. "time to calculate gram matrix with different hyper-params: 2.99±0.00s\n",
  139. "time to calculate best gram matrix: 2.99±0.00s\n",
  140. "total training time with all hyper-param choices: 5.93s\n",
  141. "\n",
  142. "\n",
  143. "\n",
  144. "PAH\n",
  145. "\n",
  146. "--- This is a classification problem ---\n",
  147. "\n",
  148. "\n",
  149. "1. Loading dataset from file...\n",
  150. "\n",
  151. "2. Calculating gram matrices. This could take a while...\n",
  152. "\n",
  153. " None edge weight specified. Set all weight to 1.\n",
  154. "\n",
  155. "getting sp graphs: 94it [00:00, 2131.93it/s]\n",
  156. "calculating kernels: 1501it [00:01, 78.00it/s]"
  157. ]
  158. }
  159. ],
  160. "source": [
  161. "import functools\n",
  162. "from libs import *\n",
  163. "import multiprocessing\n",
  164. "\n",
  165. "from gklearn.kernels.spKernel import spkernel\n",
  166. "from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct\n",
  167. "#from gklearn.utils.model_selection_precomputed import trial_do\n",
  168. "\n",
  169. "# datasets\n",
  170. "dslist = [\n",
  171. " {'name': 'Acyclic', 'dataset': '../datasets/acyclic/dataset_bps.ds',\n",
  172. " 'task': 'regression'}, # node symb\n",
  173. " {'name': 'Alkane', 'dataset': '../datasets/Alkane/dataset.ds', 'task': 'regression',\n",
  174. " 'dataset_y': '../datasets/Alkane/dataset_boiling_point_names.txt'}, \n",
  175. " # contains single node graph, node symb\n",
  176. " {'name': 'MAO', 'dataset': '../datasets/MAO/dataset.ds'}, # node/edge symb\n",
  177. " {'name': 'PAH', 'dataset': '../datasets/PAH/dataset.ds'}, # unlabeled\n",
  178. " {'name': 'MUTAG', 'dataset': '../datasets/MUTAG/MUTAG_A.txt'}, # node/edge symb\n",
  179. " {'name': 'Letter-med', 'dataset': '../datasets/Letter-med/Letter-med_A.txt'},\n",
  180. " # node nsymb\n",
  181. " {'name': 'ENZYMES', 'dataset': '../datasets/ENZYMES_txt/ENZYMES_A_sparse.txt'},\n",
  182. " # node symb/nsymb\n",
  183. "# {'name': 'Mutagenicity', 'dataset': '../datasets/Mutagenicity/Mutagenicity_A.txt'},\n",
  184. "# # node/edge symb\n",
  185. "# {'name': 'D&D', 'dataset': '../datasets/DD/DD_A.txt'}, # node symb\n",
  186. "#\n",
  187. "# {'name': 'COIL-DEL', 'dataset': '../datasets/COIL-DEL/COIL-DEL_A.txt'}, # edge symb, node nsymb\n",
  188. "# {'name': 'BZR', 'dataset': '../datasets/BZR_txt/BZR_A_sparse.txt'}, # node symb/nsymb\n",
  189. "# {'name': 'COX2', 'dataset': '../datasets/COX2_txt/COX2_A_sparse.txt'}, # node symb/nsymb\n",
  190. "# {'name': 'Fingerprint', 'dataset': '../datasets/Fingerprint/Fingerprint_A.txt'},\n",
  191. "# {'name': 'DHFR', 'dataset': '../datasets/DHFR_txt/DHFR_A_sparse.txt'}, # node symb/nsymb\n",
  192. "# {'name': 'SYNTHETIC', 'dataset': '../datasets/SYNTHETIC_txt/SYNTHETIC_A_sparse.txt'}, # node symb/nsymb\n",
  193. "# {'name': 'MSRC9', 'dataset': '../datasets/MSRC_9_txt/MSRC_9_A.txt'}, # node symb\n",
  194. "# {'name': 'MSRC21', 'dataset': '../datasets/MSRC_21_txt/MSRC_21_A.txt'}, # node symb\n",
  195. "# {'name': 'FIRSTMM_DB', 'dataset': '../datasets/FIRSTMM_DB/FIRSTMM_DB_A.txt'}, # node symb/nsymb ,edge nsymb\n",
  196. "#\n",
  197. "# {'name': 'PROTEINS', 'dataset': '../datasets/PROTEINS_txt/PROTEINS_A_sparse.txt'}, # node symb/nsymb\n",
  198. "# {'name': 'PROTEINS_full', 'dataset': '../datasets/PROTEINS_full_txt/PROTEINS_full_A_sparse.txt'}, # node symb/nsymb\n",
  199. "# {'name': 'AIDS', 'dataset': '../datasets/AIDS/AIDS_A.txt'}, # node symb/nsymb, edge symb\n",
  200. "# {'name': 'NCI1', 'dataset': '../datasets/NCI1/NCI1.mat',\n",
  201. "# 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  202. "# {'name': 'NCI109', 'dataset': '../datasets/NCI109/NCI109.mat',\n",
  203. "# 'extra_params': {'am_sp_al_nl_el': [1, 1, 2, 0, -1]}}, # node symb\n",
  204. "# {'name': 'NCI-HIV', 'dataset': '../datasets/NCI-HIV/AIDO99SD.sdf',\n",
  205. "# 'dataset_y': '../datasets/NCI-HIV/aids_conc_may04.txt',}, # node/edge symb\n",
  206. "\n",
  207. " # # not working below\n",
  208. " # {'name': 'PTC_FM', 'dataset': '../datasets/PTC/Train/FM.ds',},\n",
  209. " # {'name': 'PTC_FR', 'dataset': '../datasets/PTC/Train/FR.ds',},\n",
  210. " # {'name': 'PTC_MM', 'dataset': '../datasets/PTC/Train/MM.ds',},\n",
  211. " # {'name': 'PTC_MR', 'dataset': '../datasets/PTC/Train/MR.ds',},\n",
  212. "]\n",
  213. "estimator = spkernel\n",
  214. "# hyper-parameters\n",
  215. "mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)\n",
  216. "param_grid_precomputed = {'node_kernels': [\n",
  217. " {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}]}\n",
  218. "param_grid = [{'C': np.logspace(-10, 10, num=41, base=10)},\n",
  219. " {'alpha': np.logspace(-10, 10, num=41, base=10)}]\n",
  220. "\n",
  221. "# for each dataset, do model selection.\n",
  222. "for ds in dslist:\n",
  223. " print()\n",
  224. " print(ds['name'])\n",
  225. " model_selection_for_precomputed_kernel(\n",
  226. " ds['dataset'],\n",
  227. " estimator,\n",
  228. " param_grid_precomputed,\n",
  229. " (param_grid[1] if ('task' in ds and ds['task']\n",
  230. " == 'regression') else param_grid[0]),\n",
  231. " (ds['task'] if 'task' in ds else 'classification'),\n",
  232. " NUM_TRIALS=30,\n",
  233. " datafile_y=(ds['dataset_y'] if 'dataset_y' in ds else None),\n",
  234. " extra_params=(ds['extra_params'] if 'extra_params' in ds else None),\n",
  235. " ds_name=ds['name'],\n",
  236. " n_jobs=multiprocessing.cpu_count(),\n",
  237. " read_gm_from_file=False,\n",
  238. " verbose=True)\n",
  239. " print()"
  240. ]
  241. }
  242. ],
  243. "metadata": {
  244. "kernelspec": {
  245. "display_name": "Python 3",
  246. "language": "python",
  247. "name": "python3"
  248. },
  249. "language_info": {
  250. "codemirror_mode": {
  251. "name": "ipython",
  252. "version": 3
  253. },
  254. "file_extension": ".py",
  255. "mimetype": "text/x-python",
  256. "name": "python",
  257. "nbconvert_exporter": "python",
  258. "pygments_lexer": "ipython3",
  259. "version": "3.6.7"
  260. }
  261. },
  262. "nbformat": 4,
  263. "nbformat_minor": 2
  264. }

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