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::ShiftAndRescaleVMatrix Class Reference

#include <ShiftAndRescaleVMatrix.h>

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

List of all members.

Public Member Functions

 ShiftAndRescaleVMatrix (bool call_build_=false)
 For all constructors, the original VMFields are copied upon construction.
 ShiftAndRescaleVMatrix (VMat the_source, bool call_build_=true)
 ShiftAndRescaleVMatrix (VMat the_source, Vec the_shift, Vec the_scale, bool call_build_=true)
 ShiftAndRescaleVMatrix (VMat the_source, int the_n_inputs, bool call_build_=true)
 ShiftAndRescaleVMatrix (VMat the_source, int the_n_inputs, int the_n_train, bool the_ignore_missing=false, bool the_verbosity=1, bool call_build_=true)
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ShiftAndRescaleVMatrixdeepCopy (CopiesMap &copies) const
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transform a shallow copy into a deep copy.
virtual void getNewRow (int i, const Vec &v) const
 Must be implemented in subclasses: default version returns an error.
virtual void reset_dimensions ()
 In case the dimensions of an underlying VMat has changed, recompute it.
virtual void build ()
 Simply calls inherited::build() then build_().

Static Public Member Functions

static string _classname_ ()
 ShiftAndRescaleVMatrix.
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

Vec shift
 x'_i = (x_i+shift_i)*scale_i
Vec scale
Vec min_max
real shared_shift
real shared_scale
bool automatic
 find shift and scale automatically?
int n_train
 when automatic, use the n_train first examples to estimate shift and scale
int n_inputs
bool negate_shift
bool no_scale
TVec< string > fields
int verbosity

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

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

Private Types

typedef SourceVMatrix inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

VMatrix that can be used to rescale and shift each feature of the source: x'_i = a_i*(x_i+b_i) This can be used to normalize the inputs of a distribution (see the NormalizeInputDistr function in PLearn.h)

Definition at line 58 of file ShiftAndRescaleVMatrix.h.


Member Typedef Documentation

Reimplemented from PLearn::SourceVMatrix.

Definition at line 60 of file ShiftAndRescaleVMatrix.h.


Constructor & Destructor Documentation

PLearn::ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix ( bool  call_build_ = false)

For all constructors, the original VMFields are copied upon construction.

Definition at line 61 of file ShiftAndRescaleVMatrix.cc.

References build_().

    : inherited(call_build_),
      shared_shift(0),
      shared_scale(1),
      automatic(1),
      n_train(0),
      n_inputs(-1),
      negate_shift(false),
      no_scale(false),
      verbosity(1)
{
    if( call_build_ )
        build_();
}

Here is the call graph for this function:

PLearn::ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix ( VMat  the_source,
bool  call_build_ = true 
)

Definition at line 76 of file ShiftAndRescaleVMatrix.cc.

References build_().

    : inherited(the_source,
                the_source->length(),
                the_source->width(),
                call_build_),
      automatic(1),
      n_train(0),
      n_inputs(-1),
      negate_shift(false),
      no_scale(false),
      verbosity(1)
{
    if( call_build_ )
        build_();
}

Here is the call graph for this function:

PLearn::ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix ( VMat  the_source,
Vec  the_shift,
Vec  the_scale,
bool  call_build_ = true 
)

Definition at line 93 of file ShiftAndRescaleVMatrix.cc.

References build_().

    : inherited(the_source,
                the_source->length(),
                the_source->width(),
                call_build_),
      shift(the_shift),
      scale(the_scale),
      automatic(0),
      n_train(0),
      n_inputs(-1),
      negate_shift(false),
      no_scale(false),
      verbosity(1)
{
    if( call_build_ )
        build_();
}

Here is the call graph for this function:

PLearn::ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix ( VMat  the_source,
int  the_n_inputs,
bool  call_build_ = true 
)

Definition at line 114 of file ShiftAndRescaleVMatrix.cc.

References build_().

    : inherited(the_source,
                the_source->length(),
                the_source->width(),
                call_build_),
      shift(the_source->width()),
      scale(the_source->width()),
      automatic(1),
      n_train(0),
      n_inputs(the_n_inputs),
      negate_shift(false),
      no_scale(false),
      verbosity(1)
{
    if( call_build_ )
        build_();
}

Here is the call graph for this function:

PLearn::ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix ( VMat  the_source,
int  the_n_inputs,
int  the_n_train,
bool  the_ignore_missing = false,
bool  the_verbosity = 1,
bool  call_build_ = true 
)

Definition at line 134 of file ShiftAndRescaleVMatrix.cc.

