PLearn 0.1
|
#include <SumOverBagsVariable.h>
Public Member Functions | |
SumOverBagsVariable () | |
protected default constructor for persistence | |
SumOverBagsVariable (VMat the_vmat, Func the_f, int maxbagsize, int nsamples, bool average, bool transpose=false, bool call_build_=true) | |
![]() | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual SumOverBagsVariable * | 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 | fpropOneBag (bool do_bprop=false) |
void | printInfo (bool print_gradient) |
Static Public Member Functions | |
static string | _classname_ () |
SumOverBagsVariable. | |
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 | vmat |
Func | f |
bool | average |
int | max_bag_size |
int | n_samples |
bool | transpose |
int | curpos |
current pos in VMat | |
Vec | output_value |
Mat | input_values |
Vec | bag_size_vec |
Vec | bag_target_and_bag_signal |
Vec | bag_target |
Vec | bag_signal |
Vec | bag_weight |
Array< Vec > | f_inputs |
Array< Vec > | unused_gradients |
Array< Vec > | output_av |
Array< Vec > | gradient_av |
int | bag_size |
Static Public Attributes | |
static const int | TARGET_COLUMN_FIRST = 1 |
Tags to use in the vmat given to this variable. | |
static const int | TARGET_COLUMN_LAST = 2 |
static const int | TARGET_COLUMN_SINGLE = 3 |
static const int | TARGET_COLUMN_INTERMEDIATE = 0 |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | build_ () |
Object-specific post-constructor. | |
Private Types | |
typedef NaryVariable | inherited |
Definition at line 52 of file SumOverBagsVariable.h.
typedef NaryVariable PLearn::SumOverBagsVariable::inherited [private] |
Reimplemented from PLearn::NaryVariable.
Definition at line 54 of file SumOverBagsVariable.h.
PLearn::SumOverBagsVariable::SumOverBagsVariable | ( | ) |
protected default constructor for persistence
Definition at line 78 of file SumOverBagsVariable.cc.
: average(false), max_bag_size(-1), n_samples(1), transpose(false), curpos() {}
PLearn::SumOverBagsVariable::SumOverBagsVariable | ( | VMat | the_vmat, |
Func | the_f, | ||
int | maxbagsize, | ||
int | nsamples, | ||
bool | average, | ||
bool | transpose = false , |
||
bool | call_build_ = true |
||
) |
By convention a bag is a sequence of rows of the vmat in which the last column of the target indicates whether the row is the first one (and/or) the last one, with its two least significant bits: last_column_of_target == 1 ==> first row last_column_of_target == 2 ==> last row last_column_of_target == 0 ==> intermediate row last_column_of_target == 1+2==3 ==> single-row bag (both first and last) the last column of the target is not given in the call to f, but a bag_size input is provided instead.
The inputs to f are: (matrix of bag inputs, the bag size, the bag target, the bag weight).
Definition at line 86 of file SumOverBagsVariable.cc.
References build_().
: inherited(nonInputParentsOfPath(the_f->inputs,the_f->outputs), the_f->outputs[0]->length(), the_f->outputs[0]->width(), call_build_), vmat(the_vmat), f(the_f), average(the_average), max_bag_size(max_bagsize), n_samples(nsamples), transpose(the_transpose), curpos(0), bag_size(0) { if (call_build_) build_(); }
string PLearn::SumOverBagsVariable::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 73 of file SumOverBagsVariable.cc.
OptionList & PLearn::SumOverBagsVariable::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 73 of file SumOverBagsVariable.cc.
RemoteMethodMap & PLearn::SumOverBagsVariable::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 73 of file SumOverBagsVariable.cc.
Reimplemented from PLearn::NaryVariable.
Definition at line 73 of file SumOverBagsVariable.cc.
Object * PLearn::SumOverBagsVariable::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 73 of file SumOverBagsVariable.cc.
StaticInitializer SumOverBagsVariable::_static_initializer_ & PLearn::SumOverBagsVariable::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 73 of file SumOverBagsVariable.cc.
void PLearn::SumOverBagsVariable::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 371 of file SumOverBagsVariable.cc.
References fbprop().
{ fbprop(); }
void PLearn::SumOverBagsVariable::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 106 of file SumOverBagsVariable.cc.
References PLearn::NaryVariable::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::SumOverBagsVariable::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 115 of file SumOverBagsVariable.cc.
References bag_signal, bag_size_vec, bag_target, bag_target_and_bag_signal, bag_weight, PLearn::NaryVariable::build(), std::copy(), f, f_inputs, PLearn::Variable::gradient, gradient_av, i, input_values, max_bag_size, PLearn::nonInputParentsOfPath(), output_av, output_value, PLASSERT, PLERROR, PLearn::TVec< T >::resize(), PLearn::TMat< T >::resize(), PLearn::TVec< T >::size(), PLearn::TVec< T >::subVec(), PLearn::TMat< T >::toVec(), transpose, unused_gradients, PLearn::NaryVariable::varray, vmat, and PLearn::ws().
Referenced by build(), and SumOverBagsVariable().
{ if (vmat) { PLASSERT( f ); varray = nonInputParentsOfPath(f->inputs, f->outputs); // We need to rebuild the parent class since a build option changed. inherited::build(); if (f->outputs.size()!=1) PLERROR("SumOverBagsVariable: expected a func with a single output variable (you may use concat to form a single output Var)"); if (vmat->weightsize()!=0 && vmat->weightsize()!=1) PLERROR("SumOverBagsVariable expected vmat->weightsize to be 0 or 1"); if (transpose) { input_values.resize(vmat->inputsize(), max_bag_size); } else { input_values.resize(max_bag_size,vmat->inputsize()); } output_value.resize(f->outputs[0]->nelems()); output_av = Array<Vec>(output_value); gradient_av = Array<Vec>(gradient); f->inputs.setDontBpropHere(true); bag_size_vec.resize(1); bag_target_and_bag_signal.resize(vmat->targetsize()); bag_target.resize(vmat->targetsize() - 1); bag_signal = bag_target_and_bag_signal.subVec(vmat->targetsize()-1,1); int ws = vmat->weightsize(); bag_weight.resize(ws); if (ws > 0) { f_inputs.resize(4); f_inputs[3] = bag_weight; } else { f_inputs.resize(3); } f_inputs[0] = input_values.toVec(); f_inputs[1] = bag_size_vec; f_inputs[2] = bag_target; unused_gradients.resize(f_inputs.size()); for (int i=0;i<f_inputs.size();i++) unused_gradients[i] = f_inputs[i].copy(); } }
string PLearn::SumOverBagsVariable::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 73 of file SumOverBagsVariable.cc.
void PLearn::SumOverBagsVariable::declareOptions | ( | OptionList & | ol | ) | [static] |
Default constructor for persistence.
Reimplemented from PLearn::NaryVariable.
Definition at line 163 of file SumOverBagsVariable.cc.
References average, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::NaryVariable::declareOptions(), f, max_bag_size, n_samples, transpose, and vmat.
{ declareOption(ol, "f", &SumOverBagsVariable::f, OptionBase::buildoption, " Func that is applied on each bag, whose input is the following array of Vars:\n" " (matrix of bag inputs, the bag size, the bag target, [the bag weight]).\n"); declareOption(ol, "vmat", &SumOverBagsVariable::vmat, OptionBase::buildoption, " VMatrix that contains the data, with multiple consecutive rows forming one bag.\n" " The last column of the target indicates the beginning and end of each bag, as follows:\n" " last_column_of_target == 1 ==> first row\n" " last_column_of_target == 2 ==> last row\n" " last_column_of_target == 0 ==> intermediate row\n" " last_column_of_target == 1+2==3 ==> single-row bag (both first and last).\n"); declareOption(ol, "average", &SumOverBagsVariable::average, OptionBase::buildoption, " If set to 1, then will compute the mean of the sum, and not the sum itself."); declareOption(ol, "max_bag_size", &SumOverBagsVariable::max_bag_size, OptionBase::buildoption, " maximum number of examples in a bag (more than that in vmat will trigger a run-time error).\n"); declareOption(ol, "n_samples", &SumOverBagsVariable::n_samples, OptionBase::buildoption, " number of bags to iterate over (1 for online gradient, <=0 for batch)."); declareOption(ol, "transpose", &SumOverBagsVariable::transpose, OptionBase::buildoption, " If set to 1, then the bag inputs will be put in columns instead of rows.\n" " This can be useful if the Func f takes column vars as inputs."); inherited::declareOptions(ol); }
static const PPath& PLearn::SumOverBagsVariable::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 102 of file SumOverBagsVariable.h.
: void build_();
SumOverBagsVariable * PLearn::SumOverBagsVariable::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::NaryVariable.
Definition at line 73 of file SumOverBagsVariable.cc.
void PLearn::SumOverBagsVariable::fbprop | ( | ) | [virtual] |
do both fprop and bprop
Reimplemented from PLearn::Variable.
Definition at line 337 of file SumOverBagsVariable.cc.
References average, PLearn::TVec< T >::clear(), curpos, f, fpropOneBag(), PLearn::Variable::gradient, i, n_samples, PLERROR, and PLearn::Variable::value.
Referenced by bprop().
{ value.clear(); f->recomputeParents(); if (n_samples==1) fpropOneBag(true); else if (n_samples<=0) // one pass through the whole data set { if (average) { // We don't know in advance how many bags there are, so the gradient // can't be propagated correctly. PLERROR("In SumOverBagsVariable::fbprop - If you want to get the average, you must tell me the number of bags in n_samples > 0, because I'm too dumb to guess it."); } curpos = 0; do { fpropOneBag(true); } while (curpos>0); } else { if (average) { gradient /= n_samples; } for (int i=0;i<n_samples;i++) fpropOneBag(true); if (average) { value /= n_samples; } } }
void PLearn::SumOverBagsVariable::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 305 of file SumOverBagsVariable.cc.
References average, PLearn::TVec< T >::clear(), curpos, f, fpropOneBag(), i, n_samples, and PLearn::Variable::value.
{ value.clear(); f->recomputeParents(); if (n_samples==1) fpropOneBag(); else if (n_samples<=0) // one pass through the whole data set { curpos=0; int count_bags = 0; do { fpropOneBag(); count_bags++; } while (curpos>0); if (average) { value /= count_bags; } } else { for (int i=0;i<n_samples;i++) fpropOneBag(); if (average) { value /= n_samples; } } }
void PLearn::SumOverBagsVariable::fpropOneBag | ( | bool | do_bprop = false | ) | [virtual] |
Definition at line 231 of file SumOverBagsVariable.cc.
References bag_signal, bag_size, bag_size_vec, bag_target, bag_target_and_bag_signal, bag_weight, PLearn::TMat< T >::column(), curpos, f, f_inputs, PLearn::VMat::getExample(), gradient_av, i, input_values, PLearn::is_equal(), PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), PLearn::VMat::length(), max_bag_size, output_av, output_value, PLASSERT, PLERROR, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::TVec< T >::subVec(), transpose, unused_gradients, PLearn::Variable::value, vmat, and PLearn::TMat< T >::width().
Referenced by fbprop(), and fprop().
{ static real dummy_weight=0; bool reached_end_of_bag=false; if (transpose) { input_values.resize(input_values.length(), max_bag_size); } else { input_values.resize(max_bag_size,input_values.width()); } for (bag_size=0;!reached_end_of_bag;bag_size++) { if (bag_size>=max_bag_size) PLERROR("SumOverBagsVariable: bag size=%d > expected max. bag size(%d)", bag_size,max_bag_size); Vec input_value; if (transpose) { input_value.resize(input_values.length()); } else { input_value = input_values(bag_size); } if (vmat->weightsize()>0) { real& weight = bag_weight[0]; vmat->getExample(curpos,input_value,bag_target_and_bag_signal,weight); } else vmat->getExample(curpos, input_value, bag_target_and_bag_signal, dummy_weight); if (bag_size == 0) { // It's the first element: we copy the good target. bag_target << bag_target_and_bag_signal.subVec( 0, bag_target_and_bag_signal.length() - 1); } else { #ifdef BOUNDCHECK // Safety check: make sure the target is the same for all elements // in the bag. Vec targ = bag_target_and_bag_signal.subVec( 0, bag_target_and_bag_signal.length() - 1); PLASSERT( targ.length() == bag_target.length() ); for (int i = 0; i < targ.length(); i++) if (!is_equal(bag_target[i], targ[i])) PLERROR("In SumOverBagsVariable::fpropOneBag - A bag must " "have the same target across all elements in it"); #endif } if (transpose) { // Need to put input_value into input_values, because it uses a separate // storage. input_values.column(bag_size) << input_value; } if (bag_size==0 && !(int(bag_signal[0]) & 1)) PLERROR("SumOverBagsVariable: data synchronization error, first row of bag has wrong bag signal"); reached_end_of_bag = (int(bag_signal[0]) & 2) != 0; if(++curpos == vmat->length()) { curpos = 0; if (!reached_end_of_bag) { PLERROR("SumOverBagsVariable: last bag of VMatrix is not complete"); return; } } } bag_size_vec[0]=bag_size; if (do_bprop) f->fbprop(f_inputs,output_av,unused_gradients,gradient_av); else f->fprop(f_inputs,output_av); value += output_value; }
OptionList & PLearn::SumOverBagsVariable::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 73 of file SumOverBagsVariable.cc.
OptionMap & PLearn::SumOverBagsVariable::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 73 of file SumOverBagsVariable.cc.
RemoteMethodMap & PLearn::SumOverBagsVariable::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 73 of file SumOverBagsVariable.cc.
void PLearn::SumOverBagsVariable::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 209 of file SumOverBagsVariable.cc.
References bag_signal, bag_size_vec, bag_target, bag_target_and_bag_signal, bag_weight, PLearn::deepCopyField(), f, f_inputs, gradient_av, input_values, PLearn::NaryVariable::makeDeepCopyFromShallowCopy(), output_av, output_value, unused_gradients, and vmat.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(vmat, copies); deepCopyField(f, copies); deepCopyField(output_value, copies); deepCopyField(input_values, copies); deepCopyField(bag_size_vec, copies); deepCopyField(bag_target_and_bag_signal, copies); deepCopyField(bag_target, copies); deepCopyField(bag_signal, copies); deepCopyField(bag_weight, copies); deepCopyField(f_inputs, copies); deepCopyField(unused_gradients, copies); deepCopyField(output_av, copies); deepCopyField(gradient_av, copies); }
void PLearn::SumOverBagsVariable::printInfo | ( | bool | print_gradient | ) | [virtual] |
Reimplemented from PLearn::NaryVariable.
Definition at line 379 of file SumOverBagsVariable.cc.
References PLearn::endl(), f, PLearn::Variable::getName(), PLearn::Variable::gradient, PLearn::Object::info(), max_bag_size, n_samples, and PLearn::Variable::value.
{ f->fproppath.printInfo(print_gradient); cout << info() << " : " << getName() << "(max_bag_size=" << max_bag_size << ", "; cout << ", n_samples=" << n_samples << ") = " << value; if (print_gradient) cout << " gradient=" << gradient; cout << 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 196 of file SumOverBagsVariable.cc.
References f.
Reimplemented from PLearn::NaryVariable.
Definition at line 102 of file SumOverBagsVariable.h.
Definition at line 60 of file SumOverBagsVariable.h.
Referenced by declareOptions(), fbprop(), and fprop().
Definition at line 72 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 78 of file SumOverBagsVariable.h.
Referenced by fpropOneBag().
Definition at line 69 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 71 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 70 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 73 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
current pos in VMat
Definition at line 65 of file SumOverBagsVariable.h.
Referenced by fbprop(), fprop(), and fpropOneBag().
Definition at line 59 of file SumOverBagsVariable.h.
Referenced by build_(), declareOptions(), fbprop(), fprop(), fpropOneBag(), makeDeepCopyFromShallowCopy(), printInfo(), and recomputeSize().
Definition at line 74 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 77 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 68 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 61 of file SumOverBagsVariable.h.
Referenced by build_(), declareOptions(), fpropOneBag(), and printInfo().
Definition at line 62 of file SumOverBagsVariable.h.
Referenced by declareOptions(), fbprop(), fprop(), and printInfo().
Definition at line 76 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 67 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
const int PLearn::SumOverBagsVariable::TARGET_COLUMN_FIRST = 1 [static] |
Tags to use in the vmat given to this variable.
Definition at line 97 of file SumOverBagsVariable.h.
Referenced by PLearn::OnBagsModule::bpropUpdate(), PLearn::MultiInstanceVMatrix::build_(), PLearn::ReplicateSamplesVMatrix::build_(), PLearn::AddCostToLearner::computeCostsFromOutputs(), PLearn::NNet::computeOutputAndCosts(), PLearn::OnBagsModule::fprop(), PLearn::NNet::train(), PLearn::NeighborhoodSmoothnessNNet::train(), PLearn::MultiInstanceNNet::train(), and PLearn::ToBagClassifier::updateCostAndBagOutput().
const int PLearn::SumOverBagsVariable::TARGET_COLUMN_INTERMEDIATE = 0 [static] |
Definition at line 100 of file SumOverBagsVariable.h.
Referenced by PLearn::MultiInstanceVMatrix::build_().
const int PLearn::SumOverBagsVariable::TARGET_COLUMN_LAST = 2 [static] |
Definition at line 98 of file SumOverBagsVariable.h.
Referenced by PLearn::OnBagsModule::bpropUpdate(), PLearn::ToBagSplitter::build_(), PLearn::MultiInstanceVMatrix::build_(), PLearn::AddCostToLearner::computeCostsFromOutputs(), PLearn::NNet::computeOutputAndCosts(), PLearn::OnBagsModule::fprop(), PLearn::ModuleLearner::train(), and PLearn::ToBagClassifier::updateCostAndBagOutput().
const int PLearn::SumOverBagsVariable::TARGET_COLUMN_SINGLE = 3 [static] |
Definition at line 99 of file SumOverBagsVariable.h.
Referenced by PLearn::MultiInstanceVMatrix::build_().
Definition at line 63 of file SumOverBagsVariable.h.
Referenced by build_(), declareOptions(), and fpropOneBag().
Definition at line 75 of file SumOverBagsVariable.h.
Referenced by build_(), fpropOneBag(), and makeDeepCopyFromShallowCopy().
Definition at line 58 of file SumOverBagsVariable.h.
Referenced by build_(), declareOptions(), fpropOneBag(), and makeDeepCopyFromShallowCopy().