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

#include <UnfoldedFuncVariable.h>

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

List of all members.

Public Member Functions

 UnfoldedFuncVariable ()
 Default constructor.
 UnfoldedFuncVariable (Var inputmatrix, Func the_f, bool transpose, Var bagsize=(Variable *) NULL, bool call_build_=true)
 Concatenate_{i=0 to n_unfold} f(i-th row of input_matrix).
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual UnfoldedFuncVariabledeepCopy (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 ()
void printInfo (bool print_gradient)

Static Public Member Functions

static string _classname_ ()
 UnfoldedFuncVariable.
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 void declareOptions (OptionList &ol)
 Default constructor for persistence.

Public Attributes

Var input_matrix
Var bag_size
Func f
bool transpose
TVec< VarArrayinputs
TVec< Varoutputs
TVec< VarArrayf_paths

Static Public Attributes

static StaticInitializer _static_initializer_

Private Types

typedef NaryVariable inherited

Private Member Functions

void build_ ()
 Object-specific post-constructor.

Detailed Description

Definition at line 52 of file UnfoldedFuncVariable.h.


Member Typedef Documentation

Reimplemented from PLearn::NaryVariable.

Definition at line 54 of file UnfoldedFuncVariable.h.


Constructor & Destructor Documentation

PLearn::UnfoldedFuncVariable::UnfoldedFuncVariable ( )

Default constructor.

Definition at line 67 of file UnfoldedFuncVariable.cc.

                                          :
    transpose(false)
{}
PLearn::UnfoldedFuncVariable::UnfoldedFuncVariable ( Var  inputmatrix,
Func  the_f,
bool  transpose,
Var  bagsize = (Variable*) NULL,
bool  call_build_ = true 
)

Concatenate_{i=0 to n_unfold} f(i-th row of input_matrix).

Definition at line 71 of file UnfoldedFuncVariable.cc.

References build_().

                                      :
    inherited(VarArray(),
            the_transpose ? the_f->outputs[0]->length()
                                                * the_f->outputs[0]->width()
                          : inputmatrix->length(),
            the_transpose ? inputmatrix->width()
                          : the_f->outputs[0]->length()
                                                * the_f->outputs[0]->width(),
            call_build_),
      input_matrix(inputmatrix), 
      bag_size(bagsize),
      f(the_f),
      transpose(the_transpose)
{
    if (call_build_)
        build_();
}

Here is the call graph for this function:


Member Function Documentation

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

UnfoldedFuncVariable.

Reimplemented from PLearn::NaryVariable.

Definition at line 62 of file UnfoldedFuncVariable.cc.

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

Reimplemented from PLearn::NaryVariable.

Definition at line 62 of file UnfoldedFuncVariable.cc.

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

Reimplemented from PLearn::NaryVariable.

Definition at line 62 of file UnfoldedFuncVariable.cc.

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

Reimplemented from PLearn::NaryVariable.

Definition at line 62 of file UnfoldedFuncVariable.cc.

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

Reimplemented from PLearn::Object.

Definition at line 62 of file UnfoldedFuncVariable.cc.

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

Reimplemented from PLearn::NaryVariable.

Definition at line 62 of file UnfoldedFuncVariable.cc.

void PLearn::UnfoldedFuncVariable::bprop ( ) [virtual]

Implements PLearn::Variable.

Definition at line 222 of file UnfoldedFuncVariable.cc.

References bag_size, PLearn::TMat< T >::column(), f_paths, i, input_matrix, PLearn::Var::length(), PLearn::Variable::matGradient, outputs, PLearn::TMat< T >::toVecCopy(), transpose, and PLearn::Var::width().

{ 
    int n_unfold = bag_size ? int(round(bag_size->value[0]))
                            : transpose ? input_matrix->width()
                                        : input_matrix->length();
    for (int i=0;i<n_unfold;i++)
    {
        f_paths[i].clearGradient();
        if (transpose) {
            Vec tmp = matGradient.column(i).toVecCopy(); // TODO more efficient + check while it compiled without tmp = toVecCopy
            outputs[i]->gradient << tmp;
        } else {
            outputs[i]->gradient << matGradient(i);
        }
        f_paths[i].bprop();
    }
}

Here is the call graph for this function:

void PLearn::UnfoldedFuncVariable::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 94 of file UnfoldedFuncVariable.cc.

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

Here is the call graph for this function:

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

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 103 of file UnfoldedFuncVariable.cc.

References bag_size, PLearn::NaryVariable::build(), f, f_paths, i, input_matrix, inputs, j, PLearn::Var::length(), PLearn::TVec< T >::length(), PLearn::nonInputParentsOfPath(), outputs, PLASSERT, PLERROR, PLearn::propagationPath(), PLearn::TVec< T >::resize(), transpose, PLearn::Variable::Var, PLearn::NaryVariable::varray, and PLearn::Var::width().

Referenced by build(), and UnfoldedFuncVariable().

{
    if (f) {
        VarArray f_parents = nonInputParentsOfPath(f->inputs, f->outputs);
        varray.resize(f_parents.length() + 2);
        varray << (f_parents & input_matrix & bag_size);

        if(f->outputs.size()!=1)
            PLERROR("In UnfoldedFuncVariable: 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)");
        f->inputs.setDontBpropHere(true);
        int n_unfold = transpose ? input_matrix->width() : input_matrix->length();
        inputs.resize(n_unfold);
        outputs.resize(n_unfold);
        f_paths.resize(n_unfold);
        for (int i=0;i<n_unfold;i++)
        {
            inputs[i].resize(f->inputs.size());
            for (int j = 0; j < f->inputs.size(); j++) {
                inputs[i][j] = Var(f->inputs[j]->length(), f->inputs[j]->width());
            }
            outputs[i] = f(inputs[i])[0];
            f_paths[i] = propagationPath(inputs[i],outputs[i]);
        }
        inherited::build(); // Re-build since varray has changed.
    }

    if (bag_size)
        PLASSERT( bag_size->isScalar() );
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 62 of file UnfoldedFuncVariable.cc.

void PLearn::UnfoldedFuncVariable::declareOptions ( OptionList ol) [static]

Default constructor for persistence.

Reimplemented from PLearn::NaryVariable.

Definition at line 136 of file UnfoldedFuncVariable.cc.

References bag_size, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::NaryVariable::declareOptions(), f, input_matrix, PLearn::OptionBase::nosave, PLearn::redeclareOption(), transpose, and PLearn::NaryVariable::varray.

{
    declareOption(ol, "f", &UnfoldedFuncVariable::f, OptionBase::buildoption, 
                  "    Func that is replicated for each element of the 'bag' taken from the VMat.");

    declareOption(ol, "input_matrix", &UnfoldedFuncVariable::input_matrix, OptionBase::buildoption, 
        "Var containing the data: multiple consecutive rows form one bag.");

    declareOption(ol, "bag_size", &UnfoldedFuncVariable::bag_size,
                  OptionBase::buildoption, 
        "Optional Var that contains the size of the bag being presented.\n"
        "If provided, then only the corresponding number of function values\n"
        "will be computed, while the rest of the output data matrix will be\n"
        "left untouched.");

    declareOption(ol, "transpose", &UnfoldedFuncVariable::transpose, OptionBase::buildoption, 
                  "    If set to 1, then instead puts in the columns of the output matrix the values\n"
                  "    of f at the columns of the input matrix.");

    inherited::declareOptions(ol);

    redeclareOption(ol, "varray", &UnfoldedFuncVariable::varray,
                    OptionBase::nosave,
            "This option is set at build time from other options.");
}

Here is the call graph for this function:

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

Reimplemented from PLearn::NaryVariable.

Definition at line 77 of file UnfoldedFuncVariable.h.

:
    void build_();
UnfoldedFuncVariable * PLearn::UnfoldedFuncVariable::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::NaryVariable.

Definition at line 62 of file UnfoldedFuncVariable.cc.

void PLearn::UnfoldedFuncVariable::fprop ( ) [virtual]

compute output given input

Implements PLearn::Variable.

Definition at line 196 of file UnfoldedFuncVariable.cc.

References bag_size, PLearn::TMat< T >::column(), PLearn::Var::column(), f_paths, i, input_matrix, inputs, PLearn::is_equal(), PLearn::Var::length(), PLearn::Variable::matValue, outputs, PLASSERT, transpose, and PLearn::Var::width().

{
    int n_unfold = bag_size ? int(round(bag_size->value[0]))
                            : transpose ? input_matrix->width()
                                        : input_matrix->length();
    PLASSERT( !bag_size || is_equal(bag_size->value[0],
                                    round(bag_size->value[0])) );
    for (int i=0;i<n_unfold;i++) {
        if (transpose) {
            Vec tmp = input_matrix->matValue.column(i).toVecCopy(); // TODO something more efficient
            inputs[i] << tmp;
        } else {
            inputs[i] << input_matrix->matValue(i);
        }
        f_paths[i].fprop();
        if (transpose) {
            matValue.column(i) << outputs[i]->value;
        } else {
            matValue(i) << outputs[i]->value;
        }
    }
}

Here is the call graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 62 of file UnfoldedFuncVariable.cc.

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

Reimplemented from PLearn::Object.

Definition at line 62 of file UnfoldedFuncVariable.cc.

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

Reimplemented from PLearn::Object.

Definition at line 62 of file UnfoldedFuncVariable.cc.

void PLearn::UnfoldedFuncVariable::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);
      ...
  }
