PLearn 0.1
ExtendedVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal
00006 // Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux
00007 //
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 //
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 //
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 //
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 //
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037 /* *******************************************************
00038  * $Id: ExtendedVMatrix.cc 6351 2006-10-25 19:05:45Z chapados $
00039  ******************************************************* */
00040 
00041 #include "ExtendedVMatrix.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 
00049 PLEARN_IMPLEMENT_OBJECT(
00050     ExtendedVMatrix,
00051     "Extends a VMatrix by filling new rows/columns with a constant",
00052     "VMatrix that extends the underlying VMat by appending rows at \n"
00053     "its top and bottom and columns at its left and right.\n"
00054     "The appended rows/columns are filled with the given fill_value\n"
00055     "This can be used for instance to easily implement the usual trick \n"
00056     "to include the bias in the weights vectors, by appending a 1 to\n"
00057     "the inputs.\n");
00058 
00059 
00060 ExtendedVMatrix::ExtendedVMatrix(bool call_build_)
00061     : inherited(call_build_),
00062       top_extent(0), bottom_extent(0), left_extent(0), right_extent(0),
00063       fill_value(0)
00064 {
00065     if( call_build_ )
00066         build_();
00067 }
00068 
00069 ExtendedVMatrix::ExtendedVMatrix(VMat the_source,
00070                                  int the_top_extent, int the_bottom_extent,
00071                                  int the_left_extent, int the_right_extent,
00072                                  real the_fill_value, bool call_build_)
00073     : inherited(the_source,
00074                 the_source->length()+the_top_extent+the_bottom_extent,
00075                 the_source->width()+the_left_extent+the_right_extent,
00076                 call_build_),
00077       top_extent(the_top_extent), bottom_extent(the_bottom_extent),
00078       left_extent(the_left_extent), right_extent(the_right_extent),
00079       fill_value(the_fill_value)
00080 {
00081     if( call_build_ )
00082         build_();
00083 }
00084 
00085 void ExtendedVMatrix::declareOptions(OptionList &ol)
00086 {
00087     declareOption(ol, "distr", &ExtendedVMatrix::source,
00088                   (OptionBase::learntoption | OptionBase::nosave),
00089                   "DEPRECATED - Use 'source' instead.");
00090     declareOption(ol, "top_extent", &ExtendedVMatrix::top_extent,
00091                   OptionBase::buildoption,
00092                   "Number of rows to add at the top");
00093     declareOption(ol, "bottom_extent", &ExtendedVMatrix::bottom_extent,
00094                   OptionBase::buildoption,
00095                   "Number of rows to add at the bottom");
00096     declareOption(ol, "left_extent", &ExtendedVMatrix::left_extent,
00097                   OptionBase::buildoption,
00098                   "Number of columns to add at the left");
00099     declareOption(ol, "right_extent", &ExtendedVMatrix::right_extent,
00100                   OptionBase::buildoption,
00101                   "Number of columns to add at the right");
00102     declareOption(ol, "fill_value", &ExtendedVMatrix::fill_value,
00103                   OptionBase::buildoption,
00104                   "Value to use to fill the added columns/rows");
00105 
00106     declareOption(
00107         ol, "extfieldnames", &ExtendedVMatrix::extfieldnames,
00108         OptionBase::buildoption,
00109         "The fieldnames to use for the added fields. Length must be equal to\n"
00110         "left_extent+right_extent.\n"
00111         "\n"
00112         "Default: [], i.e all are set to \"extended\"." );
00113 
00114     inherited::declareOptions(ol);
00115 }
00116 
00117 void ExtendedVMatrix::build()
00118 {
00119     inherited::build();
00120     build_();
00121 }
00122 
00123 void ExtendedVMatrix::build_()
00124 {
00125     this->length_ = source->length() + top_extent  + bottom_extent;
00126     this->width_  = source->width()  + left_extent + right_extent;
00127 
00128     if ( ! extfieldnames.isEmpty() )
00129         PLASSERT( extfieldnames.length() == left_extent + right_extent );
00130 
00131     TVec<string> fieldnames = source->fieldNames( );
00132     TVec<string> extended_fieldnames( width() );
00133     for ( int fno = 0, extno=0; fno < width(); fno++ )
00134         if ( fno < left_extent )
00135         {
00136             if ( extfieldnames.isEmpty() )
00137                 extended_fieldnames[fno] = "extended";
00138             else
00139                 extended_fieldnames[fno] = extfieldnames[extno++];
00140         }
00141         else if ( fno >= width()-right_extent )
00142         {
00143             if ( extfieldnames.isEmpty() )
00144                 extended_fieldnames[fno] = "extended";
00145             else
00146                 extended_fieldnames[fno] = extfieldnames[extno++];
00147         }
00148         else
00149             extended_fieldnames[fno]   = fieldnames[fno-left_extent];
00150 
00151     declareFieldNames( extended_fieldnames );
00152 
00153     setMetaInfoFromSource();
00154 }
00155 
00156 void ExtendedVMatrix::getNewRow(int i, const Vec& v) const
00157 {
00158 #ifdef BOUNDCHECK
00159     if(i<0 || i>=length())
00160         PLERROR("In ExtendedVMatrix::getNewRow OUT OF BOUNDS");
00161     if(v.length() != width())
00162         PLERROR("In ExtendedVMatrix::getNewRow v.length() must be equal to the VMat's width");
00163 #endif
00164 
00165     if(i<top_extent || i>=length()-bottom_extent)
00166         v.fill(fill_value);
00167     else
00168     {
00169         Vec subv = v.subVec(left_extent, source->width());
00170         source->getRow(i-top_extent,subv);
00171         if(left_extent>0)
00172             v.subVec(0,left_extent).fill(fill_value);
00173         if(right_extent>0)
00174             v.subVec(width()-right_extent,right_extent).fill(fill_value);
00175     }
00176 }
00177 
00179 // makeDeepCopyFromShallowCopy //
00181 void ExtendedVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00182 {
00183     inherited::makeDeepCopyFromShallowCopy(copies);
00184 
00185     // ### Call deepCopyField on all "pointer-like" fields.
00186     // ### that you wish to be deepCopied rather than.
00187     // ### shallow-copied.
00188     // ### ex:
00189     // deepCopyField(trainvec, copies);
00190 
00191     deepCopyField(extfieldnames, copies);
00192 
00193 }
00194 
00195 
00196 } // end of namespace PLearn
00197 
00198 
00199 /*
00200   Local Variables:
00201   mode:c++
00202   c-basic-offset:4
00203   c-file-style:"stroustrup"
00204   c-file-offsets:((innamespace . 0)(inline-open . 0))
00205   indent-tabs-mode:nil
00206   fill-column:79
00207   End:
00208 */
00209 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines