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_spkernel_for_syntheticnew.py 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sun Dec 23 16:40:52 2018
  5. @author: ljia
  6. """
  7. import sys
  8. import numpy as np
  9. import networkx as nx
  10. sys.path.insert(0, "../")
  11. from gklearn.utils.graphfiles import loadDataset
  12. from gklearn.utils.model_selection_precomputed import compute_gram_matrices
  13. from gklearn.kernels.spKernel import spkernel
  14. from sklearn.model_selection import ParameterGrid
  15. from libs import *
  16. import multiprocessing
  17. import functools
  18. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  19. if __name__ == "__main__":
  20. # load dataset.
  21. print('getting dataset and computing kernel distance matrix first...')
  22. ds_name = 'SYNTHETICnew'
  23. gkernel = 'spkernel'
  24. dataset = '../datasets/SYNTHETICnew/SYNTHETICnew_A.txt'
  25. Gn, y_all = loadDataset(dataset)
  26. for G in Gn:
  27. G.graph['filename'] = 'graph' + str(G.graph['name']) + '.gxl'
  28. # compute/read Gram matrix and pair distances.
  29. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  30. Kmatrix = np.empty((len(Gn), len(Gn)))
  31. Kmatrix, run_time, idx = spkernel(Gn, node_label=None, node_kernels=
  32. {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel},
  33. n_jobs=multiprocessing.cpu_count(), verbose=True)
  34. # normalization
  35. Kmatrix_diag = Kmatrix.diagonal().copy()
  36. for i in range(len(Kmatrix)):
  37. for j in range(i, len(Kmatrix)):
  38. Kmatrix[i][j] /= np.sqrt(Kmatrix_diag[i] * Kmatrix_diag[j])
  39. Kmatrix[j][i] = Kmatrix[i][j]
  40. np.savez('results/xp_fit_method/Kmatrix.' + ds_name + '.' + gkernel + '.gm',
  41. Kmatrix=Kmatrix, run_time=run_time)
  42. print('complete!')

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