References build_(), and PLDEPRECATED.

    : inherited(the_source,
                the_source->length(),
                the_source->width(),
                call_build_),
      shift(the_source->width()),
      scale(the_source->width()),
      automatic(1),
      n_train(the_n_train),
      n_inputs(the_n_inputs),
      negate_shift(false),
      no_scale(false),
      verbosity(the_verbosity)
{
    PLDEPRECATED("In ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix - This "
                 "constructor is deprecated, as it takes an option "
                 "'ignore_missing' that does not exist anymore");
    if( call_build_ )
        build_();
}

Here is the call graph for this function:


Member Function Documentation

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

ShiftAndRescaleVMatrix.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

void PLearn::ShiftAndRescaleVMatrix::build ( ) [virtual]

Simply calls inherited::build() then build_().

Reimplemented from PLearn::SourceVMatrix.

Definition at line 411 of file ShiftAndRescaleVMatrix.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 239 of file ShiftAndRescaleVMatrix.cc.

References automatic, PLearn::computeMeanAndStddev(), PLearn::fast_exact_is_equal(), fields, PLearn::TVec< T >::fill(), PLearn::VMatrix::fill(), PLearn::VMat::getFieldIndex(), i, PLearn::invertElements(), PLearn::TVec< T >::isEmpty(), j, PLearn::TVec< T >::length(), min_max, n_inputs, n_train, negate_shift, PLearn::negateElements(), no_scale, PLASSERT, PLCHECK, PLERROR, PLWARNING, reset_dimensions(), PLearn::TVec< T >::resize(), PLearn::rowMax(), PLearn::rowMin(), scale, PLearn::SourceVMatrix::setMetaInfoFromSource(), shared_scale, shared_shift, shift, PLearn::TVec< T >::size(), PLearn::SourceVMatrix::source, PLearn::VMat::subMatColumns(), PLearn::VMat::subMatRows(), PLearn::TVec< T >::subVec(), PLearn::VMat::toMat(), PLearn::transpose(), verbosity, and PLearn::VMat::width().

Referenced by build(), and ShiftAndRescaleVMatrix().

