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 "SequentialLearner.h"
00039
00040
namespace PLearn {
00041
using namespace std;
00042
00043
00044
PLEARN_IMPLEMENT_ABSTRACT_OBJECT(SequentialLearner,
"ONE LINE DESCR",
"NO HELP");
00045
00046 SequentialLearner::SequentialLearner()
00047 : last_train_t(-1), last_call_train_t(-1), last_test_t(-1),
00048 init_train_size(1), max_seq_len(-1), max_train_len(-1), train_step(1), horizon(1),
00049 outputsize_(1)
00050 {}
00051
00052 void SequentialLearner::makeDeepCopyFromShallowCopy(
CopiesMap& copies)
00053 {
00054 inherited::makeDeepCopyFromShallowCopy(copies);
00055
deepCopyField(
predictions, copies);
00056
deepCopyField(
errors, copies);
00057 }
00058
00059 void SequentialLearner::build_()
00060 {
00061
if(
max_seq_len != -1)
00062 {
00063
if(
outputsize() == 0 )
00064
PLERROR(
"SequentialLearner::build_\n"
00065
"outputsize() returns 0 but predictions will later be assumed to have nonzero width.");
00066
predictions.
resize(
max_seq_len,
outputsize());
00067
predictions.
fill(
MISSING_VALUE);
00068
00069
int n_test_costs =
nTestCosts();
00070
if( n_test_costs > 0 )
00071 {
00072
errors.
resize(
max_seq_len, n_test_costs);
00073
errors.
fill(
MISSING_VALUE);
00074 }
00075 }
00076 }
00077
00078 void SequentialLearner::build()
00079 {
00080 inherited::build();
00081
build_();
00082 }
00083
00084 void SequentialLearner::declareOptions(
OptionList& ol)
00085 {
00086
declareOption(ol,
"init_train_size", &SequentialLearner::init_train_size, OptionBase::buildoption,
00087
"Before the length of train_set reaches init_train_size, train doesn't do anything.\n"
00088
"Default = 1.");
00089
00090
declareOption(ol,
"max_seq_len", &SequentialLearner::max_seq_len,
00091 OptionBase::buildoption,
00092
"Maximum length that the training matrix will ever reach;\n"
00093
"this is used as an optimization to preallocate buffers\n"
00094
"and avoid reallocations as training/testing proceeds.");
00095
00096
declareOption(ol,
"max_train_len", &SequentialLearner::max_train_len,
00097 OptionBase::buildoption,
00098
"Maximum number of (input,target) pairs used for training;\n"
00099
"for longer training sequences, only the last max_train_len\n"
00100
"pairs are actually used for training");
00101
00102
declareOption(ol,
"train_step", &SequentialLearner::train_step,
00103 OptionBase::buildoption,
00104
"How often we have to re-train a model;\n"
00105
"value of 1 = after every time step");
00106
00107
declareOption(ol,
"horizon", &SequentialLearner::horizon,
00108 OptionBase::buildoption,
00109
"How much to offset the target columns with respect to\n"
00110
"the input columns");
00111
00112
declareOption(ol,
"outputsize", &SequentialLearner::outputsize_,
00113 OptionBase::buildoption,
00114
"Size of the output vector (number of outputs)");
00115
00116 inherited::declareOptions(ol);
00117 }
00118
00119 void SequentialLearner::setTrainingSet(
VMat training_set,
bool call_forget)
00120 {
00121 train_set = training_set;
00122
if (call_forget)
forget();
00123 }
00124
00125 void SequentialLearner::forget()
00126 {
00127
if (
predictions.
isNotEmpty())
00128
predictions.
fill(
MISSING_VALUE);
00129
if (
errors.
isNotEmpty())
00130
errors.
fill(
MISSING_VALUE);
00131
last_train_t = -1;
00132
last_call_train_t = -1;
00133
last_test_t = -1;
00134 }
00135
00137 int SequentialLearner::outputsize()
const
00138
{
return outputsize_; }
00139
00140 int SequentialLearner::nTestCosts()
const
00141
{
return getTestCostNames().
size(); }
00142
00143 void SequentialLearner::computeOutputAndCosts(
const Vec& input,
00144
const Vec& target,
Vec& output,
Vec& costs)
const
00145
{
PLERROR(
"The method computeOutputAndCosts is not defined for this SequentialLearner"); }
00146
00147 void SequentialLearner::computeCostsOnly(
const Vec& input,
const Vec& target,
00148
Vec& costs)
const
00149
{
PLERROR(
"The method computeCostsOnly is not defined for this SequentialLearner"); }
00150
00151 void SequentialLearner::computeOutput(
const Vec& input,
Vec& output)
const
00152
{
PLERROR(
"The method computeOutput is not defined for this SequentialLearner"); }
00153
00154 void SequentialLearner::computeCostsFromOutputs(
const Vec& input,
00155
const Vec& output,
const Vec& target,
Vec& costs)
const
00156
{
PLERROR(
"The method computeCostsFromOutputs is not defined for this SequentialLearner"); }
00157
00158 void SequentialLearner::matlabSave(
const string& matlab_subdir)
00159 {
00160 inherited::matlabSave(matlab_subdir);
00161
00162
string save_dir =
append_slash(
getExperimentDirectory()) + matlab_subdir;
00163
Vec dummy,
add(1);
add[0] = 0;
00164
00165
TVec<string> cost_names =
getTestCostNames();
00166
Vec startX(1,
real(
sequenceStart()));
00167
for(
int g=0; g < cost_names.
length(); g++)
00168
PLearn::matlabSave(save_dir, cost_names[g],
00169 startX,
00170
getCostSequence(g),
add, dummy);
00171
00172
PLearn::matlabSave(save_dir,
"Predictions",
predictions, dummy, dummy);
00173 }
00174
00175 }
00176