PLearn 0.1
|
Sums the value of a Function evaluated on each row of a VMatrix. More...
#include <SumOfVariable.h>
Public Member Functions | |
SumOfVariable () | |
Default constructor. | |
SumOfVariable (VMat the_distr, Func the_f, int the_nsamples=-1, bool the_do_resizeprop=false, bool call_build_=true) | |
Convenience constructor. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual SumOfVariable * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | recomputeSize (int &l, int &w) const |
Recomputes the length l and width w that this variable should have, according to its parent variables. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be. | |
virtual void | fprop () |
compute output given input | |
virtual void | bprop () |
virtual void | fbprop () |
do both fprop and bprop | |
virtual void | symbolicBprop () |
compute a piece of new Var graph that represents the symbolic derivative of this Var | |
virtual void | rfprop () |
VMat | getDataSet () const |
void | setDataSet (VMat dset) |
void | setCurrentSamplePos (int pos) |
int | getCurrentSamplePos () const |
void | setSampleRange (int startpos, int n, bool do_loop) |
This allows to control over which part of the dataset the next propagation operation(s) will sum. | |
void | printInfo (bool print_gradient) |
Static Public Member Functions | |
static string | _classname_ () |
SumOfVariable. | |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
static void | declareOptions (OptionList &ol) |
Default constructor for persistence. | |
Public Attributes | |
VMat | distr |
Func | f |
int | nsamples |
number of consecutive samples from the dataset distr that every propagation operation will use | |
int | curpos |
position of current sample in dataset distr | |
bool | loop |
if true, every propagation operation, before returning, will set back curpos to the value it had when entering the call. | |
Vec | input_value |
Vec | input_gradient |
Vec | output_value |
bool | do_sizeprop |
Indication that sizefprop should be used on f. | |
int | beginpos |
int | endpos |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | build_ () |
Object-specific post-constructor. | |
Private Types | |
typedef NaryVariable | inherited |
Sums the value of a Function evaluated on each row of a VMatrix.
SumOfVariable computes the sum of the value of a Func evaluated on each row of a VMat. This summation is not necessarily constrained to be over all the rows: each fprop computes the sum over 'nsample' rows of the associated VMatrix. This Variable is used within the implementation of NNet to create the optimization criterion over the training set (which corresponds here to the VMatrix we are summing over).
Definition at line 62 of file SumOfVariable.h.
typedef NaryVariable PLearn::SumOfVariable::inherited [private] |
Reimplemented from PLearn::NaryVariable.
Definition at line 64 of file SumOfVariable.h.
PLearn::SumOfVariable::SumOfVariable | ( | ) |
Default constructor.
Definition at line 71 of file SumOfVariable.cc.
: nsamples(0), curpos(0), loop(false), do_sizeprop(false) {}
PLearn::SumOfVariable::SumOfVariable | ( | VMat | the_distr, |
Func | the_f, | ||
int | the_nsamples = -1 , |
||
bool | the_do_resizeprop = false , |
||
bool | call_build_ = true |
||
) |
Convenience constructor.
Definition at line 78 of file SumOfVariable.cc.
References build_().
: inherited(nonInputParentsOfPath(the_f->inputs, the_f->outputs), the_f->outputs[0]->length(), the_f->outputs[0]->width(), call_build_), distr(the_distr), f(the_f), nsamples(the_nsamples), curpos(0), loop(false), input_value(the_distr->width()), input_gradient(the_distr->width()), output_value(the_f->outputs[0]->size()), do_sizeprop(the_do_sizeprop) { if (call_build_) build_(); }
string PLearn::SumOfVariable::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 65 of file SumOfVariable.cc.
OptionList & PLearn::SumOfVariable::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 65 of file SumOfVariable.cc.
RemoteMethodMap & PLearn::SumOfVariable::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 65 of file SumOfVariable.cc.
Reimplemented from PLearn::NaryVariable.
Definition at line 65 of file SumOfVariable.cc.
Object * PLearn::SumOfVariable::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 65 of file SumOfVariable.cc.
StaticInitializer SumOfVariable::_static_initializer_ & PLearn::SumOfVariable::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 65 of file SumOfVariable.cc.
void PLearn::SumOfVariable::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 234 of file SumOfVariable.cc.
References fbprop().
{ fbprop(); }
void PLearn::SumOfVariable::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::NaryVariable.
Definition at line 101 of file SumOfVariable.cc.
References PLearn::NaryVariable::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::SumOfVariable::build_ | ( | ) | [protected] |
Object-specific post-constructor.
This method should be redefined in subclasses and do the actual building of the object according to previously set option fields. Constructors can just set option fields, and then call build_. This method is NOT virtual, and will typically be called only from three places: a constructor, the public virtual build()
method, and possibly the public virtual read method (which calls its parent's read). build_()
can assume that its parent's build_()
has already been called.
Reimplemented from PLearn::NaryVariable.
Definition at line 110 of file SumOfVariable.cc.
References PLearn::NaryVariable::build(), distr, f, input_gradient, input_value, PLearn::VMat::length(), PLearn::nonInputParentsOfPath(), nsamples, PLERROR, PLearn::TVec< T >::resize(), and PLearn::NaryVariable::varray.
Referenced by build(), and SumOfVariable().
{ if (f && distr) { varray = nonInputParentsOfPath(f->inputs, f->outputs); // We need to rebuild the parent class since a build option changed. inherited::build(); input_value.resize(distr->inputsize() + distr->targetsize() + distr->weightsize()); input_gradient.resize(distr->inputsize() + distr->targetsize() + distr->weightsize()); if(f->outputs.size() != 1) PLERROR("In SumOfVariable::build_: function must have a single " "variable output (maybe you can vconcat the vars into a " "single one prior to calling sumOf, if this is really " "what you want)"); if(nsamples == -1) nsamples = distr->length(); f->inputs.setDontBpropHere(true); } }
string PLearn::SumOfVariable::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file SumOfVariable.cc.
void PLearn::SumOfVariable::declareOptions | ( | OptionList & | ol | ) | [static] |
Default constructor for persistence.
Reimplemented from PLearn::NaryVariable.
Definition at line 132 of file SumOfVariable.cc.
References PLearn::OptionBase::buildoption, curpos, PLearn::declareOption(), PLearn::NaryVariable::declareOptions(), distr, f, loop, and nsamples.
{ declareOption(ol, "distr", &SumOfVariable::distr, OptionBase::buildoption, "VMatrix over which the summation should be done."); declareOption(ol, "f", &SumOfVariable::f, OptionBase::buildoption, "Function that is passed the rows of the VMat as input."); declareOption(ol, "nsamples", &SumOfVariable::nsamples, OptionBase::buildoption, "How many rows of the VMatrix should be summed at a time when\n" "performing an fprop/bprop on the Variable. If -1 (the default)\n" "the length of 'distr' is assumed, i.e. the sum is done over\n" "all rows of the matrix."); declareOption(ol, "curpos", &SumOfVariable::curpos, OptionBase::buildoption, "Current position (row) in the VMatrix we are summing over."); declareOption(ol, "loop", &SumOfVariable::loop, OptionBase::buildoption, "If true, every propagation operation, before returning,\n" "will set back curpos to the value it had when entering\n" "the call. So curpos will be left unchanged by the call.\n" "This behavior corresponds to propagation operations \n" "always summing over the same nsamples (in range \n" "curpos, ..., curpos+nsamples-1) \n" "If loop is false however, any propagation call will \n" "move curpos by nsamples, thus a subsequent propagation \n" "call will sum over the *next* nsamples (which will correspond \n" "to the same saples only if nsamples == distr.length())."); inherited::declareOptions(ol); }
static const PPath& PLearn::SumOfVariable::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 100 of file SumOfVariable.h.
{ return distr; }
SumOfVariable * PLearn::SumOfVariable::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::NaryVariable.
Definition at line 65 of file SumOfVariable.cc.
void PLearn::SumOfVariable::fbprop | ( | ) | [virtual] |
do both fprop and bprop
Reimplemented from PLearn::Variable.
Definition at line 238 of file SumOfVariable.cc.
References PLearn::TVec< T >::clear(), curpos, PLearn::TVec< T >::data(), PLearn::displayFunction(), distr, do_sizeprop, f, PLearn::Variable::gradient, PLearn::Variable::gradientdata, i, input_gradient, input_value, PLearn::TVec< T >::length(), PLearn::VMat::length(), loop, nsamples, output_value, PLERROR, PLearn::PLMPI::rank, PLearn::TVec< T >::resize(), PLearn::PLMPI::size, PLearn::Variable::size(), PLearn::Variable::value, and PLearn::VMat::width().
Referenced by bprop().
{ f->recomputeParents(); int orig_curpos = curpos; if(nsamples==1) { input_value.resize(distr->width()); distr->getRow(curpos, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); //displayFunction(f, true, false, 250); if(do_sizeprop) f->sizefbprop(input_value, value, input_gradient, gradient); else f->fbprop(input_value, value, input_gradient, gradient); //displayFunction(f, true, false, 250); if(++curpos == distr->length()) curpos = 0; } else { value.clear(); #if USING_MPI if (nsamples > distr->length()) PLERROR("In SumOfVariable::fbprop, the case where nsamples is greater than distr->length is not supported in parallel computation"); int nb_sample = nsamples/PLMPI::size; int start_pos = PLMPI::rank * nb_sample; int end_pos = (PLMPI::rank==PLMPI::size-1) ? nsamples : start_pos + nb_sample; Vec dummy_value(value.length()); for(int i=start_pos; i<end_pos; i++) { input_value.resize(distr->width()); distr->getRow(i, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); if(do_sizeprop) f->sizefbprop(input_value, output_value, input_gradient, gradient); else f->fbprop(input_value, output_value, input_gradient, gradient); dummy_value += output_value; } MPI_Allreduce(dummy_value.data(), value.data(), value.length(), PLMPI_REAL, MPI_SUM, MPI_COMM_WORLD); VarArray params = f->parameters; for (int i=0; i<params->length(); i++) { Vec buffer(params[i]->size()); MPI_Reduce(params[i]->gradientdata, buffer.data(), buffer.length(), PLMPI_REAL, MPI_SUM, 0, MPI_COMM_WORLD); buffer >> params[i]->gradient; MPI_Bcast(params[i]->gradientdata, buffer.length(), PLMPI_REAL, 0, MPI_COMM_WORLD); } #else for(int i=0; i<nsamples; i++) { input_value.resize(distr->width()); distr->getRow(curpos, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); static bool display_fn=false; if (display_fn) displayFunction(f, true, false, 250); if(do_sizeprop) f->sizefbprop(input_value, output_value, input_gradient, gradient); else f->fbprop(input_value, output_value, input_gradient, gradient); value += output_value; if(++curpos == distr->length()) curpos = 0; } #endif } if(loop) curpos = orig_curpos; }
void PLearn::SumOfVariable::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 178 of file SumOfVariable.cc.
References PLearn::TVec< T >::clear(), curpos, PLearn::TVec< T >::data(), distr, do_sizeprop, f, i, input_value, PLearn::TVec< T >::length(), PLearn::VMat::length(), loop, nsamples, output_value, PLERROR, PLearn::PLMPI::rank, PLearn::TVec< T >::resize(), PLearn::Variable::size(), PLearn::PLMPI::size, PLearn::Variable::value, and PLearn::VMat::width().
{ int orig_curpos = curpos; f->recomputeParents(); if(nsamples==1) { input_value.resize(distr->width()); distr->getRow(curpos, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); if(do_sizeprop) f->sizefprop(input_value, value); else f->fprop(input_value, value); if(++curpos == distr->length()) curpos = 0; } else { value.clear(); #if USING_MPI if (nsamples > distr->length()) PLERROR("In SumOfVariable::fprop, the case where nsamples is greater than distr->length is not supported in parallel computation"); int nb_sample = nsamples/PLMPI::size; int start_pos = PLMPI::rank * nb_sample; int end_pos = (PLMPI::rank==PLMPI::size-1) ? nsamples : start_pos + nb_sample; Vec dummy_value(value.length()); for(int i=start_pos; i<end_pos; i++) { input_value.resize(distr->width()); distr->getRow(i, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); if(do_sizeprop) f->sizefprop(input_value, output_value); else f->fprop(input_value, output_value); dummy_value += output_value; } MPI_Allreduce(dummy_value.data(), value.data(), value.length(), PLMPI_REAL, MPI_SUM, MPI_COMM_WORLD); #else for(int i=0; i<nsamples; i++) { input_value.resize(distr->width()); distr->getRow(curpos, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); if(do_sizeprop) f->sizefprop(input_value, output_value); else f->fprop(input_value, output_value); value += output_value; if(++curpos == distr->length()) curpos = 0; } #endif } if(loop) curpos = orig_curpos; }
int PLearn::SumOfVariable::getCurrentSamplePos | ( | ) | const [inline] |
Definition at line 131 of file SumOfVariable.h.
{ return curpos; }
VMat PLearn::SumOfVariable::getDataSet | ( | ) | const [inline] |
Definition at line 113 of file SumOfVariable.h.
Referenced by PLearn::ConjGradientOptimizer::optimizeN().
{ return distr; }
OptionList & PLearn::SumOfVariable::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file SumOfVariable.cc.
OptionMap & PLearn::SumOfVariable::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file SumOfVariable.cc.
RemoteMethodMap & PLearn::SumOfVariable::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file SumOfVariable.cc.
void PLearn::SumOfVariable::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.
This needs to be overridden by every class that adds "complex" data members to the class, such as Vec
, Mat
, PP<Something>
, etc. Typical implementation:
void CLASS_OF_THIS::makeDeepCopyFromShallowCopy(CopiesMap& copies) { inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(complex_data_member1, copies); deepCopyField(complex_data_member2, copies); ... }
copies | A map used by the deep-copy mechanism to keep track of already-copied objects. |
Reimplemented from PLearn::NaryVariable.
Definition at line 170 of file SumOfVariable.cc.
References PLearn::deepCopyField(), distr, f, and PLearn::NaryVariable::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(distr, copies); deepCopyField(f, copies); }
void PLearn::SumOfVariable::printInfo | ( | bool | print_gradient | ) | [virtual] |
Reimplemented from PLearn::NaryVariable.
Definition at line 380 of file SumOfVariable.cc.
References PLearn::TVec< T >::clear(), curpos, distr, do_sizeprop, PLearn::endl(), f, PLearn::Variable::getName(), PLearn::Variable::gradient, i, PLearn::Object::info(), input_gradient, input_value, PLearn::VMat::length(), PLearn::Variable::nelems(), nsamples, output_value, PLearn::pout, PLearn::TVec< T >::resize(), PLearn::Variable::value, and PLearn::VMat::width().
{ Vec input_value(distr->width()); Vec input_gradient(distr->width()); Vec output_value(nelems()); f->recomputeParents(); value.clear(); for(int i=0; i<nsamples; i++) { input_value.resize(distr->width()); distr->getRow(curpos++,input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); if(do_sizeprop) f->sizefprop(input_value,output_value); if (print_gradient) f->fbprop(input_value, output_value, input_gradient, gradient); else f->fprop(input_value, output_value); value += output_value; if(curpos>=distr->length()) curpos = 0; f->fproppath.printInfo(print_gradient); } pout << info() << " : " << getName() << " = " << value; if (print_gradient) cout << " gradient=" << gradient; pout << endl; }
Recomputes the length l and width w that this variable should have, according to its parent variables.
This is used for ex. by sizeprop() The default version stupidly returns the current dimensions, so make sure to overload it in subclasses if this is not appropriate.
Reimplemented from PLearn::Variable.
Definition at line 160 of file SumOfVariable.cc.
References f.
void PLearn::SumOfVariable::rfprop | ( | ) | [virtual] |
Reimplemented from PLearn::Variable.
Definition at line 328 of file SumOfVariable.cc.
References curpos, PLearn::TVec< T >::length(), loop, PLearn::NaryVariable::resizeRValue(), and PLearn::Variable::rValue.
{ int orig_curpos = curpos; if (rValue.length()==0) resizeRValue(); // TODO... (we will need a rfprop() in Func) // f->recomputeParents(); // if(nsamples==1) // { // distr->getRow(curpos, input_value); // f->fprop(input_value, value); // if(++curpos == distr->length()) // curpos = 0; // } // else // { // value.clear(); // #if USING_MPI // if (nsamples > distr->length()) // PLERROR("In SumOfVariable::fprop, the case where nsamples is greater than distr->length is not supported in parallel computation"); // int nb_sample = nsamples/PLMPI::size; // int start_pos = PLMPI::rank * nb_sample; // int end_pos = (PLMPI::rank==PLMPI::size-1) ? nsamples : start_pos + nb_sample; // Vec dummy_value(value.length()); // for(int i=start_pos; i<end_pos; i++) // { // distr->getRow(i, input_value); // f->fprop(input_value, output_value); // dummy_value += output_value; // } // MPI_Allreduce(dummy_value.data(), value.data(), value.length(), PLMPI_REAL, MPI_SUM, MPI_COMM_WORLD); // #else // for(int i=0; i<nsamples; i++) // { // distr->getRow(curpos, input_value); // f->fprop(input_value, output_value); // value += output_value; // if(++curpos == distr->length()) // curpos = 0; // } // #endif // } if(loop) curpos = orig_curpos; }
void PLearn::SumOfVariable::setCurrentSamplePos | ( | int | pos | ) | [inline] |
Definition at line 128 of file SumOfVariable.h.
{ curpos = pos; }
void PLearn::SumOfVariable::setDataSet | ( | VMat | dset | ) | [inline] |
This allows to control over which part of the dataset the next propagation operation(s) will sum.
The call sets the curpos, nsamples and loop options. Thus the next propagation call will start at sample curpos=startpos and sum over nsamples=n consecutive samples. If loop (assigned the value do_loop) is true, then curpos will be left unchanged by propagation calls, which will thus always sum over the same nsamples samples. If loop is false however, any propagation call will move curpos by nsamples, thus a subsequent propagation call will sum over the *next* nsamples (which will correspond to the same smaples only if nsamples == distr.length())
Definition at line 145 of file SumOfVariable.h.
References n.
Referenced by PLearn::ConjGradientOptimizer::optimizeN().
void PLearn::SumOfVariable::symbolicBprop | ( | ) | [virtual] |
compute a piece of new Var graph that represents the symbolic derivative of this Var
Reimplemented from PLearn::Variable.
Definition at line 307 of file SumOfVariable.cc.
{ /* // f is a function of its inputs, what we want is a function of the parameters of f (which are in the inputs field of this SumOfVariable) VarArray& params = varray; int nparams = params.size(); f->bproppath.symbolicBprop(); VarArray dparams(nparams); for(int i=0; i<nparams; i++) dparams[i] = params[i]->g; Var dparams_concat = new ConcatElementsVariable(dparams); Var dparams_sum = new SumOfVariable(distr, Func(params,dparams_concat), nsamples); for(int i=0; i<nparams; i++) params[i]->g += dparams_sum.sub(...) */ }
Reimplemented from PLearn::NaryVariable.
Definition at line 100 of file SumOfVariable.h.
Definition at line 87 of file SumOfVariable.h.
position of current sample in dataset distr
Definition at line 74 of file SumOfVariable.h.
Referenced by declareOptions(), fbprop(), fprop(), printInfo(), and rfprop().
Definition at line 68 of file SumOfVariable.h.
Referenced by build_(), declareOptions(), fbprop(), fprop(), makeDeepCopyFromShallowCopy(), and printInfo().
Indication that sizefprop should be used on f.
Definition at line 85 of file SumOfVariable.h.
Referenced by fbprop(), fprop(), and printInfo().
Definition at line 88 of file SumOfVariable.h.
Definition at line 69 of file SumOfVariable.h.
Referenced by build_(), declareOptions(), fbprop(), fprop(), makeDeepCopyFromShallowCopy(), printInfo(), and recomputeSize().
Definition at line 82 of file SumOfVariable.h.
Referenced by build_(), fbprop(), and printInfo().
Definition at line 81 of file SumOfVariable.h.
Referenced by build_(), fbprop(), fprop(), and printInfo().
if true, every propagation operation, before returning, will set back curpos to the value it had when entering the call.
So that curpos will be unchanged by the call.
Definition at line 76 of file SumOfVariable.h.
Referenced by declareOptions(), fbprop(), fprop(), and rfprop().
number of consecutive samples from the dataset distr that every propagation operation will use
Definition at line 71 of file SumOfVariable.h.
Referenced by build_(), PLearn::AdaptGradientOptimizer::build_(), declareOptions(), fbprop(), fprop(), PLearn::GradientOptimizer::optimizeN(), and printInfo().
Definition at line 83 of file SumOfVariable.h.
Referenced by fbprop(), fprop(), and printInfo().