├── Clusters.png
├── DistanceMatrixBeforeSorting.png
├── EigenvectorOnData.png
├── FiedlerVector.png
├── FiedlerVectorLaplacian.py
├── InputData.png
├── LICENSE
├── PointDistance.png
├── PytorchConnectivityGraph.png
├── PytorchFiedlerVector.png
├── PytorchInputData.png
├── Pytorchclusters.png
├── README.md
├── Sorted_matrix.png
├── demo.py
└── diffusion_map.py
/Clusters.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/Clusters.png
--------------------------------------------------------------------------------
/DistanceMatrixBeforeSorting.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/DistanceMatrixBeforeSorting.png
--------------------------------------------------------------------------------
/EigenvectorOnData.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/EigenvectorOnData.png
--------------------------------------------------------------------------------
/FiedlerVector.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/FiedlerVector.png
--------------------------------------------------------------------------------
/FiedlerVectorLaplacian.py:
--------------------------------------------------------------------------------
1 | """
2 | % -------------------------------------------------------------
3 | % Matlab code
4 | % -------------------------------------------------------------
5 | % grpah partition using the eigenvector corresponding to the second
6 | % smallest eigenvalue
7 | % grpah partition using the eigenvector corresponding to the second
8 | % smallest eigenvalue
9 | t=[randn(500,2)+repmat([-2,-2],500,1) ;randn(500,2)+repmat([2,2],500,1)];
10 | scatter(t(:,1),t(:,2))
11 | W=squareform(pdist(t));
12 | A=W<3; % create adjacency matrix (set connected notes equal to one)
13 | D = sum(A,1);
14 | L = diag(D)-A;
15 | Lsym = diag(D.^-0.5)*L*diag(D.^-0.5);
16 | [u,s,v] = svd(Lsym);
17 |
18 | figure; plot(u(:, (end-1)))
19 | F = u(:, (end-1));
20 | plot(F);title('Second smallest non-zero eigenvalue eigenvector');
21 | scatter(t(F<0,1),t(F<0,2),'bo','filled');hold on
22 | scatter(t(F>0,1),t(F>0,2),'go','filled');
23 | """
24 | # Pytorch equivalent code
25 | import torch
26 | from torch.autograd import Variable
27 |
28 |
29 | import numpy as np
30 | import matplotlib.pyplot as plt
31 | import matplotlib.cm as cm
32 |
33 |
34 | import matplotlib.colors as colors
35 | import matplotlib.cm as cm
36 | import matplotlib as mpl
37 |
38 |
39 | color_map = plt.get_cmap('jet')
40 |
41 | def distance_matrix(mat):
42 | d= ((mat.unsqueeze (0)-mat.unsqueeze (1))**2).sum (2)**0.5
43 | return d
44 |
45 |
46 | # Generate Clusters
47 | mat = torch.cat([torch.randn(500,2)+torch.Tensor([-2,-3]), torch.randn(500,2)+torch.Tensor([2,1])])
48 | plt.scatter(mat[:,0].numpy(),mat[:,1].numpy())
49 | plt.show(block=False)
50 | ##-------------------------------------------
51 | # Compute distance matrix and then the Laplacian
52 | ##-------------------------------------------
53 | d= distance_matrix(mat);
54 | da=d<2;
55 | plt.figure()
56 | plt.imshow(da.numpy())
57 | plt.show(block=False)
58 |
59 | D= ((da.float()).sum(1)).diag()
60 | L = D -da.float()
61 | plt.figure()
62 | plt.title("Laplacian")
63 | plt.imshow(L.numpy())
64 | plt.show(block=False)
65 |
66 |
67 |
68 | Lsym=torch.mm(torch.mm(torch.diag(torch.pow(torch.diag(D),-0.5)),L),torch.diag(torch.pow(torch.diag(D),-0.5)));
69 | plt.figure()
70 | plt.imshow(Lsym.numpy())
71 | plt.title("Symmetric Laplacian")
72 | plt.show(block=False)
73 |
74 |
75 | [u,s,v]=torch.svd(Lsym)
76 |
77 | # plot fiedler vector
78 |
79 | plt.figure()
80 | plt.title('Fiedler vector')
81 | plt.plot(u[:,-2].numpy());
82 | plt.show(block=False)
83 | norm = colors.Normalize(vmin=-1, vmax=1)
84 |
85 | scalarMap = cm.ScalarMappable( norm=norm , cmap=color_map)
86 |
87 |
88 | plt.figure()
89 | plt.title('clusters')
90 | for i in range(len(u[:,-2])):
91 | if u[i,-2]<0:
92 | color = scalarMap.to_rgba(-1)
93 | plt.scatter(mat[i,0],mat[i,1], color=color,marker='o')
94 | else:
95 | color = scalarMap.to_rgba(1)
96 | plt.scatter(mat[i,0],mat[i,1], color=color,marker='*')
97 |
98 | plt.show(block=False)
99 |
100 | raw_input("Press Enter to exit..")
101 | plt.close('all')
102 |
--------------------------------------------------------------------------------
/InputData.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/InputData.png
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Dimitris Kastaniotis
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/PointDistance.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/PointDistance.png
--------------------------------------------------------------------------------
/PytorchConnectivityGraph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/PytorchConnectivityGraph.png
--------------------------------------------------------------------------------
/PytorchFiedlerVector.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/PytorchFiedlerVector.png
--------------------------------------------------------------------------------
/PytorchInputData.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/PytorchInputData.png
--------------------------------------------------------------------------------
/Pytorchclusters.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dimkastan/PyTorch-Spectral-clustering/6f08aaf511d9ee55def55b04d8a555b37318fd0e/Pytorchclusters.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PyTorch-Spectral-clustering
2 | [Under development]- Implementation of various methods for dimensionality reduction and spectral clustering with PyTorch and Matlab equivalent code.
3 |
4 | Sample Images from PyTorch code
5 |
6 |
7 |
8 |
9 |
10 |
23 |
24 |
25 |
26 |