Parameters:
copiesA map used by the deep-copy mechanism to keep track of already-copied objects.

Reimplemented from PLearn::NaryVariable.

Definition at line 182 of file UnfoldedFuncVariable.cc.

References bag_size, PLearn::deepCopyField(), f, f_paths, input_matrix, inputs, PLearn::NaryVariable::makeDeepCopyFromShallowCopy(), and outputs.

Here is the call graph for this function:

void PLearn::UnfoldedFuncVariable::printInfo ( bool  print_gradient) [virtual]

Reimplemented from PLearn::NaryVariable.

Definition at line 243 of file UnfoldedFuncVariable.cc.

References bag_size, PLearn::endl(), f_paths, PLearn::Variable::getName(), PLearn::Variable::gradient, i, PLearn::Object::info(), input_matrix, PLearn::Var::length(), outputs, PLearn::pout, transpose, PLearn::Variable::value, and PLearn::Var::width().

{
    int n_unfold = bag_size ? int(round(bag_size->value[0]))
                            : transpose ? input_matrix->width()
                                        : input_matrix->length();
    for (int i=0;i<n_unfold;i++)
        f_paths[i].printInfo(print_gradient);
    pout << info() << " : " << getName() << "[" << (void*)this << "]" 
         << "(input_matrix=" << (void*)input_matrix << " ";
    for(int i=0; i<n_unfold; i++)
        pout << (void*)outputs[i] << " ";
    pout << ") = " << value;
    if (print_gradient)
        pout << " gradient=" << gradient;
    pout << endl; 
}

Here is the call graph for this function:

void PLearn::UnfoldedFuncVariable::recomputeSize ( int l,
int w 
) const [virtual]

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 165 of file UnfoldedFuncVariable.cc.

References f, input_matrix, PLearn::Var::length(), transpose, w, and PLearn::Var::width().

{
    if (f && f->outputs.size() > 0) {
        w = f->outputs[0]->length()*f->outputs[0]->width();
        if (transpose) {
            l = w;
            w = input_matrix->width();
        } else {
            l = input_matrix->length();
        }
    } else
        l = w = 0;
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::NaryVariable.

Definition at line 77 of file UnfoldedFuncVariable.h.

Definition at line 65 of file UnfoldedFuncVariable.h.

Referenced by bprop(), build_(), fprop(), makeDeepCopyFromShallowCopy(), and printInfo().

Definition at line 63 of file UnfoldedFuncVariable.h.

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

Definition at line 64 of file UnfoldedFuncVariable.h.

Referenced by bprop(), build_(), fprop(), makeDeepCopyFromShallowCopy(), and printInfo().

Definition at line 61 of file UnfoldedFuncVariable.h.

Referenced by bprop(), build_(), declareOptions(), fprop(), printInfo(), and recomputeSize().


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