{
    PLASSERT( n_inputs >= 0 || n_inputs == -1 || n_inputs == -2 );
    PLCHECK(fields.size()<=0||automatic);
    if( source )
    {
        if (automatic && min_max.isEmpty())
        {
            if (n_inputs<0)
            {
                PLCHECK( source->inputsize() >= 0 );
                if (n_inputs == -1)
                    n_inputs = source->inputsize();
                else if (n_inputs == -2) {
                    PLCHECK( source->targetsize() >= 0 );
                    n_inputs = source->inputsize() + source->targetsize();
                } else
                    PLERROR("In ShiftAndRescaleVMatrix::build_ - Wrong value "
                            "for 'n_inputs'");
                if (n_inputs<0)
                    PLERROR("ShiftAndRescaleVMatrix: either n_inputs should be"
                            " provided explicitly\n"
                            "or the source VMatrix should have a set value of"
                            " inputsize.\n");
            }
            if (n_train>0)
                computeMeanAndStddev(source.subMatRows(0,n_train),
                        shift, scale);
            else
                computeMeanAndStddev(source, shift, scale);
            if (!negate_shift)
                negateElements(shift);
            if (!no_scale) {
                for (int i=0;i<scale.length();i++)
                    if (fast_exact_is_equal(scale[i], 0))
                    {
                        if (verbosity >= 1)
                            PLWARNING("ShiftAndRescale: data column number %d"
                                      " is constant",i);
                        scale[i]=1;
                    }
                invertElements(scale);
                scale.resize(source->width());
                scale.subVec(n_inputs, scale.length()-n_inputs).fill(1);
            }
            
            if(fields.size()>0){
                //we shift and scale only the fields
                TVec<int> idx(fields.size());
                for(int i=0;i<fields.size();i++){
                    idx[i]=source->getFieldIndex(fields[i]);
                    if(idx[i]>n_inputs)
                        PLERROR("In ShiftAndRescaleVMatrix::build_() - The "
                                "fields index must be lower or equal to n_inputs");
                }
                for(int i=0;i<n_inputs;i++){
                    bool found=false;
                    for(int j=0;j<idx.size();j++){
                        if(i==idx[j])
                            found=true;
                    }
                    if(!found){
                        shift[i]=0;
                        scale[i]=1;
                    }
                }
            }
            shift.resize(source->width());
            shift.subVec(n_inputs, shift.length()-n_inputs).fill(0);
        }
        else {
            if (!min_max.isEmpty()) {
                
                if ( min_max.length()!=2 )
                    PLERROR("ShiftAndRescale: min_max should have exactly 2"
                            "elements(if set)") ; 
                
                if ( min_max[0] >= min_max[1])
                    PLERROR("ShiftAndRescale: min_max[0] should be smaller than"
                            "min_max[1]") ; 
            if (n_inputs<0)
            {
                PLCHECK(source->inputsize()>=0);
                if(n_inputs==-1)
                    n_inputs = source->inputsize();
                else if (n_inputs == -2) {
                    PLCHECK( source->targetsize() >= 0 );
                    n_inputs = source->inputsize() + source->targetsize();
                } else
                    PLERROR("In ShiftAndRescaleVMatrix::build_ - Wrong value "
                            "for 'n_inputs'");
            }
                
                Vec min_col(n_inputs) , max_col(n_inputs) ; 
                Mat data = transpose(source.subMatColumns(0,n_inputs).toMat());
                     
                rowMin(data , min_col) ; 
                rowMax(data , max_col) ; 
                
                shift.resize(source->width()) ; 
                shift.subVec(n_inputs, shift.length()-n_inputs).fill(0);
                scale.resize(source->width()) ; 
                scale.subVec(n_inputs, scale.length()-n_inputs).fill(1);

                for(int i=0 ; i<n_inputs ; ++i) { 
                    if (fast_exact_is_equal(min_col[i], max_col[i])) {
                        // Constant column.
                        shift[i] = (min_max[1] + min_max[0]) / 2 - min_col[i];
                        scale[i] = 1;
                    } else {
                        shift[i] = (max_col[i] - min_col[i]) /
                            (min_max[1] - min_max[0]) * 
                            (min_max[0] - (min_max[1] - min_max[0])*min_col[i]
                             / (max_col[i] - min_col[i]) ); 
                        scale[i] = (min_max[1] - min_max[0]) /
                            (max_col[i] - min_col[i]); 
                    }
                }

            }
            else
            {
                n_inputs = source->inputsize();
                if (n_inputs<0)
                {
                    n_inputs = source->inputsize();
                    if (n_inputs<0)
                        PLERROR("ShiftAndRescaleVMatrix: either n_inputs should be"
                                " provided explicitly\n"
                                "or the source VMatrix should have a set value of"
                                " inputsize.\n");
                }
                
                if( shift.length() == 0)
                {
                    shift.resize(source->width()) ; 
                    shift.subVec(n_inputs, shift.length()-n_inputs).fill(0);
                    shift.subVec(0,n_inputs).fill(shared_shift);
                }
                
                if( scale.length() == 0)
                {
                    scale.resize(source->width()) ; 
                    scale.subVec(n_inputs, scale.length()-n_inputs).fill(1);
                    scale.subVec(0,n_inputs).fill(shared_scale);
                }
            }
        }
        reset_dimensions();
        setMetaInfoFromSource();
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Declares this class' options.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 160 of file ShiftAndRescaleVMatrix.cc.

References automatic, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::SourceVMatrix::declareOptions(), fields, PLearn::OptionBase::learntoption, min_max, n_inputs, n_train, negate_shift, no_scale, PLearn::OptionBase::nosave, scale, shared_scale, shared_shift, shift, PLearn::SourceVMatrix::source, and verbosity.

{
    declareOption(ol, "underlying_vmat", &ShiftAndRescaleVMatrix::source,
                  (OptionBase::learntoption | OptionBase::nosave),
                  "DEPRECATED - Use 'source' instead.");

    declareOption(ol, "shift", &ShiftAndRescaleVMatrix::shift,
                  OptionBase::buildoption,
                  "Quantity added to each INPUT element of a row of the source"
                  " vmatrix.");

    declareOption(ol, "scale", &ShiftAndRescaleVMatrix::scale,
                  OptionBase::buildoption,
                  "Quantity multiplied to each shifted element of a row of the"
                  " source vmatrix.");

    declareOption(ol, "shared_shift", &ShiftAndRescaleVMatrix::shared_shift,
                  OptionBase::buildoption,
                  "Quantity added to ALL INPUT elements of a row of the source\n"
                  "vmatrix. This option is ignored if 'shift' is provided\n"
                  "or 'automatic' is true.\n");

    declareOption(ol, "shared_scale", &ShiftAndRescaleVMatrix::shared_scale,
                  OptionBase::buildoption,
                  "Quantity multiplied to ALL shifted INPUT elements of a row of the"
                  "source vmatrix. This option is ignored if 'scale' is provided,\n"
                  "'automatic' is true or 'no_scale' is true.\n");

    declareOption(ol, "automatic", &ShiftAndRescaleVMatrix::automatic,
                  OptionBase::buildoption,
                  "Whether shift and scale are determined from the mean and"
                  " stddev\n"
                  "of the source vmatrix, or user-provided.\n");

    declareOption(ol, "n_train", &ShiftAndRescaleVMatrix::n_train,
                  OptionBase::buildoption,
                  "when automatic, use only the n_train first examples to"
                  " estimate\n"
                  "shift and scale, if n_train>0.\n");

    declareOption(ol, "n_inputs", &ShiftAndRescaleVMatrix::n_inputs,
                  OptionBase::buildoption,
        "When automatic, shift and scale only the first n_inputs columns.\n"
        "Special values are as follows:\n"
        " -1 : n_inputs <- source->inputsize()\n"
        " -2 : n_inputs <- source->inputsize() + source->targetsize()");

    declareOption(ol, "negate_shift", &ShiftAndRescaleVMatrix::negate_shift,
                  OptionBase::buildoption,
                  "If set to 1, the shift will be removed instead of added.");

    declareOption(ol, "no_scale", &ShiftAndRescaleVMatrix::no_scale,
                  OptionBase::buildoption,
                  "If set to 1, no scaling will be performed (only a shift"
                  " will be applied).");

    declareOption(ol, "fields", &ShiftAndRescaleVMatrix::fields,
                  OptionBase::buildoption,
                  "The fields to shift and rescale. Automatic must be true and"
                  " if empty we use instead n_inputs.");

    declareOption(ol, "verbosity", &ShiftAndRescaleVMatrix::verbosity,
                  OptionBase::buildoption,
                  "Controls the amount of output.");

    declareOption(ol, "min_max", &ShiftAndRescaleVMatrix::min_max,            
                  OptionBase::buildoption,
        "A vector of size 2 [min,max]. For each column, the elements will be\n"
        "shifted and rescaled to be in [min,max]. If set, it will override\n"
        "the value of the 'automatic' option. A constant column will be set\n"
        "to the average of 'min' and 'max'.");        
    
    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 104 of file ShiftAndRescaleVMatrix.h.

{
ShiftAndRescaleVMatrix * PLearn::ShiftAndRescaleVMatrix::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

void PLearn::ShiftAndRescaleVMatrix::getNewRow ( int  i,
const Vec v 
) const [virtual]

Must be implemented in subclasses: default version returns an error.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 395 of file ShiftAndRescaleVMatrix.cc.

References negate_shift, no_scale, scale, shift, and PLearn::SourceVMatrix::source.

{
    source->getRow(i, v);

    if( negate_shift )
        v -= shift;
    else
        v += shift;

    if( !no_scale )
        v *= scale;
}
OptionList & PLearn::ShiftAndRescaleVMatrix::getOptionList ( ) const [virtual]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Reimplemented from PLearn::SourceVMatrix.

Definition at line 59 of file ShiftAndRescaleVMatrix.cc.

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

Transform a shallow copy into a deep copy.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 420 of file ShiftAndRescaleVMatrix.cc.

References PLearn::deepCopyField(), PLearn::SourceVMatrix::makeDeepCopyFromShallowCopy(), min_max, scale, and shift.

Here is the call graph for this function:

virtual void PLearn::ShiftAndRescaleVMatrix::reset_dimensions ( ) [inline, virtual]

In case the dimensions of an underlying VMat has changed, recompute it.

Reimplemented from PLearn::VMatrix.

Definition at line 111 of file ShiftAndRescaleVMatrix.h.

References PLERROR.

Referenced by build_().

    {
        if (width_>0 && width_!=source->width())
            PLERROR("ShiftAndRescaleVMatrix: can't change width");
        inherited::reset_dimensions();
    }

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from PLearn::SourceVMatrix.

Definition at line 104 of file ShiftAndRescaleVMatrix.h.

find shift and scale automatically?

Definition at line 75 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 83 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 69 of file ShiftAndRescaleVMatrix.h.

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

Definition at line 80 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), and declareOptions().

when automatic, use the n_train first examples to estimate shift and scale

Definition at line 79 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 81 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), declareOptions(), and getNewRow().

Definition at line 82 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), declareOptions(), and getNewRow().

Definition at line 72 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 71 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), and declareOptions().

x'_i = (x_i+shift_i)*scale_i

Definition at line 67 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), declareOptions(), getNewRow(), and makeDeepCopyFromShallowCopy().

Definition at line 84 of file ShiftAndRescaleVMatrix.h.

Referenced by build_(), and declareOptions().


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