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

This class propagates a (possibly scaled) 'tanh' function. More...

#include <TanhModule.h>

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

List of all members.

Public Member Functions

 TanhModule ()
 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 fprop (const Mat &inputs, Mat &outputs)
 Mini-batch fprop.
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 Mat &inputs, const Mat &outputs, Mat &input_gradients, const Mat &output_gradients, bool accumulate=false)
 SOON TO BE DEPRECATED, USE bpropAccUpdate(const TVec<Mat*>& ports_value, const TVec<Mat*>& ports_gradient)
virtual void bpropUpdate (const Vec &input, const Vec &output, Vec &input_gradient, const Vec &output_gradient, bool accumulate=false)
 SOON TO BE DEPRECATED, USE bpropAccUpdate(const TVec<Mat*>& ports_value, const TVec<Mat*>& ports_gradient) this version allows to obtain the input gradient as well N.B.
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, bool accumulate=false)
 this version allows to obtain the input gradient and diag_hessian The flag indicates whether the input_gradient and input_diag_hessian gets accumulated into or set with the computed derivatives.
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 TanhModuledeepCopy (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 ()

Public Attributes

real in_scale
real ex_scale

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

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

Private Types

typedef OnlineLearningModule inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

This class propagates a (possibly scaled) 'tanh' function.

The output is ex_scale( in_scale * input ), for each coordinate. Usually in_scale = ex_scale = 1, but the values in_scale = 2/3 and ex_scale = 1.7159 are also used.

Definition at line 59 of file TanhModule.h.


Member Typedef Documentation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 61 of file TanhModule.h.


Constructor & Destructor Documentation

PLearn::TanhModule::TanhModule ( )

Default constructor.

Definition at line 57 of file TanhModule.cc.

                       :
    in_scale(1),
    ex_scale(1)
{
}

Member Function Documentation

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 55 of file TanhModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 55 of file TanhModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 55 of file TanhModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 55 of file TanhModule.cc.

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

Reimplemented from PLearn::Object.

Definition at line 55 of file TanhModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 55 of file TanhModule.cc.

void PLearn::TanhModule::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 190 of file TanhModule.cc.

References bpropUpdate(), PLearn::OnlineLearningModule::output_size, PLERROR, and PLearn::TVec< T >::size().

{
    int odh_size = output_diag_hessian.size();
    if( odh_size != output_size )
    {
        PLERROR("TanhModule::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::TanhModule::bbpropUpdate ( const Vec input,
const Vec output,
Vec input_gradient,
const Vec output_gradient,
Vec input_diag_hessian,
const Vec output_diag_hessian,
bool  accumulate = false 
) [virtual]

this version allows to obtain the input gradient and diag_hessian The flag indicates whether the input_gradient and input_diag_hessian gets accumulated into or set with the computed derivatives.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 207 of file TanhModule.cc.

References bpropUpdate(), PLearn::TVec< T >::clear(), PLearn::OnlineLearningModule::estimate_simpler_diag_hessian, ex_scale, i, in_scale, PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLASSERT_MSG, PLERROR, PLearn::TVec< T >::resize(), and PLearn::TVec< T >::size().

{
    int odh_size = output_diag_hessian.size();

    // size check
    // others size checks will be done in bpropUpdate()
    if( odh_size != output_size )
    {
        PLERROR("TanhModule::bbpropUpdate : 'output_diag_hessian.size()'\n"
                " should be equal to 'output_size' (%i != %i)\n",
                odh_size, output_size);
    }

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

    for( int i=0 ; i<input_size ; i++ )
    {
        real output_i = output[i];
        real fprime_i = in_scale * (ex_scale-output_i*output_i / ex_scale);

        if( estimate_simpler_diag_hessian )
            input_diag_hessian[i] +=
                fprime_i*fprime_i*output_diag_hessian[i];
        else
            input_diag_hessian[i] +=
                fprime_i*fprime_i*output_diag_hessian[i]
                - 2*in_scale/ex_scale*fprime_i*output_i*output_gradient[i];
    }

    bpropUpdate( input, output, input_gradient, output_gradient, accumulate );
}

Here is the call graph for this function:

void PLearn::TanhModule::bpropUpdate ( const Mat inputs,
const Mat outputs,
Mat input_gradients,
const Mat output_gradients,
bool  accumulate = false 
) [virtual]

SOON TO BE DEPRECATED, USE bpropAccUpdate(const TVec<Mat*>& ports_value, const TVec<Mat*>& ports_gradient)

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 96 of file TanhModule.cc.

References bpropUpdate(), i, PLearn::OnlineLearningModule::input_size, PLearn::TMat< T >::length(), PLASSERT, and PLearn::TMat< T >::resize().

{
    int mbs=inputs.length();
    PLASSERT(mbs==outputs.length() &&
             mbs==output_gradients.length());
    input_gradients.resize(mbs,input_size);
    for (int i=0;i<mbs;i++)
    {
        Vec in_i = inputs(i);
        Vec out_i = outputs(i);
        Vec gin_i = input_gradients(i);
        Vec gout_i = output_gradients(i);
        bpropUpdate(in_i,out_i,gin_i,gout_i,
                    accumulate);
    }
}

Here is the call graph for this function:

void PLearn::TanhModule::bpropUpdate ( const Vec input,
const Vec output,
Vec input_gradient,
const Vec output_gradient,
bool  accumulate = false 
) [virtual]

SOON TO BE DEPRECATED, USE bpropAccUpdate(const TVec<Mat*>& ports_value, const TVec<Mat*>& ports_gradient) this version allows to obtain the input gradient as well N.B.

THE DEFAULT IMPLEMENTATION JUST RAISES A PLERROR. The flag indicates whether the input_gradients gets accumulated into or set with the computed derivatives.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 143 of file TanhModule.cc.

References PLearn::TVec< T >::clear(), ex_scale, i, in_scale, PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLASSERT_MSG, PLERROR, PLearn::TVec< T >::resize(), and PLearn::TVec< T >::size().

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

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

    for( int i=0 ; i<input_size ; i++ )
    {
        real output_i = output[i];
        input_gradient[i] += in_scale *
            (ex_scale - output_i*output_i/ex_scale)*output_gradient[i];
    }

}

Here is the call graph for this function:

void PLearn::TanhModule::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 116 of file TanhModule.cc.

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

Referenced by bbpropUpdate(), and bpropUpdate().

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

    // size check
    if( in_size != input_size )
    {
        PLERROR("TanhModule::bpropUpdate: 'input.size()' should be equal\n"
                " to 'input_size' (%i != %i)\n", in_size, input_size);
    }
    if( out_size != output_size )
    {
        PLERROR("TanhModule::bpropUpdate: 'output.size()' should be equal\n"
                " to 'output_size' (%i != %i)\n", out_size, output_size);
    }
    if( og_size != output_size )
    {
        PLERROR("TanhModule::bpropUpdate: 'output_gradient.size()' should\n"
                  " 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::TanhModule::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 261 of file TanhModule.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 290 of file TanhModule.cc.

References PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, and PLERROR.

Referenced by build().

{
    if( input_size < 0 )
        PLERROR("TanhModule::build_: 'input_size' (%d) < 0", input_size);

    output_size = input_size;
}

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 55 of file TanhModule.cc.

void PLearn::TanhModule::declareOptions ( OptionList ol) [static, protected]
static const PPath& PLearn::TanhModule::declaringFile ( ) [inline, static]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 106 of file TanhModule.h.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 55 of file TanhModule.cc.

void PLearn::TanhModule::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 256 of file TanhModule.cc.

{}
void PLearn::TanhModule::fprop ( const Mat inputs,
Mat outputs 
) [virtual]

Mini-batch fprop.

Default implementation raises an error. SOON TO BE DEPRECATED, USE fprop(const TVec<Mat*>& ports_value)

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 84 of file TanhModule.cc.

References fprop(), i, PLearn::TMat< T >::length(), PLearn::OnlineLearningModule::output_size, and PLearn::TMat< T >::resize().

{
    int mbs=inputs.length();
    outputs.resize(mbs,output_size);
    for (int i=0;i<mbs;i++)
    {
        Vec in_i = inputs(i);
        Vec out_i = outputs(i);
        fprop(in_i,out_i);
    }
}

Here is the call graph for this function:

void PLearn::TanhModule::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 64 of file TanhModule.cc.

References ex_scale, PLearn::fasttanh(), i, in_scale, PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::output_size, PLERROR, PLearn::TVec< T >::resize(), PLearn::TVec< T >::size(), PLearn::tanh(), and PLearn::OnlineLearningModule::use_fast_approximations.

Referenced by fprop().

{
    int in_size = input.size();

    // size check
    if( in_size != input_size )
    {
        PLERROR("TanhModule::fprop: 'input.size()' should be equal\n"
                " to 'input_size' (%i != %i)\n", in_size, input_size);
    }

    output.resize( output_size );
    if (use_fast_approximations)
        for( int i=0 ; i<output_size ; i++ )
            output[i] = ex_scale * fasttanh( in_scale * input[i] );
    else
        for( int i=0 ; i<output_size ; i++ )
            output[i] = ex_scale * tanh( in_scale * input[i] );
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 55 of file TanhModule.cc.

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

Reimplemented from PLearn::Object.

Definition at line 55 of file TanhModule.cc.

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

Reimplemented from PLearn::Object.

Definition at line 55 of file TanhModule.cc.

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 267 of file TanhModule.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 TanhModule.h.

Definition at line 66 of file TanhModule.h.

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

Definition at line 65 of file TanhModule.h.

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


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