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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 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 x == y #(1 if condition else 0)
  22. def deltakernel(x, y):
  23. return delta_kernel(x, y)
  24. def gaussian_kernel(x, y, gamma=None):
  25. """Gaussian kernel.
  26. Compute the rbf (gaussian) kernel between x and y:
  27. K(x, y) = exp(-gamma ||x-y||^2).
  28. Read more in the `User Guide of scikit-learn library <https://scikit-learn.org/stable/modules/metrics.html#rbf-kernel>`__.
  29. Parameters
  30. ----------
  31. x, y : array
  32. gamma : float, default None
  33. If None, defaults to 1.0 / n_features
  34. Returns
  35. -------
  36. kernel : float
  37. """
  38. if gamma is None:
  39. gamma = 1.0 / len(x)
  40. # xt = np.array([float(itm) for itm in x]) # @todo: move this to dataset or datafile to speed up.
  41. # yt = np.array([float(itm) for itm in y])
  42. # kernel = xt - yt
  43. # kernel = kernel ** 2
  44. # kernel = np.sum(kernel)
  45. # kernel *= -gamma
  46. # kernel = np.exp(kernel)
  47. # return kernel
  48. return np.exp((np.sum(np.subtract(x, y) ** 2)) * -gamma)
  49. def gaussiankernel(x, y, gamma=None):
  50. return gaussian_kernel(x, y, gamma=gamma)
  51. def polynomial_kernel(x, y, gamma=1, coef0=0, d=1):
  52. return (np.dot(x, y) * gamma + coef0) ** d
  53. def highest_polynomial_kernel(x, y, d=1, c=0):
  54. """Polynomial kernel.
  55. Compute the polynomial kernel between x and y:
  56. K(x, y) = <x, y> ^d + c.
  57. Parameters
  58. ----------
  59. x, y : array
  60. d : integer, default 1
  61. c : float, default 0
  62. Returns
  63. -------
  64. kernel : float
  65. """
  66. return np.dot(x, y) ** d + c
  67. def polynomialkernel(x, y, d=1, c=0):
  68. return highest_polynomial_kernel(x, y, d=d, c=c)
  69. def linear_kernel(x, y):
  70. """Polynomial kernel.
  71. Compute the polynomial kernel between x and y:
  72. K(x, y) = <x, y>.
  73. Parameters
  74. ----------
  75. x, y : array
  76. d : integer, default 1
  77. c : float, default 0
  78. Returns
  79. -------
  80. kernel : float
  81. """
  82. return np.dot(x, y)
  83. def linearkernel(x, y):
  84. return linear_kernel(x, y)
  85. def cosine_kernel(x, y):
  86. return np.dot(x, y) / (np.abs(x) * np.abs(y))
  87. def sigmoid_kernel(x, y, gamma=None, coef0=1):
  88. if gamma is None:
  89. gamma = 1.0 / len(x)
  90. k = np.dot(x, y)
  91. k *= gamma
  92. k += coef0
  93. k = np.tanh(k)
  94. # k = np.tanh(k, k) # compute tanh in-place
  95. return k
  96. def laplacian_kernel(x, y, gamma=None):
  97. if gamma is None:
  98. gamma = 1.0 / len(x)
  99. k = -gamma * np.abs(np.subtract(x, y))
  100. k = np.exp(k)
  101. return k
  102. def chi2_kernel(x, y, gamma=1.0):
  103. k = np.divide(np.subtract(x, y) ** 2, np.add(x, y))
  104. k = np.sum(k)
  105. k *= -gamma
  106. return np.exp(k)
  107. def exponential_kernel(x, y, gamma=None):
  108. if gamma is None:
  109. gamma = 1.0 / len(x)
  110. return np.exp(np.dot(x, y) * gamma)
  111. def intersection_kernel(x, y):
  112. return np.sum(np.minimum(x, y))
  113. def multiquadratic_kernel(x, y, c=0):
  114. return np.sqrt((np.sum(np.subtract(x, y) ** 2)) + c)
  115. def inverse_multiquadratic_kernel(x, y, c=0):
  116. return 1 / multiquadratic_kernel(x, y, c=c)
  117. def kernelsum(k1, k2, d11, d12, d21=None, d22=None, lamda1=1, lamda2=1):
  118. """Sum of a pair of kernels.
  119. k = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  120. Parameters
  121. ----------
  122. k1, k2 : function
  123. A pair of kernel functions.
  124. d11, d12:
  125. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  126. d21, d22:
  127. Inputs of k2.
  128. lamda1, lamda2: float
  129. Coefficients of the product.
  130. Return
  131. ------
  132. kernel : integer
  133. """
  134. if d21 == None or d22 == None:
  135. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d11, d12)
  136. else:
  137. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  138. return kernel
  139. def kernelproduct(k1, k2, d11, d12, d21=None, d22=None, lamda=1):
  140. """Product of a pair of kernels.
  141. k = lamda * k1(d11, d12) * k2(d21, d22)
  142. Parameters
  143. ----------
  144. k1, k2 : function
  145. A pair of kernel functions.
  146. d11, d12:
  147. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  148. d21, d22:
  149. Inputs of k2.
  150. lamda: float
  151. Coefficient of the product.
  152. Return
  153. ------
  154. kernel : integer
  155. """
  156. if d21 == None or d22 == None:
  157. kernel = lamda * k1(d11, d12) * k2(d11, d12)
  158. else:
  159. kernel = lamda * k1(d11, d12) * k2(d21, d22)
  160. return kernel
  161. if __name__ == '__main__':
  162. o = polynomialkernel([1, 2], [3, 4], 2, 3)

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