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

NLL (and derivatives thereof) between the target and input. More...

#include <NLLErrModule.h>

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

List of all members.

Public Member Functions

 NLLErrModule ()
 Default constructor.
virtual int getTarget (const Vec &input) const
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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual NLLErrModuledeepCopy (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.

Protected Attributes

int target_size

Private Types

typedef OnlineLearningModule inherited

Private Member Functions

void build_ ()
 This does the actual building.

Private Attributes

Vec fp_sm

Detailed Description

NLL (and derivatives thereof) between the target and input.

This class computes the Negative Log-Likelihood of the input, given the desired 'target'. Also propagates gradient and diagonal of Hessian backwards.

Deprecated:
: Use ../NLLCostModule if you only want the NLL, or SoftmaxModule and NLLCostModule (possibly in a ProcessInputCostModule) if you want softmax + NLL

Definition at line 64 of file NLLErrModule.h.


Member Typedef Documentation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 66 of file NLLErrModule.h.


Constructor & Destructor Documentation

PLearn::NLLErrModule::NLLErrModule ( )

Default constructor.

Definition at line 58 of file NLLErrModule.cc.

References PLearn::OnlineLearningModule::output_size.

                          :
    target_size(1)
    /* ### Initialize all fields to their default value */
{
    output_size = 1;
}

Member Function Documentation

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 56 of file NLLErrModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 56 of file NLLErrModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 56 of file NLLErrModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 56 of file NLLErrModule.cc.

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

Reimplemented from PLearn::Object.

Definition at line 56 of file NLLErrModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 56 of file NLLErrModule.cc.

void PLearn::NLLErrModule::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 198 of file NLLErrModule.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("NLLErrModule::bbpropUpdate:"
                  " 'output_diag_hessian.size()'\n"
                  " should be equal to 'output_size' (%i != %i)\n",
                  odh_size, output_size);
    }

    bpropUpdate( input, output, output_gradient );
}

Here is the call graph for this function:

void PLearn::NLLErrModule::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 215 of file NLLErrModule.cc.

References bpropUpdate(), PLearn::TVec< T >::clear(), PLearn::OnlineLearningModule::estimate_simpler_diag_hessian, getTarget(), i, PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLERROR, PLWARNING, PLearn::TVec< T >::resize(), PLearn::TVec< T >::size(), PLearn::softmax(), and PLearn::TVec< T >::subVec().

{
    int odh_size = output_diag_hessian.size();
    int target = getTarget( input );
    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("NLLErrModule::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("NLLErrModule::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 );
    input_diag_hessian.resize( input_size );
    Vec softmax_in = softmax( input_ );

    // 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
    {
        for( int i=0 ; i<input_size ; i++ )
        {
            real sm_i = softmax_in[i];
            input_diag_hessian[i] = sm_i*( 1-sm_i);
        }

        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 )
    {
        Vec fprime = softmax_in;
        fprime[target] -= 1;
        fprime *= fprime;

        input_diag_hessian += output_diag_hessian[0] * fprime;
    }

}

Here is the call graph for this function:

void PLearn::NLLErrModule::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 128 of file NLLErrModule.cc.

References getTarget(), PLearn::OnlineLearningModule::output_size, PLWARNING, and PLearn::TVec< T >::size().

Referenced by bbpropUpdate().

