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 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Those who are not graph kernels. We can be kernels for nodes or edges!
  2. """
  3. def deltakernel(x, y):
  4. """Delta kernel. Return 1 if x == y, 0 otherwise.
  5. Parameters
  6. ----------
  7. x, y : any
  8. Two parts to compare.
  9. Return
  10. ------
  11. kernel : integer
  12. Delta kernel.
  13. References
  14. ----------
  15. [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.
  16. """
  17. return x == y #(1 if condition else 0)
  18. def gaussiankernel(x, y):
  19. """Gaussian kernel. Use sklearn.metrics.pairwise.rbf_kernel instead.
  20. """
  21. pass
  22. def kernelsum(k1, k2, d11, d12, d21=None, d22=None, lamda1=1, lamda2=1):
  23. """Sum of a pair of kernels.
  24. k = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  25. Parameters
  26. ----------
  27. k1, k2 : function
  28. A pair of kernel functions.
  29. d11, d12:
  30. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  31. d21, d22:
  32. Inputs of k2.
  33. lamda1, lamda2: float
  34. Coefficients of the product.
  35. Return
  36. ------
  37. kernel : integer
  38. """
  39. if d21 == None or d22 == None:
  40. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d11, d12)
  41. else:
  42. kernel = lamda1 * k1(d11, d12) + lamda2 * k2(d21, d22)
  43. return kernel
  44. def kernelproduct(k1, k2, d11, d12, d21=None, d22=None, lamda=1):
  45. """Product of a pair of kernels.
  46. k = lamda * k1(d11, d12) * k2(d21, d22)
  47. Parameters
  48. ----------
  49. k1, k2 : function
  50. A pair of kernel functions.
  51. d11, d12:
  52. Inputs of k1. If d21 or d22 is None, apply d11, d12 to both k1 and k2.
  53. d21, d22:
  54. Inputs of k2.
  55. lamda: float
  56. Coefficient of the product.
  57. Return
  58. ------
  59. kernel : integer
  60. """
  61. if d21 == None or d22 == None:
  62. kernel = lamda * k1(d11, d12) * k2(d11, d12)
  63. else:
  64. kernel = lamda * k1(d11, d12) * k2(d21, d22)
  65. return kernel

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