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
00040
#include "LocallyWeightedDistribution.h"
00041
#include <plearn/vmat/ConcatColumnsVMatrix.h>
00042
00043
namespace PLearn {
00044
using namespace std;
00045
00046 LocallyWeightedDistribution::LocallyWeightedDistribution()
00047 {}
00048
00049
00050
PLEARN_IMPLEMENT_OBJECT(
LocallyWeightedDistribution,
"ONE LINE DESCR",
"NO HELP");
00051
00052 void LocallyWeightedDistribution::declareOptions(
OptionList& ol)
00053 {
00054
declareOption(ol,
"weighting_kernel", &LocallyWeightedDistribution::weighting_kernel, OptionBase::buildoption,
00055
"The kernel that will be used to locally weigh the samples");
00056
00057
declareOption(ol,
"localdistr", &LocallyWeightedDistribution::localdistr, OptionBase::buildoption,
00058
"The distribution that will be trianed with local weights");
00059
00060
00061 inherited::declareOptions(ol);
00062 }
00063
00064 void LocallyWeightedDistribution::build_()
00065 {
00066
00067
00068
00069
00070
00071
00072
00073
00074
if(
weightsize()!=0 &&
weightsize()!=1)
00075
PLERROR(
"In LocallyWeightedDistribution::build_, weightsize must be 0 or 1");
00076
00077
localdistr->inputsize_ = inputsize_;
00078
localdistr->weightsize_ = 1;
00079
localdistr->build();
00080 }
00081
00082
00083 void LocallyWeightedDistribution::build()
00084 {
00085 inherited::build();
00086
build_();
00087 }
00088
00089
00090 void LocallyWeightedDistribution::train(
VMat training_set)
00091 {
00092
if(training_set.
width() !=
inputsize()+
weightsize())
00093
PLERROR(
"In LocallyWeightedDistribution::train width of training set is different from inputsize()+weightsize()");
00094 setTrainingSet(training_set);
00095 }
00096
00097
00098 void LocallyWeightedDistribution::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00099 {
00100 Distribution::makeDeepCopyFromShallowCopy(copies);
00101
00102
00103
00104
00105
00106
00107
00108
00109
PLERROR(
"LocallyWeightedDistribution::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00110 }
00111
00112
00113 double LocallyWeightedDistribution::log_density(
const Vec& x)
const
00114
{
00115
int l = train_set.
length();
00116
int w =
inputsize();
00117
weights.
resize(l);
00118
00119
trainsample.
resize(w+
weightsize());
00120
Vec input =
trainsample.
subVec(0,w);
00121
00122
for(
int i=0; i<l; i++)
00123 {
00124 train_set->getRow(i,
trainsample);
00125
real weight =
weighting_kernel(
x,input);
00126
if(
weightsize()==1)
00127 weight *=
trainsample[w];
00128
weights[i] = weight;
00129 }
00130
00131
VMat weight_column(
columnmatrix(
weights));
00132
00133
VMat weighted_trainset;
00134
if(
weightsize()==0)
00135 weighted_trainset =
hconcat(train_set, weight_column);
00136
else
00137 weighted_trainset =
hconcat(train_set.
subMatColumns(0,inputsize()), weight_column);
00138
00139
localdistr->forget();
00140
localdistr->train(weighted_trainset);
00141
return localdistr->log_density(
x);
00142 }
00143
00144
00145 }