PLearn 0.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Private Types | Private Member Functions
PLearn::SquaredErrorCostModule Class Reference

Computes the sum of squared difference between input and target. More...

#include <SquaredErrorCostModule.h>

Inheritance diagram for PLearn::SquaredErrorCostModule:
Inheritance graph
[legend]
Collaboration diagram for PLearn::SquaredErrorCostModule:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SquaredErrorCostModule ()
 Default constructor.
virtual void fprop (const TVec< Mat * > &ports_value)
 Perform a fprop step.
virtual void fprop (const Vec &input, const Vec &target, Vec &cost) const
 given the input and target, compute the cost
virtual void bpropUpdate (const Vec &input, const Vec &target, real cost, Vec &input_gradient, bool accumulate=false)
 Adapt based on the output gradient: this method should only be called just after a corresponding fprop.
virtual void bpropUpdate (const Mat &inputs, const Mat &targets, const Vec &costs, Mat &input_gradients, bool accumulate=false)
 Adapt based on the mini-batch cost gradient, and obtain the mini-batch input gradient.
virtual void bpropUpdate (const Vec &input, const Vec &target, real cost)
 Does nothing.
virtual void bbpropUpdate (const Vec &input, const Vec &target, real cost, Vec &input_gradient, Vec &input_diag_hessian, bool accumulate=false)
 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 &target, real cost)
 Does nothing.
