Skip to content

Commit 0eac1fc

Browse files
committed
Add MLGKernel
1 parent 8b83cec commit 0eac1fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+9129
-0
lines changed

MLGkernel/LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
-----------------------------------------------------------------------------
3+
4+
MLGkernel is an open source implementation of the Multiscale Laplacian Graph
5+
Kernel for computing the gram matrix of a collection of graphs.
6+
7+
Copyright (C) 2016 Imre Risi Kondor, Horace Pan
8+
9+
10+
This program is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU General Public License
12+
as published by the Free Software Foundation; either version 2
13+
of the License, or (at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program; if not, see <http://www.gnu.org/licenses/>.
22+
23+
----------------------------------------------------------------------------- */
24+
25+
26+
27+
#include"FLGinstance.hpp"
28+
29+
30+
void FLGinstance::precompute(const double gamma){
31+
if(Sinv.nrows>0) return;
32+
33+
//cout<<"L="<<endl<<L<<endl;
34+
//cout<<"U="<<endl<<U<<endl;
35+
36+
Cmatrix Linv=invert(L);
37+
//cout<<"Linv="<<endl<<Linv<<endl;
38+
39+
//int n=Linv.nrows;
40+
//assert(labels.size()==n);
41+
//int m=labels[0].n;
42+
43+
//Cmatrix U(n,m);
44+
//for(int i=0; i<n; i++)
45+
// for(int j=0; j<m; j++)
46+
// U(i,j)=labels[i](j);
47+
Cmatrix S=U.dot(Linv*U);
48+
for(int i=0; i<S.nrows; i++) S(i,i)+=gamma;
49+
//cout<<"S="<<endl<<S<<endl;
50+
51+
//Sinv=invert(S,0,&detS)*0.5;
52+
Sinv=invert(S,0,&log_detS)*0.5;
53+
//cout<<"Sinv="<<endl<<Sinv<<endl;
54+
}
55+
56+
57+
58+
Cmatrix FLGinstance::invert(const Cmatrix& M, const int _maxrank, double* detp) const{
59+
60+
pair<Cmatrix*,Cvector*> eigenp=M.symmetricEigensolver();
61+
Cmatrix& eigs=*eigenp.first;
62+
Cvector& lambda=*eigenp.second;
63+
int n=M.nrows;
64+
//cout<<"eigs="<<eigs<<endl;
65+
66+
int maxrank=min(_maxrank,n);
67+
if(maxrank==0) maxrank=n;
68+
for(int i=0; i<lambda.n-1; i++)
69+
if(lambda(i+1)<lambda(i)) cout<<"WARNING: eigenvalues not sorted in FLGinstance::invert"<<endl;
70+
for(int i=0; i<maxrank; i++)
71+
if(lambda(n-1-i)<10e-5*lambda(n-1)) maxrank=i;
72+
if(detp != nullptr) maxrank = n;
73+
Cmatrix R=Cmatrix::Zero(n,n);
74+
//cout<<"maxrank="<<maxrank<<endl;
75+
for(int i=0; i<n; i++)
76+
for(int j=0; j<n; j++)
77+
for(int p=0; p<maxrank; p++)
78+
R(i,j)+=eigs(i,n-1-p)*eigs(j,n-1-p)/lambda(n-1-p); // CORRECTED
79+
80+
//if(detp!=nullptr){
81+
// *detp=1.0; for(int i=0; i<lambda.n; i++) (*detp)*=lambda(i);
82+
//}
83+
if(detp!=nullptr){
84+
*detp=0.0; for(int i=0; i<lambda.n; i++) (*detp)+=log(lambda(i));
85+
}
86+
87+
delete eigenp.first;
88+
delete eigenp.second;
89+
90+
return R;
91+
}
92+
93+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
-----------------------------------------------------------------------------
3+
4+
MLGkernel is an open source implementation of the Multiscale Laplacian Graph
5+
Kernel for computing the gram matrix of a collection of graphs.
6+
7+
Copyright (C) 2016 Imre Risi Kondor, Horace Pan
8+
9+
10+
This program is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU General Public License
12+
as published by the Free Software Foundation; either version 2
13+
of the License, or (at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program; if not, see <http://www.gnu.org/licenses/>.
22+
23+
----------------------------------------------------------------------------- */
24+
25+
26+
27+
#ifndef _FLGinstance
28+
#define _FLGinstance
29+
30+
#include "Cmatrix.hpp"
31+
#include "Cvector.hpp"
32+
33+
34+
class FLGinstance{
35+
public:
36+
37+
//FLGinstance(Cmatrix&& _L, vector<Cvector>&& _labels):
38+
// L(move(_L)), labels(_labels){};
39+
40+
FLGinstance(){}
41+
42+
FLGinstance(Cmatrix&& _L, Cmatrix&& _U):
43+
L(move(_L)), U(move(_U)){};
44+
45+
46+
public:
47+
48+
void precompute(const double gamma);
49+
50+
bool operator==(const FLGinstance& x) const{
51+
if(L!=x.L) return false;
52+
if(U!=x.U) return false;
53+
//if(labels.size()!=x.labels.size()) return false;
54+
//if(labels!=x.labels) return false;
55+
return true;
56+
}
57+
58+
string str(){
59+
ostringstream oss; oss<<L<<U<<endl; return oss.str();}
60+
61+
private:
62+
63+
Cmatrix invert(const Cmatrix& M, const int _maxrank=0, double* detp=nullptr) const;
64+
65+
public:
66+
67+
Cmatrix L;
68+
//vector<Cvector> labels;
69+
Cmatrix U;
70+
71+
Cmatrix Sinv; // actually Sinv/2
72+
//double detS;
73+
double log_detS;
74+
75+
// Cvector linearization;
76+
77+
};
78+
79+
80+
81+
82+
namespace std{
83+
template<>
84+
class hash<FLGinstance>{
85+
public:
86+
size_t operator()(const FLGinstance& x) const{
87+
size_t h=hash<Cmatrix>()(x.L)^hash<Cmatrix>()(x.U);
88+
//for(auto& p: G.labels) h=(h<<1)^hash<Cvector>()(p);
89+
return h;
90+
}
91+
};
92+
};
93+
94+
95+
#endif

MLGkernel/MLGkernel/FLGkernel.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
-----------------------------------------------------------------------------
3+
4+
MLGkernel is an open source implementation of the Multiscale Laplacian Graph
5+
Kernel for computing the gram matrix of a collection of graphs.
6+
7+
Copyright (C) 2016 Imre Risi Kondor, Horace Pan
8+
9+
10+
This program is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU General Public License
12+
as published by the Free Software Foundation; either version 2
13+
of the License, or (at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program; if not, see <http://www.gnu.org/licenses/>.
22+
23+
----------------------------------------------------------------------------- */
24+
25+
26+
27+
#include"FLGkernel.hpp"
28+
29+
30+
double FLGkernel::operator()(const FLGinstance& x1, const FLGinstance& x2) const
31+
{
32+
33+
if(x1.Sinv.nrows==0) const_cast<FLGinstance&>(x1).precompute(gamma);
34+
if(x2.Sinv.nrows==0) const_cast<FLGinstance&>(x2).precompute(gamma);
35+
36+
Cvector lambda=(x1.Sinv+x2.Sinv).eigenvalues();
37+
//double detS=1; for(int i=0; i<lambda.n; i++) detS*=lambda(i); detS=1.0/detS;
38+
double log_detS=0; for(int i=0; i<lambda.n; i++) log_detS-=log(lambda(i));
39+
40+
//double r=sqrt(detS/sqrt(x1.detS*x2.detS));
41+
double logr=(log_detS-0.5*(x1.log_detS+x2.log_detS))/2;
42+
double r=0;
43+
if(logr<-30){cout<<"Underflow!"<<endl;}
44+
else r=exp(logr);
45+
/**
46+
cout << "x1.sinv" << endl;
47+
cout << x1.Sinv << endl;
48+
49+
cout << "x2.sinv" << endl;
50+
cout << x2.Sinv << endl;
51+
52+
cout << "x1.logdets" << endl;
53+
cout << x1.log_detS << endl;
54+
55+
cout << "x2.logdets" << endl;
56+
cout << x2.log_detS << endl;
57+
cout << "log dets" << endl;
58+
cout << log_detS << endl;
59+
**/
60+
//cout<<"k(.,.)="<<r<<endl;
61+
return r;
62+
}
63+

MLGkernel/MLGkernel/FLGkernel.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
-----------------------------------------------------------------------------
3+
4+
MLGkernel is an open source implementation of the Multiscale Laplacian Graph
5+
Kernel for computing the gram matrix of a collection of graphs.
6+
7+
Copyright (C) 2016 Imre Risi Kondor, Horace Pan
8+
9+
10+
This program is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU General Public License
12+
as published by the Free Software Foundation; either version 2
13+
of the License, or (at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program; if not, see <http://www.gnu.org/licenses/>.
22+
23+
----------------------------------------------------------------------------- */
24+
25+
26+
27+
#ifndef _FLGkernel
28+
#define _FLGkernel
29+
30+
#include "FLGinstance.hpp"
31+
#include "MLGgraph.hpp"
32+
#include "Kernel.hpp"
33+
34+
35+
36+
class FLGkernel: public Kernel<FLGinstance>{
37+
public:
38+
39+
FLGkernel(const double _gamma): gamma(_gamma){}
40+
41+
double operator()(const FLGinstance& x1, const FLGinstance& x2) const;
42+
43+
double operator()(const MLGgraph& x1, const MLGgraph& x2) const{return (*this)(x1.flg,x2.flg);};
44+
45+
public:
46+
47+
double gamma=0.1;
48+
49+
};
50+
51+
#endif

MLGkernel/MLGkernel/Kernel.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
-----------------------------------------------------------------------------
3+
4+
MLGkernel is an open source implementation of the Multiscale Laplacian Graph
5+
Kernel for computing the gram matrix of a collection of graphs.
6+
7+
Copyright (C) 2016 Imre Risi Kondor, Horace Pan
8+
9+
10+
This program is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU General Public License
12+
as published by the Free Software Foundation; either version 2
13+
of the License, or (at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program; if not, see <http://www.gnu.org/licenses/>.
22+
23+
----------------------------------------------------------------------------- */
24+
25+
26+
27+
#ifndef _Kernel
28+
#define _Kernel
29+
30+
#include "pMMFbase.hpp"
31+
32+
33+
template<class TYPE>
34+
class Kernel{
35+
public:
36+
37+
virtual double operator()(const TYPE& x1, const TYPE& x2) const =0;
38+
39+
};
40+
41+
42+
43+
44+
#endif

0 commit comments

Comments
 (0)