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.

marginalizedKernel.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import sys
  2. import pathlib
  3. sys.path.insert(0, "../")
  4. import networkx as nx
  5. import numpy as np
  6. import time
  7. from pygraph.kernels.deltaKernel import deltakernel
  8. def marginalizedkernel(*args):
  9. """Calculate marginalized graph kernels between graphs.
  10. Parameters
  11. ----------
  12. Gn : List of NetworkX graph
  13. List of graphs between which the kernels are calculated.
  14. /
  15. G1, G2 : NetworkX graphs
  16. 2 graphs between which the kernel is calculated.
  17. p_quit : integer
  18. the termination probability in the random walks generating step
  19. itr : integer
  20. time of iterations to calculate R_inf
  21. Return
  22. ------
  23. Kmatrix/Kernel : Numpy matrix/int
  24. Kernel matrix, each element of which is the marginalized kernel between 2 praphs. / Marginalized Kernel between 2 graphs.
  25. References
  26. ----------
  27. [1] H. Kashima, K. Tsuda, and A. Inokuchi. Marginalized kernels between labeled graphs. In Proceedings of the 20th International Conference on Machine Learning, Washington, DC, United States, 2003.
  28. """
  29. if len(args) == 3: # for a list of graphs
  30. Gn = args[0]
  31. Kmatrix = np.zeros((len(Gn), len(Gn)))
  32. start_time = time.time()
  33. for i in range(0, len(Gn)):
  34. for j in range(i, len(Gn)):
  35. Kmatrix[i][j] = _marginalizedkernel_do(Gn[i], Gn[j], args[1], args[2])
  36. Kmatrix[j][i] = Kmatrix[i][j]
  37. print("\n --- marginalized kernel matrix of size %d built in %s seconds ---" % (len(Gn), (time.time() - start_time)))
  38. return Kmatrix
  39. else: # for only 2 graphs
  40. start_time = time.time()
  41. kernel = _marginalizedkernel_do(args[0], args[1], args[2], args[3])
  42. print("\n --- marginalized kernel built in %s seconds ---" % (time.time() - start_time))
  43. return kernel
  44. def _marginalizedkernel_do(G1, G2, p_quit, itr):
  45. """Calculate marginalized graph kernels between 2 graphs.
  46. Parameters
  47. ----------
  48. G1, G2 : NetworkX graphs
  49. 2 graphs between which the kernel is calculated.
  50. p_quit : integer
  51. the termination probability in the random walks generating step
  52. itr : integer
  53. time of iterations to calculate R_inf
  54. Return
  55. ------
  56. Kernel : int
  57. Marginalized Kernel between 2 graphs.
  58. """
  59. # init parameters
  60. kernel = 0
  61. num_nodes_G1 = nx.number_of_nodes(G1)
  62. num_nodes_G2 = nx.number_of_nodes(G2)
  63. p_init_G1 = 1 / num_nodes_G1 # the initial probability distribution in the random walks generating step (uniform distribution over |G|)
  64. p_init_G2 = 1 / num_nodes_G2
  65. q = p_quit * p_quit
  66. r1 = q
  67. # initial R_inf
  68. R_inf = np.zeros([num_nodes_G1, num_nodes_G2]) # matrix to save all the R_inf for all pairs of nodes
  69. # calculate R_inf with a simple interative method
  70. for i in range(1, itr):
  71. R_inf_new = np.zeros([num_nodes_G1, num_nodes_G2])
  72. R_inf_new.fill(r1)
  73. # calculate R_inf for each pair of nodes
  74. for node1 in G1.nodes(data = True):
  75. neighbor_n1 = G1[node1[0]]
  76. p_trans_n1 = (1 - p_quit) / len(neighbor_n1) # the transition probability distribution in the random walks generating step (uniform distribution over the vertices adjacent to the current vertex)
  77. for node2 in G2.nodes(data = True):
  78. neighbor_n2 = G2[node2[0]]
  79. p_trans_n2 = (1 - p_quit) / len(neighbor_n2)
  80. for neighbor1 in neighbor_n1:
  81. for neighbor2 in neighbor_n2:
  82. t = p_trans_n1 * p_trans_n2 * \
  83. deltakernel(G1.node[neighbor1]['label'] == G2.node[neighbor2]['label']) * \
  84. deltakernel(neighbor_n1[neighbor1]['label'] == neighbor_n2[neighbor2]['label'])
  85. R_inf_new[node1[0]][node2[0]] += t * R_inf[neighbor1][neighbor2] # ref [1] equation (8)
  86. R_inf[:] = R_inf_new
  87. # add elements of R_inf up and calculate kernel
  88. for node1 in G1.nodes(data = True):
  89. for node2 in G2.nodes(data = True):
  90. s = p_init_G1 * p_init_G2 * deltakernel(node1[1]['label'] == node2[1]['label'])
  91. kernel += s * R_inf[node1[0]][node2[0]] # ref [1] equation (6)
  92. return kernel

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