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
#include "EmbeddedSequentialLearner.h"
00039
#include <plearn/vmat/TemporalHorizonVMatrix.h>
00040
#include <plearn/io/TmpFilenames.h>
00041
#include <plearn/vmat/VMat_maths.h>
00042
00043
namespace PLearn {
00044
using namespace std;
00045
00046
00047
PLEARN_IMPLEMENT_OBJECT(EmbeddedSequentialLearner,
"ONE LINE DESCR",
"NO HELP");
00048
00049 EmbeddedSequentialLearner::EmbeddedSequentialLearner()
00050 {}
00051
00052 void EmbeddedSequentialLearner::makeDeepCopyFromShallowCopy(
CopiesMap& copies)
00053 {
00054 inherited::makeDeepCopyFromShallowCopy(copies);
00055
deepCopyField(
learner, copies);
00056 }
00057
00058 void EmbeddedSequentialLearner::build_()
00059 {
00060
if (
learner.
isNull())
00061
PLERROR(
"EmbeddedSequentialLearner::build()_ - learner attribute is NULL");
00062
00063
learner->build();
00064
00065
forget();
00066 }
00067
00068 void EmbeddedSequentialLearner::build()
00069 {
00070 inherited::build();
00071
build_();
00072 }
00073
00074 void EmbeddedSequentialLearner::declareOptions(
OptionList& ol)
00075 {
00076
declareOption(ol,
"learner", &EmbeddedSequentialLearner::learner,
00077 OptionBase::buildoption,
"The underlying learner \n");
00078
00079 inherited::declareOptions(ol);
00080 }
00081
00082 void EmbeddedSequentialLearner::train()
00083 {
00084
00085
00086
int t = train_set.
length();
00087
if (t >= last_train_t+train_step)
00088 {
00089
VMat aligned_set =
new TemporalHorizonVMatrix(train_set, horizon,
targetsize());
00090
int start = (max_train_len<0) ? 0 :
max(0,aligned_set.
length()-max_train_len);
00091
int len = aligned_set.
length()-start;
00092
TmpFilenames tmpfile;
00093
00094
string index_fname = tmpfile.
addFilename();
00095
VMat aligned_set_non_missing =
filter(aligned_set.
subMatRows(start,len), index_fname);
00096
learner->setTrainingSet(aligned_set_non_missing);
00097
learner->setTrainStatsCollector(train_stats);
00098
learner->train();
00099 last_train_t = t;
00100 }
00101
00102
00103 }
00104
00105 void EmbeddedSequentialLearner::test(
VMat testset,
PP<VecStatsCollector> test_stats,
00106
VMat testoutputs,
VMat testcosts)
const
00107
{
00108
int l = testset.
length();
00109
Vec input, target;
00110
static Vec dummy_input;
00111
real weight;
00112
00113
Vec output(testoutputs ?
outputsize() :0);
00114
Vec costs(
nTestCosts());
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
int start =
MAX(last_train_t-1,last_test_t);
00127
ProgressBar* pb = NULL;
00128
if(report_progress)
00129 pb =
new ProgressBar(
"Testing learner",l-start);
00130
for (
int t=start; t<testset.
length(); t++)
00131 {
00132 testset.
getExample(t, input, target, weight);
00133
00134
00135
00136
if (!input.
hasMissing())
00137 {
00138
Vec output = predictions(t);
00139
learner->computeOutput(input, output);
00140
if (testoutputs) testoutputs->appendRow(output);
00141 }
00142
if (t>=horizon)
00143 {
00144
Vec output = predictions(t-horizon);
00145
if (!target.
hasMissing() && !output.
hasMissing())
00146 {
00147
Vec error_t = errors(t);
00148
learner->computeCostsFromOutputs(dummy_input, output, target, error_t);
00149
if (testcosts) testcosts->appendRow(error_t);
00150 test_stats->update(error_t);
00151 }
00152
00153
00154
00155
00156
if (pb)
00157 pb->
update(t-start);
00158 }
00159 }
00160 last_test_t = testset.
length();
00161
00162 test_stats->finalize();
00163
00164
if (pb)
00165
delete pb;
00166 }
00167
00168 void EmbeddedSequentialLearner::forget()
00169 {
00170
00171
learner->forget();
00172 }
00173
00174 void EmbeddedSequentialLearner::computeOutput(
const Vec& input,
Vec& output)
00175 {
learner->computeOutput(input, output); }
00176
00177 void EmbeddedSequentialLearner::computeCostsFromOutputs(
const Vec& input,
const Vec& output,
00178
const Vec& target,
Vec& costs)
00179 {
learner->computeCostsFromOutputs(input, output, target, costs); }
00180
00181 void EmbeddedSequentialLearner::computeOutputAndCosts(
const Vec& input,
const Vec& target,
00182
Vec& output,
Vec& costs)
00183 {
learner->computeOutputAndCosts(input, target, output, costs); }
00184
00185 void EmbeddedSequentialLearner::computeCostsOnly(
const Vec& input,
const Vec& target,
Vec& costs)
00186 {
learner->computeCostsOnly(input, target, costs); }
00187
00188 TVec<string> EmbeddedSequentialLearner::getTestCostNames()
const
00189
{
return learner->getTestCostNames(); }
00190
00191 TVec<string> EmbeddedSequentialLearner::getTrainCostNames()
const
00192
{
return learner->getTrainCostNames(); }
00193
00194
00195 }
00196