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_sspkernel_for_syntheticnew.py 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.structuralspKernel import structuralspkernel
  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 = 'structuralspkernel'
  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. sub_kernels = {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}
  31. Kmatrix, run_time = structuralspkernel(Gn, node_label=None, edge_label=None,
  32. node_kernels=sub_kernels, edge_kernels=sub_kernels,
  33. parallel=None, # parallel='imap_unordered',
  34. n_jobs=multiprocessing.cpu_count(),
  35. verbose=True)
  36. # normalization
  37. Kmatrix_diag = Kmatrix.diagonal().copy()
  38. for i in range(len(Kmatrix)):
  39. for j in range(i, len(Kmatrix)):
  40. Kmatrix[i][j] /= np.sqrt(Kmatrix_diag[i] * Kmatrix_diag[j])
  41. Kmatrix[j][i] = Kmatrix[i][j]
  42. np.savez('results/xp_fit_method/Kmatrix.' + ds_name + '.' + gkernel + '.gm',
  43. Kmatrix=Kmatrix, run_time=run_time)
  44. print('complete!')

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