virtual TVec< string > costNames ()
 Indicates the name of the computed costs.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual SquaredErrorCostModuledeepCopy (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 PPathdeclaringFile ()

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares the class options.

Private Types

typedef CostModule inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Computes the sum of squared difference between input and target.

Definition at line 50 of file SquaredErrorCostModule.h.


Member Typedef Documentation

Reimplemented from PLearn::CostModule.

Definition at line 52 of file SquaredErrorCostModule.h.


Constructor & Destructor Documentation

PLearn::SquaredErrorCostModule::SquaredErrorCostModule ( )

Default constructor.

Definition at line 52 of file SquaredErrorCostModule.cc.

{
    output_size = 1;
}

Member Function Documentation

string PLearn::SquaredErrorCostModule::_classname_ ( ) [static]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

OptionList & PLearn::SquaredErrorCostModule::_getOptionList_ ( ) [static]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

RemoteMethodMap & PLearn::SquaredErrorCostModule::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

bool PLearn::SquaredErrorCostModule::_isa_ ( const Object o) [static]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

Object * PLearn::SquaredErrorCostModule::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

StaticInitializer SquaredErrorCostModule::_static_initializer_ & PLearn::SquaredErrorCostModule::_static_initialize_ ( ) [static]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

void PLearn::SquaredErrorCostModule::bbpropUpdate ( const Vec input,
const Vec target,
real  cost,
Vec input_gradient,
Vec input_diag_hessian,
bool  accumulate = false 
) [virtual]

Similar to bpropUpdate, but adapt based also on the estimation of the diagonal of the Hessian matrix, and propagates this back.

Reimplemented from PLearn::CostModule.

Definition at line 183 of file SquaredErrorCostModule.cc.

References PLearn::TVec< T >::fill(), PLASSERT_MSG, PLearn::TVec< T >::resize(), and PLearn::TVec< T >::size().

{
    if( accumulate )
    {
        PLASSERT_MSG( input_diag_hessian.size() == input_size,
                      "Cannot resize input_diag_hessian AND accumulate into it"
                    );
        input_diag_hessian += real(2.);
    }
    else
    {
        input_diag_hessian.resize( input_size );
        input_diag_hessian.fill( 2. );
    }

    bpropUpdate( input, target, cost, input_gradient, accumulate );
}

Here is the call graph for this function:

virtual void PLearn::SquaredErrorCostModule::bbpropUpdate ( const Vec input,
const Vec target,
real  cost 
) [inline, virtual]

Does nothing.

Reimplemented from PLearn::CostModule.

Definition at line 92 of file SquaredErrorCostModule.h.

    {
    }
void PLearn::SquaredErrorCostModule::bpropUpdate ( const Mat inputs,
const Mat targets,
const Vec costs,
Mat input_gradients,
bool  accumulate = false 
) [virtual]

Adapt based on the mini-batch cost gradient, and obtain the mini-batch input gradient.

Reimplemented from PLearn::CostModule.

Definition at line 153 of file SquaredErrorCostModule.cc.

References PLearn::TMat< T >::clear(), i, PLearn::TMat< T >::length(), PLASSERT, PLASSERT_MSG, PLearn::TMat< T >::resize(), and PLearn::TMat< T >::width().

{
    PLASSERT( inputs.width() == input_size );
    PLASSERT( targets.width() == target_size );

    if( accumulate )
    {
        PLASSERT_MSG( input_gradients.width() == input_size &&
                      input_gradients.length() == inputs.length(),
                      "Cannot resize input_gradients AND accumulate into it" );
    }
    else
    {
        input_gradients.resize( inputs.length(), input_size );
        input_gradients.clear();
    }

    // input_gradient = 2*(input - target)
    // TODO This is a dumb unefficient implementation, for testing purpose.
    for (int k = 0; k < inputs.length(); k++)
        for( int i=0 ; i<input_size ; i++ )
        {
            input_gradients(k, i) += 2*(inputs(k, i) - targets(k, i));
        }
}

Here is the call graph for this function:

virtual void PLearn::SquaredErrorCostModule::bpropUpdate ( const Vec input,
const Vec target,
real  cost 
) [inline, virtual]

Does nothing.

Reimplemented from PLearn::CostModule.

Definition at line 81 of file SquaredErrorCostModule.h.

    {
    }
void PLearn::SquaredErrorCostModule::bpropUpdate ( const Vec input,
const Vec target,
real  cost,
Vec input_gradient,
bool  accumulate = false 
) [virtual]

Adapt based on the output gradient: this method should only be called just after a corresponding fprop.

Reimplemented from PLearn::CostModule.

Definition at line 128 of file SquaredErrorCostModule.cc.

References PLearn::TVec< T >::clear(), i, PLASSERT, PLASSERT_MSG, PLearn::TVec< T >::resize(), and PLearn::TVec< T >::size().

{
    PLASSERT( input.size() == input_size );
    PLASSERT( target.size() == target_size );

    if( accumulate )
    {
        PLASSERT_MSG( input_gradient.size() == input_size,
                      "Cannot resize input_gradient AND accumulate into it" );
    }
    else
    {
        input_gradient.resize( input_size );
        input_gradient.clear();
    }

    // input_gradient = 2*(input - target)
    for( int i=0 ; i<input_size ; i++ )
    {
        input_gradient[i] += 2*(input[i] - target[i]);
    }
}

Here is the call graph for this function:

void PLearn::SquaredErrorCostModule::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::CostModule.

Definition at line 78 of file SquaredErrorCostModule.cc.

void PLearn::SquaredErrorCostModule::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::CostModule.

Definition at line 70 of file SquaredErrorCostModule.cc.

string PLearn::SquaredErrorCostModule::classname ( ) const [virtual]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

TVec< string > PLearn::SquaredErrorCostModule::costNames ( ) [virtual]

Indicates the name of the computed costs.

Reimplemented from PLearn::CostModule.

Definition at line 205 of file SquaredErrorCostModule.cc.

{
    if (name == "" || name == classname())
        return TVec<string>(1, "mse");
    else
        return TVec<string>(1, name + ".mse");
}
void PLearn::SquaredErrorCostModule::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::CostModule.

Definition at line 57 of file SquaredErrorCostModule.cc.

{
    // declareOption(ol, "myoption", &SquaredErrorCostModule::myoption,
    //               OptionBase::buildoption,
    //               "Help text describing this option");

    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}
static const PPath& PLearn::SquaredErrorCostModule::declaringFile ( ) [inline, static]

Reimplemented from PLearn::CostModule.

Definition at line 104 of file SquaredErrorCostModule.h.

:
    //#####  Protected Member Functions  ######################################
SquaredErrorCostModule * PLearn::SquaredErrorCostModule::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

void PLearn::SquaredErrorCostModule::fprop ( const Vec input,
const Vec target,
Vec cost 
) const [virtual]

given the input and target, compute the cost

Reimplemented from PLearn::CostModule.

Definition at line 115 of file SquaredErrorCostModule.cc.

References PLASSERT, PLearn::powdistance(), PLearn::TVec< T >::resize(), and PLearn::TVec< T >::size().

{
    PLASSERT( input.size() == input_size );
    PLASSERT( target.size() == target_size );
    cost.resize( output_size );

    cost[0] = powdistance( input, target );
}

Here is the call graph for this function:

void PLearn::SquaredErrorCostModule::fprop ( const TVec< Mat * > &  ports_value) [virtual]

Perform a fprop step.

Reimplemented from PLearn::CostModule.

Definition at line 97 of file SquaredErrorCostModule.cc.

References i, PLearn::TMat< T >::isEmpty(), PLearn::TVec< T >::length(), PLASSERT, PLearn::powdistance(), and PLearn::TMat< T >::resize().

{
    PLASSERT( ports_value.length() == 3 );
    Mat* pred = ports_value[0];
    Mat* target = ports_value[1];
    Mat* mse = ports_value[2];
    if (mse && mse->isEmpty()) {
        PLASSERT( pred && !pred->isEmpty() && target && !target->isEmpty() );
        mse->resize(pred->length(), 1);
        // TODO It may be possible to come up with a more efficient
        // implementation.
        for (int i = 0; i < pred->length(); i++) {
            (*mse)(i, 0) = powdistance( (*pred)(i), (*target)(i) );
        }
    }
    checkProp(ports_value);
}

Here is the call graph for this function:

OptionList & PLearn::SquaredErrorCostModule::getOptionList ( ) const [virtual]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

OptionMap & PLearn::SquaredErrorCostModule::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

RemoteMethodMap & PLearn::SquaredErrorCostModule::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::CostModule.

Definition at line 50 of file SquaredErrorCostModule.cc.

void PLearn::SquaredErrorCostModule::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::CostModule.

Definition at line 88 of file SquaredErrorCostModule.cc.


Member Data Documentation

Reimplemented from PLearn::CostModule.

Definition at line 104 of file SquaredErrorCostModule.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines