PLearn 0.1
|
#include <CCCostVariable.h>
Public Member Functions | |
CCCostVariable () | |
protected default constructor for persistence | |
CCCostVariable (VMat the_distr, Func the_f, Func f_candidate) | |
constructor | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual CCCostVariable * | 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 () |
void | printInfo (bool print_gradient) |
Static Public Member Functions | |
static string | _classname_ () |
CCCostVariable. | |
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_error |
Func | f_candidate |
Vec | input_value |
Vec | input_gradient |
Vec | error_output_value |
Vec | candidate_output_value |
Vec | error_correlations |
Vec | adjusted_gradient |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | build_ () |
Object-specific post-constructor. | |
Private Types | |
typedef NaryVariable | inherited |
Private Attributes | |
Vec | mean_error |
real | mean_candidate |
Definition at line 52 of file CCCostVariable.h.
typedef NaryVariable PLearn::CCCostVariable::inherited [private] |
Reimplemented from PLearn::NaryVariable.
Definition at line 54 of file CCCostVariable.h.
PLearn::CCCostVariable::CCCostVariable | ( | ) | [inline] |
protected default constructor for persistence
Definition at line 74 of file CCCostVariable.h.
: distr(), f_error(), f_candidate() {}
constructor
Definition at line 58 of file CCCostVariable.cc.
References build_().
: inherited(nonInputParentsOfPath(the_f_candidate->inputs,the_f_candidate->outputs), 1, 1), distr(the_distr), f_error(the_f_error), f_candidate(the_f_candidate), input_value(the_distr->width()), input_gradient(distr->inputsize()), error_output_value(the_f_error->outputs[0]->size()), candidate_output_value(the_f_candidate->outputs[0]->size()), error_correlations(the_f_error->outputs[0]->size()), adjusted_gradient(1) { build_(); }
string PLearn::CCCostVariable::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 56 of file CCCostVariable.cc.
OptionList & PLearn::CCCostVariable::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 56 of file CCCostVariable.cc.
RemoteMethodMap & PLearn::CCCostVariable::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 56 of file CCCostVariable.cc.
Reimplemented from PLearn::NaryVariable.
Definition at line 56 of file CCCostVariable.cc.
Object * PLearn::CCCostVariable::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 56 of file CCCostVariable.cc.
StaticInitializer CCCostVariable::_static_initializer_ & PLearn::CCCostVariable::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 56 of file CCCostVariable.cc.
void PLearn::CCCostVariable::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 169 of file CCCostVariable.cc.
References fbprop().
{ fbprop(); }
void PLearn::CCCostVariable::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 74 of file CCCostVariable.cc.
References PLearn::NaryVariable::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::CCCostVariable::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 81 of file CCCostVariable.cc.
References distr, f_candidate, f_error, input_gradient, input_value, mean_error, PLERROR, and PLearn::TVec< T >::resize().
Referenced by build(), and CCCostVariable().
{ if (f_error && f_candidate && distr) { mean_error.resize(f_error->outputs[0]->size()); input_value.resize(distr->inputsize() + distr->targetsize() + distr->weightsize()); input_gradient.resize(distr->inputsize()); if(f_error->outputs.size() != 1) PLERROR("In CCCostVariable: error 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(f_error->outputs[0].width() != 1) PLERROR("In CCCostVariable: the error function's output must be a column vector "); f_error->inputs.setDontBpropHere(true); if(f_candidate->outputs.size() != 1) PLERROR("In CCCostVariable: candidate node 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(f_candidate->outputs[0].width() != 1 || f_candidate->outputs[0].length() != 1) PLERROR("In CCCostVariable: the candidate node function's output must be a column vector "); } }
string PLearn::CCCostVariable::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file CCCostVariable.cc.
void PLearn::CCCostVariable::declareOptions | ( | OptionList & | ol | ) | [static] |
Default constructor for persistence.
Reimplemented from PLearn::NaryVariable.
Definition at line 103 of file CCCostVariable.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::NaryVariable::declareOptions(), distr, f_candidate, and f_error.
{ declareOption(ol, "distr", &CCCostVariable::distr, OptionBase::buildoption, ""); declareOption(ol, "f_error", &CCCostVariable::f_error, OptionBase::buildoption, ""); declareOption(ol, "f_candidate", &CCCostVariable::f_candidate, OptionBase::buildoption, ""); inherited::declareOptions(ol); }
static const PPath& PLearn::CCCostVariable::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 78 of file CCCostVariable.h.
: void build_();
CCCostVariable * PLearn::CCCostVariable::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::NaryVariable.
Definition at line 56 of file CCCostVariable.cc.
void PLearn::CCCostVariable::fbprop | ( | ) | [virtual] |
do both fprop and bprop
Reimplemented from PLearn::Variable.
Definition at line 173 of file CCCostVariable.cc.
References adjusted_gradient, candidate_output_value, distr, error_correlations, error_output_value, f_candidate, f_error, fprop(), PLearn::Variable::gradient, i, input_gradient, input_value, j, PLearn::TVec< T >::length(), PLearn::VMat::length(), mean_error, PLearn::TVec< T >::resize(), PLearn::TVec< T >::subVec(), and PLearn::VMat::width().
Referenced by bprop().
{ fprop(); for(int i=0; i<distr->length(); i++) { input_value.resize(distr->width()); distr->getRow(i, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); f_error->fprop(input_value, error_output_value); for(int j=0; j<error_correlations.length(); j++) { adjusted_gradient[0] = -1*gradient[0]*(error_output_value[j]-mean_error[j]) * (error_correlations[j] > 0 ? 1 : -1)/distr->length(); f_candidate->fbprop(input_value.subVec(0,distr->inputsize()),candidate_output_value,input_gradient, adjusted_gradient); // could be more efficient (do just a bprop: not implemented) } } }
void PLearn::CCCostVariable::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 129 of file CCCostVariable.cc.
References PLearn::abs(), candidate_output_value, PLearn::TVec< T >::clear(), distr, error_correlations, error_output_value, f_candidate, f_error, i, input_value, j, PLearn::TVec< T >::length(), PLearn::VMat::length(), mean_candidate, mean_error, PLearn::TVec< T >::resize(), PLearn::TVec< T >::subVec(), PLearn::Variable::value, and PLearn::VMat::width().
Referenced by fbprop(), and printInfo().
{ f_error->recomputeParents(); f_candidate->recomputeParents(); mean_error.clear(); mean_candidate=0; // Compute the means of the candidate node and of the error for every output for(int i=0; i<distr->length(); i++) { input_value.resize(distr->width()); distr->getRow(i, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); f_error->fprop(input_value, error_output_value); mean_error += error_output_value; f_candidate->fprop(input_value.subVec(0,distr->inputsize()),candidate_output_value); mean_candidate += candidate_output_value[0]; } mean_error /= distr->length(); mean_candidate /= distr->length(); value.clear(); error_correlations.clear(); for(int i=0; i<distr->length(); i++) { input_value.resize(distr->width()); distr->getRow(i, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); f_error->fprop(input_value, error_output_value); f_candidate->fprop(input_value.subVec(0,distr->inputsize()),candidate_output_value); for(int j=0; j<error_correlations.length(); j++) error_correlations[j] += (candidate_output_value[0]-mean_candidate)*(error_output_value[j]-mean_error[j]); } for(int j=0; j<error_correlations.length(); j++) value[0] -= abs(error_correlations[j]); value[0] /= distr->length(); }
OptionList & PLearn::CCCostVariable::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file CCCostVariable.cc.
OptionMap & PLearn::CCCostVariable::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file CCCostVariable.cc.
RemoteMethodMap & PLearn::CCCostVariable::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file CCCostVariable.cc.
void PLearn::CCCostVariable::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 120 of file CCCostVariable.cc.
References PLearn::deepCopyField(), distr, f_candidate, f_error, and PLearn::NaryVariable::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(distr, copies); deepCopyField(f_error, copies); deepCopyField(f_candidate, copies); }
void PLearn::CCCostVariable::printInfo | ( | bool | print_gradient | ) | [virtual] |
Reimplemented from PLearn::NaryVariable.
Definition at line 206 of file CCCostVariable.cc.
References adjusted_gradient, candidate_output_value, distr, PLearn::endl(), error_correlations, error_output_value, f_candidate, f_error, fprop(), PLearn::Variable::getName(), PLearn::Variable::gradient, i, PLearn::Object::info(), input_gradient, input_value, j, PLearn::TVec< T >::length(), PLearn::VMat::length(), mean_error, PLearn::TVec< T >::resize(), PLearn::TVec< T >::subVec(), PLearn::Variable::value, and PLearn::VMat::width().
{ fprop(); for(int i=0; i<distr->length(); i++) { input_value.resize(distr->width()); distr->getRow(i, input_value); input_value.resize(distr->inputsize()+distr->targetsize()+distr->weightsize()); f_error->fprop(input_value, error_output_value); for(int j=0; j<error_correlations.length(); j++) { adjusted_gradient[0] = -1*gradient[0]*(error_output_value[j]-mean_error[j]) * (error_correlations[j] > 0 ? 1 : -1); f_candidate->fbprop(input_value.subVec(0,distr->inputsize()),candidate_output_value,input_gradient, adjusted_gradient); // could be more efficient } f_candidate->fproppath.printInfo(print_gradient); } cout << info() << " : " << getName() << " = " << 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 113 of file CCCostVariable.cc.
{ w = 1; l = 1; }
void PLearn::CCCostVariable::rfprop | ( | ) | [virtual] |
Reimplemented from PLearn::Variable.
Definition at line 200 of file CCCostVariable.cc.
References PLERROR.
{ PLERROR("In CCCostVariable::rfprop() : Not implemented"); }
void PLearn::CCCostVariable::symbolicBprop | ( | ) | [virtual] |
compute a piece of new Var graph that represents the symbolic derivative of this Var
Reimplemented from PLearn::Variable.
Definition at line 195 of file CCCostVariable.cc.
References PLERROR.
{ PLERROR("In CCCostVariable::symbolicBprop() : Not implemented"); }
Reimplemented from PLearn::NaryVariable.
Definition at line 78 of file CCCostVariable.h.
Definition at line 67 of file CCCostVariable.h.
Referenced by fbprop(), and printInfo().
Definition at line 65 of file CCCostVariable.h.
Referenced by fbprop(), fprop(), and printInfo().
Definition at line 58 of file CCCostVariable.h.
Referenced by build_(), declareOptions(), fbprop(), fprop(), makeDeepCopyFromShallowCopy(), and printInfo().
Definition at line 66 of file CCCostVariable.h.
Referenced by fbprop(), fprop(), and printInfo().
Definition at line 64 of file CCCostVariable.h.
Referenced by fbprop(), fprop(), and printInfo().
Definition at line 60 of file CCCostVariable.h.
Referenced by build_(), declareOptions(), fbprop(), fprop(), makeDeepCopyFromShallowCopy(), and printInfo().
Definition at line 59 of file CCCostVariable.h.
Referenced by build_(), declareOptions(), fbprop(), fprop(), makeDeepCopyFromShallowCopy(), and printInfo().
Definition at line 63 of file CCCostVariable.h.
Referenced by build_(), fbprop(), and printInfo().
Definition at line 62 of file CCCostVariable.h.
Referenced by build_(), fbprop(), fprop(), and printInfo().
real PLearn::CCCostVariable::mean_candidate [private] |
Definition at line 70 of file CCCostVariable.h.
Referenced by fprop().
Vec PLearn::CCCostVariable::mean_error [private] |
Definition at line 69 of file CCCostVariable.h.
Referenced by build_(), fbprop(), fprop(), and printInfo().