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
00041
#include "LearnerProcessedVMatrix.h"
00042
00043
namespace PLearn {
00044
using namespace std;
00045
00046
00047
PLEARN_IMPLEMENT_OBJECT(LearnerProcessedVMatrix,
"ONE LINE DESCR",
00048
"LearnerProcessedVMatrix implements a VMatrix processed on the fly by a learner (which will optionally be first trained on the source vmatrix)");
00049
00050
00051 LearnerProcessedVMatrix::LearnerProcessedVMatrix()
00052 :train_learner('-')
00053 {
00054
build_();
00055 }
00056
00057 void LearnerProcessedVMatrix::getNewRow(
int i,
const Vec& v)
const
00058
{
00059
static Vec sv;
00060 sv.
resize(
source.
width());
00061
source->getRow(i,sv);
00062
int nin =
source->inputsize();
00063
int nout =
learner->outputsize();
00064
Vec input = sv.
subVec(0,nin);
00065
Vec output = v.
subVec(0,nout);
00066
learner->computeOutput(input,output);
00067
int rest =
source.
width()-nin;
00068 v.
subVec(nout,rest) << sv.
subVec(nin,rest);
00069 }
00070
00071 void LearnerProcessedVMatrix::declareOptions(
OptionList& ol)
00072 {
00073
declareOption(ol,
"source", &LearnerProcessedVMatrix::source, OptionBase::buildoption,
00074
"The source vmatrix whose inputs wil be porcessed by the learner.\n"
00075
"If present, the target and weight columns will be appended to the processed input in the resulting matrix.");
00076
00077
declareOption(ol,
"learner", &LearnerProcessedVMatrix::learner, OptionBase::buildoption,
00078
"The learner used to process the VMat's input.");
00079
00080
declareOption(ol,
"train_learner", &LearnerProcessedVMatrix::train_learner, OptionBase::buildoption,
00081
"Indicates if the learner should be trained on the source, upon building, and if so on what part.\n"
00082
" '-': don't train \n"
00083
" 'S': supervised training using input and target (possibly weighted if weight is present) \n"
00084
" 'U': unsupervised training using only input part (possibly weighted if weight is present). \n");
00085
00086
00087 inherited::declareOptions(ol);
00088 }
00089
00090 void LearnerProcessedVMatrix::build_()
00091 {
00092
if (
source &&
learner) {
00093
switch(
train_learner) {
00094
case '-':
00095
break;
00096
case 'S':
00097
learner->setTrainingSet(
source);
00098
learner->setTrainStatsCollector(
new VecStatsCollector());
00099
learner->train();
00100
break;
00101
case 'U':
00102 {
00103
VMat inputs =
source.
subMatColumns(0,
source->inputsize());
00104
learner->setTrainingSet(inputs);
00105
learner->setTrainStatsCollector(
new VecStatsCollector());
00106
learner->train();
00107 }
00108 }
00109 length_ =
source->
length();
00110 width_ =
learner->outputsize() +
source->targetsize() +
source->weightsize();
00111 }
00112 }
00113
00114
00115 void LearnerProcessedVMatrix::build()
00116 {
00117 inherited::build();
00118
build_();
00119 }
00120
00121 void LearnerProcessedVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00122 {
00123 inherited::makeDeepCopyFromShallowCopy(copies);
00124
deepCopyField(
source, copies);
00125
deepCopyField(
learner, copies);
00126 }
00127
00128 }
00129