00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
#ifndef Kernel_INC
00044
#define Kernel_INC
00045
00046
#include <plearn/base/Object.h>
00047
#include <plearn/vmat/VMat.h>
00048
00049
00050
namespace PLearn {
00051
using namespace std;
00052
00053 class Kernel:
public Object
00054 {
00055
00056
private:
00057
00058 typedef Object inherited;
00059
00061 mutable Vec evaluate_xi,
evaluate_xj,
k_xi_x;
00062
00064 mutable bool lock_xi,
lock_xj,
lock_k_xi_x;
00065
00066
protected:
00067
00068 VMat data;
00069 int data_inputsize;
00070 int n_examples;
00071
00072
static void declareOptions(
OptionList& ol);
00073
00074
public:
00075
00077 bool is_symmetric;
00078 int report_progress;
00079 VMat specify_dataset;
00080
00082
Kernel(
bool is__symmetric =
true);
00083
00084
PLEARN_DECLARE_ABSTRACT_OBJECT(
Kernel);
00085
00087
virtual real evaluate(
const Vec& x1,
const Vec& x2)
const = 0;
00088
00090
00096
virtual void setDataForKernelMatrix(
VMat the_data);
00097
00105
virtual void addDataForKernelMatrix(
const Vec& newRow);
00106
00107
00109 virtual int dataInputsize() {
00110
return data_inputsize;
00111 }
00112
00114 virtual int nExamples()
00115 {
return n_examples; }
00116
00118
virtual real evaluate_i_j(
int i,
int j)
const;
00119
00125
virtual real evaluate_i_x(
int i,
const Vec& x,
real squared_norm_of_x=-1)
const;
00126
00129
virtual real evaluate_x_i(
const Vec& x,
int i,
real squared_norm_of_x=-1)
const;
00130
00134
virtual real evaluate_i_x_again(
int i,
const Vec& x,
real squared_norm_of_x=-1,
bool first_time =
false)
const;
00135
virtual real evaluate_x_i_again(
const Vec& x,
int i,
real squared_norm_of_x=-1,
bool first_time =
false)
const;
00136
00138
virtual void computeGramMatrix(
Mat K)
const;
00139
00142
virtual void setParameters(
Vec paramvec);
00143
virtual Vec getParameters() const;
00144
00146
00147
void apply(
VMat m1,
VMat m2,
Mat& result) const;
00148
Mat apply(
VMat m1,
VMat m2) const;
00149
void apply(
VMat m, const
Vec& x,
Vec& result) const;
00150
void apply(
Vec x,
VMat m,
Vec& result) const;
00151
00153
void evaluate_all_i_x(const
Vec& x,
Vec& k_xi_x,
real squared_norm_of_x=-1,
int istart = 0) const;
00154
00156
void evaluate_all_x_i(const
Vec& x,
Vec& k_x_xi,
real squared_norm_of_x=-1,
int istart = 0) const;
00157
00158 inline
real operator()(const
Vec& x1, const
Vec& x2)
const
00159
{
return evaluate(x1,x2); }
00160
00162
bool hasData();
00163
00165 inline VMat getData() {
return this->
data;}
00166
00170
bool isInData(
const Vec& x,
int* i = 0)
const;
00171
00176
void computeNearestNeighbors(
const Vec& x,
Mat& k_xi_x_sorted,
int knn)
const;
00177
00181
static TMat<int> computeKNNeighbourMatrixFromDistanceMatrix(
const Mat& D,
int knn,
bool insure_self_first_neighbour=
true,
bool report_progress =
false);
00182
00186
static Mat computeNeighbourMatrixFromDistanceMatrix(
const Mat& D,
bool insure_self_first_neighbour=
true,
bool report_progress =
false);
00187
00188
Mat estimateHistograms(
VMat d,
real sameness_threshold,
real minval,
real maxval,
int nbins)
const;
00189
Mat estimateHistograms(
Mat input_and_class,
real minval,
real maxval,
int nbins)
const;
00190
real test(
VMat d,
real threshold,
real sameness_below_threshold,
real sameness_above_threshold)
const;
00191
virtual void build();
00192
00193
00194
virtual ~Kernel();
00195
00196
virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00197
00198
private:
00199
00200
void build_();
00201
00202 };
00203
DECLARE_OBJECT_PTR(Kernel);
00204
00209 class Ker:
public PP<Kernel>
00210 {
00211
public:
00212 Ker() {}
00213 Ker(
Kernel* v) :
PP<
Kernel>(v) {}
00214 Ker(
const Ker& other) :
PP<
Kernel>(other) {}
00215
00216 real operator()(
const Vec& x1,
const Vec& x2)
const
00217
{
return ptr->
evaluate(x1,x2); }
00218 };
00219
00220
DECLARE_OBJECT_PP(Ker, Kernel);
00221
00222
template <>
00223
inline
00224 void deepCopyField(Ker& field, CopiesMap& copies)
00225 {
00226
if (field)
00227 field = static_cast<Kernel*>(field->deepCopy(copies));
00228 }
00229
00230
00231
00232 Mat
findClosestPairsOfDifferentClass(
int k, VMat data, Ker dist);
00233
00236
00237 inline Array<Ker> operator&(
const Ker& k1,
const Ker& k2)
00238 {
return Array<Ker>(k1,k2); }
00239
00240
00241
00242
00243
00244
00246 typedef Ker CostFunc;
00247
00248
00249
00250
00251
00252
00254 typedef CostFunc ProfitFunc;
00255
00256
00257 }
00258
00259
#endif
00260