PLearn 0.1
|
Squared difference (and derivatives thereof) between the target and input. More...
#include <SquaredErrModule.h>
Public Member Functions | |
SquaredErrModule () | |
Default constructor. | |
virtual void | fprop (const Vec &input, Vec &output) const |
given the input, compute the output (possibly resize it appropriately) SOON TO BE DEPRECATED, USE fprop(const TVec<Mat*>& ports_value) | |
virtual void | bpropUpdate (const Vec &input, const Vec &output, const Vec &output_gradient) |
SOON TO BE DEPRECATED, USE bpropAccUpdate(const TVec<Mat*>& ports_value, const TVec<Mat*>& ports_gradient) Adapt based on the output gradient: this method should only be called just after a corresponding fprop; it should be called with the same arguments as fprop for the first two arguments (and output should not have been modified since then). | |
virtual void | bpropUpdate (const Vec &input, const Vec &output, Vec &input_gradient, const Vec &output_gradient) |
virtual void | bbpropUpdate (const Vec &input, const Vec &output, const Vec &output_gradient, const Vec &output_diag_hessian) |
Similar to bpropUpdate, but adapt based also on the estimation of the diagonal of the Hessian matrix, and propagates this back. | |
virtual void | bbpropUpdate (const Vec &input, const Vec &output, Vec &input_gradient, const Vec &output_gradient, Vec &input_diag_hessian, const Vec &output_diag_hessian) |
virtual void | forget () |
reset the parameters to the state they would be BEFORE starting training. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual SquaredErrModule * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static string | _classname_ () |
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 Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Protected Attributes | |
int | target_size |
Private Types | |
typedef OnlineLearningModule | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Squared difference (and derivatives thereof) between the target and input.
Definition at line 58 of file SquaredErrModule.h.
typedef OnlineLearningModule PLearn::SquaredErrModule::inherited [private] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 60 of file SquaredErrModule.h.
PLearn::SquaredErrModule::SquaredErrModule | ( | ) |
Default constructor.
Definition at line 55 of file SquaredErrModule.cc.
References PLearn::OnlineLearningModule::output_size.
: /* ### Initialize all fields to their default value */ target_size( 0 ) { output_size = 1; }
string PLearn::SquaredErrModule::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 53 of file SquaredErrModule.cc.
OptionList & PLearn::SquaredErrModule::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 53 of file SquaredErrModule.cc.
RemoteMethodMap & PLearn::SquaredErrModule::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 53 of file SquaredErrModule.cc.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 53 of file SquaredErrModule.cc.
Object * PLearn::SquaredErrModule::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 53 of file SquaredErrModule.cc.
StaticInitializer SquaredErrModule::_static_initializer_ & PLearn::SquaredErrModule::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 53 of file SquaredErrModule.cc.
void PLearn::SquaredErrModule::bbpropUpdate | ( | const Vec & | input, |
const Vec & | output, | ||
Vec & | input_gradient, | ||
const Vec & | output_gradient, | ||
Vec & | input_diag_hessian, | ||
const Vec & | output_diag_hessian | ||
) | [virtual] |
Definition at line 180 of file SquaredErrModule.cc.
References bpropUpdate(), PLearn::TVec< T >::clear(), PLearn::OnlineLearningModule::estimate_simpler_diag_hessian, i, PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLERROR, PLWARNING, PLearn::TVec< T >::resize(), PLearn::TVec< T >::size(), PLearn::TVec< T >::subVec(), and target_size.
{ int odh_size = output_diag_hessian.size(); bool is_final_cost = false; // if yes, output_diag_hessian is 0 // size check // others size checks will be done in bpropUpdate() if( odh_size == 0 ) { PLWARNING("SquaredErrModule::bbpropUpdate: you are not providing" " output_diag_hessian.\n" "Assuming this is the final cost," " and output_diag_hessian=0.\n"); is_final_cost = true; } else if( odh_size != output_size ) { PLERROR("SquaredErrModule::bbpropUpdate:" " 'output_diag_hessian.size()'\n" " should be equal to 'output_size' (%i != %i)\n", odh_size, output_size); } bpropUpdate( input, output, input_gradient, output_gradient ); Vec input_ = input.subVec( 0, input_size ); Vec target = input.subVec( input_size, target_size ); input_diag_hessian.resize( input_size ); // computation of term dC/dy d²y/dx², // skipped if estimate_simpler_diag_hessian, unless it is final cost if( estimate_simpler_diag_hessian && !is_final_cost ) { input_diag_hessian.clear(); } else { Vec idh( input_size, 2 ); input_diag_hessian << idh; if( !is_final_cost ) input_diag_hessian *= output_gradient[0]; } // computation of term d²C/dy² (dy/dx)², // skipped if it is final cost, because then d²C/dy² == d²C/dC² == 0 if( !is_final_cost ) { for( int i=0 ; i<input_size ; i++ ) { real fprime_i = 2*(input_[i] - target[i]); input_diag_hessian[i] += (fprime_i*fprime_i) * output_diag_hessian[0]; } } }
void PLearn::SquaredErrModule::bbpropUpdate | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | output_gradient, | ||
const Vec & | output_diag_hessian | ||
) | [virtual] |
Similar to bpropUpdate, but adapt based also on the estimation of the diagonal of the Hessian matrix, and propagates this back.
If these methods are defined, you can use them INSTEAD of bpropUpdate(...) THE DEFAULT IMPLEMENTATION PROVIDED HERE JUST CALLS bbpropUpdate(input, output, input_gradient, output_gradient, in_hess, out_hess) AND IGNORES INPUT HESSIAN AND INPUT GRADIENT
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 163 of file SquaredErrModule.cc.
References bpropUpdate(), PLearn::OnlineLearningModule::output_size, PLWARNING, and PLearn::TVec< T >::size().
{ int odh_size = output_diag_hessian.size(); if( odh_size != output_size ) { PLWARNING("SquaredErrModule::bbpropUpdate:" " 'output_diag_hessian.size()'\n" " should be equal to 'output_size' (%i != %i)\n", odh_size, output_size); } bpropUpdate( input, output, output_gradient ); }
void PLearn::SquaredErrModule::bpropUpdate | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | output_gradient | ||
) | [virtual] |
SOON TO BE DEPRECATED, USE bpropAccUpdate(const TVec<Mat*>& ports_value, const TVec<Mat*>& ports_gradient) Adapt based on the output gradient: this method should only be called just after a corresponding fprop; it should be called with the same arguments as fprop for the first two arguments (and output should not have been modified since then).
Since sub-classes are supposed to learn ONLINE, the object is 'ready-to-be-used' just after any bpropUpdate. N.B. The DEFAULT IMPLEMENTATION JUST CALLS bpropUpdate(input, output, input_gradient, output_gradient) AND IGNORES INPUT GRADIENT.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 82 of file SquaredErrModule.cc.
References PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLWARNING, PLearn::TVec< T >::size(), and target_size.
Referenced by bbpropUpdate().
{ int in_size = input.size(); int out_size = output.size(); int og_size = output_gradient.size(); // size check if( in_size != input_size + target_size ) { PLWARNING("SquaredErrModule::bpropUpdate: 'input.size()' should be\n" " equal to 'input_size' + 'target_size' (%i != %i + %i)\n", in_size, input_size, target_size); } if( out_size != output_size ) { PLWARNING("SquaredErrModule::bpropUpdate: output.size()' should be\n" " equal to 'output_size' (%i != %i)\n", out_size, output_size); } if( og_size != output_size ) { PLWARNING("SquaredErrModule::bpropUpdate: 'output_gradient.size()'\n" " should be equal to 'output_size' (%i != %i)\n", og_size, output_size); } }
void PLearn::SquaredErrModule::bpropUpdate | ( | const Vec & | input, |
const Vec & | output, | ||
Vec & | input_gradient, | ||
const Vec & | output_gradient | ||
) | [virtual] |
Definition at line 112 of file SquaredErrModule.cc.
References i, PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLERROR, PLearn::TVec< T >::resize(), PLearn::TVec< T >::size(), PLearn::TVec< T >::subVec(), and target_size.
{ int in_size = input.size(); int out_size = output.size(); int og_size = output_gradient.size(); bool is_final_cost = false; // if yes, output_gradient is 1 // size check if( in_size != input_size + target_size ) { PLERROR("SquaredErrModule::bpropUpdate: 'input.size()' should be\n" " equal to 'input_size' + 'target_size' (%i != %i + %i)\n", in_size, input_size, target_size); } if( out_size != output_size ) { PLERROR("SquaredErrModule::bpropUpdate: output.size()' should be\n" " equal to 'output_size' (%i != %i)\n", out_size, output_size); } if( og_size == 0 ) { /* PLWARNING("SquaredErrModule::bpropUpdate: you are not providing" "output_gradient.\n" "Assuming this is the final cost, and output_gradient=1.\n"); */ is_final_cost = true; } else if( og_size != output_size ) { PLERROR("SquaredErrModule::bpropUpdate: 'output_gradient.size()'\n" " should be equal to 'output_size' (%i != %i)\n", og_size, output_size); } Vec input_ = input.subVec( 0, input_size ); Vec target = input.subVec( input_size, target_size ); input_gradient.resize( input_size ); for( int i=0 ; i<input_size ; i++ ) { if( is_final_cost ) input_gradient[i] = 2*( input_[i] - target[i] ); else input_gradient[i] = 2*( input_[i] - target[i] )*output_gradient[0]; } }
void PLearn::SquaredErrModule::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::OnlineLearningModule.
Definition at line 253 of file SquaredErrModule.cc.
References PLearn::OnlineLearningModule::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::SquaredErrModule::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 271 of file SquaredErrModule.cc.
References PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLWARNING, and target_size.
Referenced by build().
{ if( input_size < 0 ) { PLWARNING("SquaredErrModule::build_: 'input_size' is < 0.\n" "You should set it to a positive integer.\n" "Defaulting to '1' (scalar version)."); input_size = 1; } if( output_size != 1 ) { PLWARNING("SquaredErrModule::build_: 'output_size' (%i) should be 1.\n" "Setting 'output_size' to 1.\n", output_size); output_size = 1; } target_size = input_size; }
string PLearn::SquaredErrModule::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 53 of file SquaredErrModule.cc.
void PLearn::SquaredErrModule::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 266 of file SquaredErrModule.cc.
References PLearn::OnlineLearningModule::declareOptions().
{ inherited::declareOptions(ol); }
static const PPath& PLearn::SquaredErrModule::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 102 of file SquaredErrModule.h.
:
//##### Protected Options ###############################################
SquaredErrModule * PLearn::SquaredErrModule::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 53 of file SquaredErrModule.cc.
void PLearn::SquaredErrModule::forget | ( | ) | [virtual] |
reset the parameters to the state they would be BEFORE starting training.
Note that this method is necessarily called from build().
Implements PLearn::OnlineLearningModule.
Definition at line 246 of file SquaredErrModule.cc.
{
// target = Vec( input_size );
}
given the input, compute the output (possibly resize it appropriately) SOON TO BE DEPRECATED, USE fprop(const TVec<Mat*>& ports_value)
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 64 of file SquaredErrModule.cc.
References PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLERROR, PLearn::TVec< T >::resize(), PLearn::TVec< T >::size(), PLearn::TVec< T >::subVec(), PLearn::sumsquare(), and target_size.
{ int in_size = input.size(); // size check if( in_size != input_size+target_size ) { PLERROR("SquaredErrModule::fprop: 'input.size()' should be equal\n" " to 'input_size' + 'target_size' (%i != %i + %i)\n", in_size, input_size, target_size); } Vec target = input.subVec( input_size, target_size ); Vec input_ = input.subVec( 0, input_size ); output.resize( output_size ); output[0] = sumsquare( input_ - target ); }
OptionList & PLearn::SquaredErrModule::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 53 of file SquaredErrModule.cc.
OptionMap & PLearn::SquaredErrModule::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 53 of file SquaredErrModule.cc.
RemoteMethodMap & PLearn::SquaredErrModule::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 53 of file SquaredErrModule.cc.
void PLearn::SquaredErrModule::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 259 of file SquaredErrModule.cc.
References PLearn::OnlineLearningModule::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); // deepCopyField(target, copies); }
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 102 of file SquaredErrModule.h.
int PLearn::SquaredErrModule::target_size [protected] |
Definition at line 114 of file SquaredErrModule.h.
Referenced by bbpropUpdate(), bpropUpdate(), build_(), and fprop().