{
    int out_size = output.size();
    int og_size = output_gradient.size();

    // for size check
    getTarget( input );

    // size check
    if( out_size != output_size )
    {
        PLWARNING("NLLErrModule::bpropUpdate: output.size()' should be\n"
                  " equal to 'output_size' (%i != %i)\n",
                  out_size, output_size);
    }
    if( og_size != output_size )
    {
        PLWARNING("NLLErrModule::bpropUpdate: 'output_gradient.size()'\n"
                  " should be equal to 'output_size' (%i != %i)\n",
                  og_size, output_size);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::NLLErrModule::bpropUpdate ( const Vec input,
const Vec output,
Vec input_gradient,
const Vec output_gradient 
) [virtual]

Definition at line 154 of file NLLErrModule.cc.

References fp_sm, getTarget(), PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLERROR, PLearn::TVec< T >::resize(), and PLearn::TVec< T >::size().

{
    int out_size = output.size();
    int og_size = output_gradient.size();
    int target = getTarget( input );
    bool is_final_cost = false; // if yes, output_gradient is 1

    // size check
    if( out_size != output_size )
    {
        PLERROR("NLLErrModule::bpropUpdate: output.size()' should be\n"
                " equal to 'output_size' (%i != %i)\n",
                out_size, output_size);
    }
    if( og_size == 0 )
    {
        /*
        PLWARNING("NLLErrModule::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("NLLErrModule::bpropUpdate: 'output_gradient.size()'\n"
                " should be equal to 'output_size' (%i != %i)\n",
                og_size, output_size);
    }

    // input_gradient[i] = output_gradient*( softmax(input)[i] ) if i!=target
    // input_gradient[target] = output_gradient*( softmax(input)[target] )
    input_gradient.resize( input_size );
    input_gradient << fp_sm;

    input_gradient[target] -= 1;
    if( !is_final_cost )
        input_gradient *= output_gradient[0];

}

Here is the call graph for this function:

void PLearn::NLLErrModule::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 291 of file NLLErrModule.cc.

References PLearn::OnlineLearningModule::build(), and build_().

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 308 of file NLLErrModule.cc.

References fp_sm, PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLWARNING, PLearn::TVec< T >::resize(), and target_size.

Referenced by build().

{
    if( input_size < 0 )
    {
        PLWARNING("NLLErrModule::build_: 'input_size' is < 0.\n"
                  "You should set it to a positive integer.\n"
                  "Defaulting to '1' (like a sigmoid function ?)\n");
        input_size = 1;
    }
    if( output_size != 1 && output_size != 2 )
    {
        PLWARNING("NLLErrModule::build_: 'output_size' (%i) should be 1.\n"
                  "Setting 'output_size' to 1.\n", output_size);
        output_size = 1;
    }

    target_size = 1;

    fp_sm.resize(input_size);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 56 of file NLLErrModule.cc.

void PLearn::NLLErrModule::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 303 of file NLLErrModule.cc.

References PLearn::OnlineLearningModule::declareOptions().

Here is the call graph for this function:

static const PPath& PLearn::NLLErrModule::declaringFile ( ) [inline, static]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 106 of file NLLErrModule.h.

:
    //#####  Protected Options  ###############################################
NLLErrModule * PLearn::NLLErrModule::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 56 of file NLLErrModule.cc.

void PLearn::NLLErrModule::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 285 of file NLLErrModule.cc.

{
}
void PLearn::NLLErrModule::fprop ( const Vec input,
Vec output 
) const [virtual]

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 111 of file NLLErrModule.cc.

References PLearn::argmax(), fp_sm, getTarget(), PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, pl_log, PLearn::TVec< T >::resize(), PLearn::softmax(), and PLearn::TVec< T >::subVec().

{
    int target = getTarget( input );
    // size check is done in getTarget()

    Vec input_ = input.subVec( 0, input_size );
    output.resize( output_size );

    fp_sm = softmax( input_ );
    output[0] = - pl_log( fp_sm[target] );


    if( output_size > 1 )
        output[1] = ( argmax( input_ ) == target ) ? 0 : 1;
}

Here is the call graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 56 of file NLLErrModule.cc.

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

Reimplemented from PLearn::Object.

Definition at line 56 of file NLLErrModule.cc.

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

Reimplemented from PLearn::Object.

Definition at line 56 of file NLLErrModule.cc.

int PLearn::NLLErrModule::getTarget ( const Vec input) const [virtual]

Definition at line 67 of file NLLErrModule.cc.

References PLearn::argmax(), i, PLearn::OnlineLearningModule::input_size, PLearn::is_equal(), PLASSERT, PLERROR, PLearn::TVec< T >::size(), and PLearn::TVec< T >::subVec().

Referenced by bbpropUpdate(), bpropUpdate(), and fprop().

{
    int t_size = input.size() - input_size;
    int target = -1;

    // size check
    if( t_size == 1 )
    {
        target = (int) input[ input_size ];
    }
    else if( t_size == input_size )
    {
        /*
        PLWARNING("NLLErrModule::getTarget: You're giving a target the same\n"
                  "size as the input, instead of an integer. Checking if\n"
                  "this is a one-hot vector from this integer.\n");
         */

        Vec the_target = input.subVec( input_size, t_size );
        // get position of '1'
        target = argmax( the_target );

#ifdef BOUNDCHECK
        // check if vector matches with a one-hot one
        PLASSERT( is_equal( the_target[target], 1. ) ) ;
        for( int i=0 ; i<input_size ; i++ )
            PLASSERT( is_equal( the_target[i], 0. ) || i == target );
#endif
    }
    else
    {
        PLERROR("NLLErrModule::getTarget: target.size() is %i,\n"
                " but should be 1. 'target' should contain an integer.\n",
                t_size);
    }

    if( target < 0 || target >= input_size )
        PLERROR("NLLErrModule::getTarget: target should be between 0 and"
                "input_size (%i).\n", input_size);

    return target;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 297 of file NLLErrModule.cc.

References PLearn::OnlineLearningModule::makeDeepCopyFromShallowCopy().

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 106 of file NLLErrModule.h.

Vec PLearn::NLLErrModule::fp_sm [mutable, private]

Definition at line 135 of file NLLErrModule.h.

Referenced by bpropUpdate(), build_(), and fprop().

Definition at line 117 of file NLLErrModule.h.

Referenced by build_().


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