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.

utils.py 4.8 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Sep 22 11:33:28 2020
  5. @author: ljia
  6. """
  7. import multiprocessing
  8. Graph_Kernel_List = ['PathUpToH', 'WLSubtree', 'SylvesterEquation', 'Marginalized', 'ShortestPath', 'Treelet', 'ConjugateGradient', 'FixedPoint', 'SpectralDecomposition', 'StructuralSP', 'CommonWalk']
  9. # Graph_Kernel_List = ['CommonWalk', 'Marginalized', 'SylvesterEquation', 'ConjugateGradient', 'FixedPoint', 'SpectralDecomposition', 'ShortestPath', 'StructuralSP', 'PathUpToH', 'Treelet', 'WLSubtree']
  10. Graph_Kernel_List_VSym = ['PathUpToH', 'WLSubtree', 'Marginalized', 'ShortestPath', 'Treelet', 'ConjugateGradient', 'FixedPoint', 'StructuralSP', 'CommonWalk']
  11. Graph_Kernel_List_ESym = ['PathUpToH', 'Marginalized', 'Treelet', 'ConjugateGradient', 'FixedPoint', 'StructuralSP', 'CommonWalk']
  12. Graph_Kernel_List_VCon = ['ShortestPath', 'ConjugateGradient', 'FixedPoint', 'StructuralSP']
  13. Graph_Kernel_List_ECon = ['ConjugateGradient', 'FixedPoint', 'StructuralSP']
  14. Dataset_List = ['Alkane', 'Acyclic', 'MAO', 'PAH', 'MUTAG', 'Letter-med', 'ENZYMES', 'AIDS', 'NCI1', 'NCI109', 'DD']
  15. def compute_graph_kernel(graphs, kernel_name, n_jobs=multiprocessing.cpu_count(), chunksize=None):
  16. if kernel_name == 'CommonWalk':
  17. from gklearn.kernels.commonWalkKernel import commonwalkkernel
  18. estimator = commonwalkkernel
  19. params = {'compute_method': 'geo', 'weight': 0.1}
  20. elif kernel_name == 'Marginalized':
  21. from gklearn.kernels.marginalizedKernel import marginalizedkernel
  22. estimator = marginalizedkernel
  23. params = {'p_quit': 0.5, 'n_iteration': 5, 'remove_totters': False}
  24. elif kernel_name == 'SylvesterEquation':
  25. from gklearn.kernels.randomWalkKernel import randomwalkkernel
  26. estimator = randomwalkkernel
  27. params = {'compute_method': 'sylvester', 'weight': 0.1}
  28. elif kernel_name == 'ConjugateGradient':
  29. from gklearn.kernels.randomWalkKernel import randomwalkkernel
  30. estimator = randomwalkkernel
  31. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  32. import functools
  33. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  34. sub_kernel = {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}
  35. params = {'compute_method': 'conjugate', 'weight': 0.1, 'node_kernels': sub_kernel, 'edge_kernels': sub_kernel}
  36. elif kernel_name == 'FixedPoint':
  37. from gklearn.kernels.randomWalkKernel import randomwalkkernel
  38. estimator = randomwalkkernel
  39. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  40. import functools
  41. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  42. sub_kernel = {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}
  43. params = {'compute_method': 'fp', 'weight': 1e-3, 'node_kernels': sub_kernel, 'edge_kernels': sub_kernel}
  44. elif kernel_name == 'SpectralDecomposition':
  45. from gklearn.kernels.randomWalkKernel import randomwalkkernel
  46. estimator = randomwalkkernel
  47. params = {'compute_method': 'spectral', 'sub_kernel': 'geo', 'weight': 0.1}
  48. elif kernel_name == 'ShortestPath':
  49. from gklearn.kernels.spKernel import spkernel
  50. estimator = spkernel
  51. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  52. import functools
  53. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  54. sub_kernel = {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}
  55. params = {'node_kernels': sub_kernel}
  56. elif kernel_name == 'StructuralSP':
  57. from gklearn.kernels.structuralspKernel import structuralspkernel
  58. estimator = structuralspkernel
  59. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  60. import functools
  61. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  62. sub_kernel = {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}
  63. params = {'node_kernels': sub_kernel, 'edge_kernels': sub_kernel}
  64. elif kernel_name == 'PathUpToH':
  65. from gklearn.kernels.untilHPathKernel import untilhpathkernel
  66. estimator = untilhpathkernel
  67. params = {'depth': 5, 'k_func': 'MinMax', 'compute_method': 'trie'}
  68. elif kernel_name == 'Treelet':
  69. from gklearn.kernels.treeletKernel import treeletkernel
  70. estimator = treeletkernel
  71. from gklearn.utils.kernels import polynomialkernel
  72. import functools
  73. sub_kernel = functools.partial(polynomialkernel, d=4, c=1e+8)
  74. params = {'sub_kernel': sub_kernel}
  75. elif kernel_name == 'WLSubtree':
  76. from gklearn.kernels.weisfeilerLehmanKernel import weisfeilerlehmankernel
  77. estimator = weisfeilerlehmankernel
  78. params = {'base_kernel': 'subtree', 'height': 5}
  79. # params['parallel'] = None
  80. params['n_jobs'] = n_jobs
  81. params['chunksize'] = chunksize
  82. params['verbose'] = True
  83. results = estimator(graphs, **params)
  84. return results[0], results[1]

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