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.

kernels.py 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. """Those who are not graph kernels. We can be kernels for nodes or edges!
  2. These kernels are defined between pairs of vectors.
  3. """
  4. import numpy as np
  5. def kronecker_delta_kernel(x, y):
  6. """Delta kernel. Return 1 if x == y, 0 otherwise.
  7. Parameters
  8. ----------
  9. x, y : any
  10. Two parts to compare.
  11. Return
  12. ------
  13. kernel : integer
  14. Delta kernel.
  15. References
  16. ----------
  17. [1] H. Kashima, K. Tsuda, and A. Inokuchi. Marginalized kernels between
  18. labeled graphs. In Proceedings of the 20th International Conference on
  19. Machine Learning, Washington, DC, United States, 2003.
  20. """
  21. return (1 if np.array_equal(x, y) else 0)
  22. def delta_kernel(x, y):
  23. return x == y #(1 if condition else 0)
  24. def deltakernel(x, y):
  25. return delta_kernel(x, y)
  26. def gaussian_kernel(x, y, gamma=None):
  27. """Gaussian kernel.
  28. Compute the rbf (gaussian) kernel between x and y:
  29. K(x, y) = exp(-gamma ||x-y||^2).
  30. Read more in the `User Guide of scikit-learn library <https://scikit-learn.org/stable/modules/metrics.html#rbf-kernel>`__.
  31. Parameters
  32. ----------
  33. x, y : array
  34. gamma : float, default None
  35. If None, defaults to 1.0 / n_features
  36. Returns
  37. -------
  38. kernel : float
  39. """
  40. if gamma is None:
  41. gamma = 1.0 / len(x)
  42. # xt = np.array([float(itm) for itm in x]) # @todo: move this to dataset or datafile to speed up.
  43. # yt = np.array([float(itm) for itm in y])
  44. # kernel = xt - yt
  45. # kernel = kernel ** 2
  46. # kernel = np.sum(kernel)
  47. # kernel *= -gamma
  48. # kernel = np.exp(kernel)
  49. # return kernel
  50. return np.exp((np.sum(np.subtract(x, y) ** 2)) * -gamma)
  51. def gaussiankernel(x, y, gamma=None):
  52. return gaussian_kernel(x, y, gamma=gamma)
  53. def polynomial_kernel(x, y, gamma=1, coef0=0, d=1):
  54. return (np.dot(x, y) * gamma + coef0) ** d
  55. def highest_polynomial_kernel(x, y, d=1, c=0):
  56. """Polynomial kernel.
  57. Compute the polynomial kernel between x and y:
  58. K(x, y) = <x, y> ^d + c.
  59. Parameters
  60. ----------
  61. x, y : array
  62. d : integer, default 1
  63. c : float, default 0
  64. Returns
  65. -------
  66. kernel : float
  67. """
  68. return np.dot(x, y) ** d + c
  69. def polynomialkernel(x, y, d=1, c=0):
  70. return highest_polynomial_kernel(x, y, d=d, c=c)
  71. def linear_kernel(x, y):
  72. """Polynomial kernel.
  73. Compute the polynomial kernel between x and y:
  74. K(x, y) = <x, y>.
  75. Parameters
  76. ----------
  77. x, y : array
  78. d : integer, default 1
  79. c : float, default 0
  80. Returns
  81. -------
  82. kernel : float
  83. """
  84. return np.dot(x, y)
  85. def linearkernel(x, y):
  86. return linear_kernel(x, y)
  87. def cosine_kernel(x, y):
  88. return np.dot(x, y) / (np.linalg.norm(x) * np.linalg.norm(y))
  89. def sigmoid_kernel(x, y, gamma=None, coef0=1):
  90. if gamma is None:
  91. gamma = 1.0 / len(x)
  92. k = np.dot(x, y)
  93. k *= gamma
  94. k += coef0
  95. k = np.tanh(k)
  96. # k = np.tanh(k, k) # compute tanh in-place
  97. return k
  98. def laplacian_kernel(x, y, gamma=None):
  99. if gamma is None:
  100. gamma = 1.0 / len(x)
  101. k = -gamma * np.linalg.norm(np.subtract(x, y))
  102. k = np.exp(k)
  103. return k
  104. def chi2_kernel(x, y, gamma=1.0):
  105. k = np.divide(np.subtract(x, y) ** 2, np.add(x, y))
  106. k = np.sum(k)
  107. k *= -gamma
  108. return np.exp(k)
  109. def exponential_kernel(x, y, gamma=None):
  110. if gamma is None:
  111. gamma = 1.0 / len(x)
  112. return np.exp(np.dot(x, y) * gamma)
  113. def intersection_kernel(x, y):
  114. return np.sum(np.minimum(x, y))
  115. def multiquadratic_kernel(x, y, c=0):
  116. return np.sqrt((np.sum(np.subtract(x, y) ** 2)) + c)
  117. def inverse_multiquadratic_kernel(x, y, c=0):
  118. return 1 / multiquadratic_kernel(x, y, c=c)
  119. def kernelsum(k1, k2, d11, d12, d21=None, d22=None, lamda1=1, lamda2=1):
  120. """Sum of a pair of kernels.
  121. k = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  122. Parameters
  123. ----------
  124. k1, k2 : function
  125. A pair of kernel functions.
  126. d11, d12:
  127. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  128. d21, d22:
  129. Inputs of k2.
  130. lamda1, lamda2: float
  131. Coefficients of the product.
  132. Return
  133. ------
  134. kernel : integer
  135. """
  136. if d21 == None or d22 == None:
  137. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d11, d12)
  138. else:
  139. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  140. return kernel
  141. def kernelproduct(k1, k2, d11, d12, d21=None, d22=None, lamda=1):
  142. """Product of a pair of kernels.
  143. k = lamda * k1(d11, d12) * k2(d21, d22)
  144. Parameters
  145. ----------
  146. k1, k2 : function
  147. A pair of kernel functions.
  148. d11, d12:
  149. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  150. d21, d22:
  151. Inputs of k2.
  152. lamda: float
  153. Coefficient of the product.
  154. Return
  155. ------
  156. kernel : integer
  157. """
  158. if d21 == None or d22 == None:
  159. kernel = lamda * k1(d11, d12) * k2(d11, d12)
  160. else:
  161. kernel = lamda * k1(d11, d12) * k2(d21, d22)
  162. return kernel
  163. if __name__ == '__main__':
  164. o = polynomialkernel([1, 2], [3, 4], 2, 3)

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