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.

compute_graph_kernel.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. """compute_graph_kernel.ipynb
  3. Automatically generated by Colaboratory.
  4. Original file is located at
  5. https://colab.research.google.com/drive/17Q2QCl9CAtDweGF8LiWnWoN2laeJqT0u
  6. **This script demonstrates how to compute a graph kernel.**
  7. ---
  8. **0. Install `graphkit-learn`.**
  9. """
  10. """**1. Get dataset.**"""
  11. from gklearn.utils import Dataset
  12. # Predefined dataset name, use dataset "MUTAG".
  13. ds_name = 'MUTAG'
  14. # Initialize a Dataset.
  15. dataset = Dataset()
  16. # Load predefined dataset "MUTAG".
  17. dataset.load_predefined_dataset(ds_name)
  18. len(dataset.graphs)
  19. """**2. Compute graph kernel.**"""
  20. from gklearn.kernels import PathUpToH
  21. # Initailize parameters for graph kernel computation.
  22. kernel_options = {'depth': 3,
  23. 'k_func': 'MinMax',
  24. 'compute_method': 'trie'
  25. }
  26. # Initialize graph kernel.
  27. graph_kernel = PathUpToH(node_labels=dataset.node_labels, # list of node label names.
  28. edge_labels=dataset.edge_labels, # list of edge label names.
  29. ds_infos=dataset.get_dataset_infos(keys=['directed']), # dataset information required for computation.
  30. **kernel_options, # options for computation.
  31. )
  32. print('done.')
  33. import multiprocessing
  34. import matplotlib.pyplot as plt
  35. # Compute Gram matrix.
  36. gram_matrix, run_time = graph_kernel.compute(dataset.graphs,
  37. parallel='imap_unordered', # or None.
  38. n_jobs=multiprocessing.cpu_count(), # number of parallel jobs.
  39. normalize=True, # whether to return normalized Gram matrix.
  40. verbose=2 # whether to print out results.
  41. )
  42. # Print results.
  43. print()
  44. print(gram_matrix)
  45. print(run_time)
  46. plt.imshow(gram_matrix)
  47. import multiprocessing
  48. # Compute grah kernels between a graph and a list of graphs.
  49. kernel_list, run_time = graph_kernel.compute(dataset.graphs, # a list of graphs.
  50. dataset.graphs[0], # a single graph.
  51. parallel='imap_unordered', # or None.
  52. n_jobs=multiprocessing.cpu_count(), # number of parallel jobs.
  53. verbose=2 # whether to print out results.
  54. )
  55. # Print results.
  56. print()
  57. print(kernel_list)
  58. print(run_time)
  59. import multiprocessing
  60. # Compute a grah kernel between two graphs.
  61. kernel, run_time = graph_kernel.compute(dataset.graphs[0], # a single graph.
  62. dataset.graphs[1], # another single graph.
  63. verbose=2 # whether to print out results.
  64. )
  65. # Print results.
  66. print()
  67. print(kernel)
  68. print(run_time)

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