PLearn 0.1
GeneralizedOneHotVMatrix.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: GeneralizedOneHotVMatrix.cc 7479 2007-06-01 00:43:49Z larocheh $
00039  ******************************************************* */
00040 
00041 #include "GeneralizedOneHotVMatrix.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 
00049 PLEARN_IMPLEMENT_OBJECT(GeneralizedOneHotVMatrix,
00050                         "VMatrix that maps many columns to a one-hot vector", 
00051                         "This VMat is a generalization of OneHotVMatrix where many columns (given\n"
00052                         "by the Vec index) are mapped, instead of just the last one.");
00053 
00054 GeneralizedOneHotVMatrix::GeneralizedOneHotVMatrix(bool call_build_)
00055     : inherited(call_build_)
00056 {
00057     if( call_build_ )
00058         build_();
00059 }
00060 
00061 GeneralizedOneHotVMatrix::GeneralizedOneHotVMatrix(VMat the_source,
00062                                                    Vec the_index,
00063                                                    Vec the_nclasses,
00064                                                    Vec the_cold_value,
00065                                                    Vec the_hot_value,
00066                                                    bool call_build_)
00067     : inherited(the_source,
00068                 the_source->length(),
00069                 the_source->width()
00070                     + (int)sum(the_nclasses)-the_nclasses.length(),
00071                 call_build_),
00072       index(the_index), nclasses(the_nclasses),
00073       cold_value(the_cold_value), hot_value(the_hot_value)
00074 {
00075     if (min(index)<0 || max(index)>source->length()-1)
00076         PLERROR("In GeneralizedOneHotVMatrix: all values of index must be in"
00077                 " range [0,%d]",
00078                 source->length()-1);
00079 
00080     if (index.length()!=nclasses.length() ||
00081         cold_value.length()!=hot_value.length() ||
00082         index.length()!=hot_value.length())
00083         PLERROR("In GeneralizedOneHotVMatrix: index, nclasses, cold_value\n"
00084                 "and hot_value must have same length.\n");
00085 
00086     if (call_build_)
00087         build_();
00088 }
00089 
00090 void GeneralizedOneHotVMatrix::build()
00091 {
00092     inherited::build();
00093     build_();
00094 }
00095 
00096 void GeneralizedOneHotVMatrix::build_()
00097 {
00098     length_ = source->length();
00099     width_ = source->width() + (int)sum(nclasses) - nclasses.length();
00100 
00101     TVec<string> fieldnames = source->fieldNames();
00102     TVec<string> extended_fieldnames( width() );
00103     int efn_pos = 0;
00104     for( int j=0 ; j<fieldnames.length() ; j++ )
00105     {
00106         const int index_pos = vec_find(index, (real)j);
00107         if( index_pos == -1 )
00108             extended_fieldnames[efn_pos++] = fieldnames[j];
00109         else
00110         {
00111             const int nb_class = (int)nclasses[index_pos];
00112             string name = fieldnames[j];
00113             for( int k=0 ; k<nb_class ; k++ )
00114                 extended_fieldnames[efn_pos++] = name + "_" + tostring(k);
00115 
00116         }
00117     }
00118     declareFieldNames( extended_fieldnames );
00119     setMetaInfoFromSource();
00120 }
00121 
00122 void GeneralizedOneHotVMatrix::declareOptions(OptionList &ol)
00123 {
00124     declareOption(ol, "distr", &GeneralizedOneHotVMatrix::source,
00125                   (OptionBase::learntoption | OptionBase::nosave),
00126                   "DEPRECATED - use 'source' instead.");
00127     declareOption(ol, "index", &GeneralizedOneHotVMatrix::index,
00128                   OptionBase::buildoption, "Columns to map to one-hot vector representation.");
00129     declareOption(ol, "nclasses", &GeneralizedOneHotVMatrix::nclasses,
00130                   OptionBase::buildoption, "Size of the one-hot vector for each columns to map.");
00131     declareOption(ol, "cold_value", &GeneralizedOneHotVMatrix::cold_value,
00132                   OptionBase::buildoption, "Cold values for all columns to map.");
00133     declareOption(ol, "hot_value", &GeneralizedOneHotVMatrix::hot_value,
00134                   OptionBase::buildoption, "Hot values for all columns to map.");
00135     inherited::declareOptions(ol);
00136 }
00137 
00138 void GeneralizedOneHotVMatrix::getNewRow(int i, const Vec& v) const
00139 {
00140 #ifdef BOUNDCHECK
00141     if(i<0 || i>=length())
00142         PLERROR("In OneHotVMatrix::getNewRow OUT OF BOUNDS");
00143     if(v.length()!=width())
00144         PLERROR("In GeneralizedOneHotVMatrix::getNewRow v.length() must be\n"
00145                 "equal to the VMat's width.\n");
00146 #endif
00147 
00148     Vec input(source->width());
00149     source->getRow(i, input);
00150     int v_pos = 0;
00151     for (int j=0; j<input.length(); j++) {
00152         const int index_pos = vec_find(index, (real)j);
00153         if (index_pos == -1)
00154             v[v_pos++] = input[j];
00155         else {
00156             const int nb_class = (int)nclasses[index_pos];
00157             Vec target = v.subVec(v_pos, nb_class);
00158             const real cold = cold_value[index_pos];
00159             const real hot = hot_value[index_pos];
00160             const real classnum = source->get(i,j); // Maybe not a desired behavior
00161             if(is_missing(classnum)) target.fill(cold);
00162             else fill_one_hot(target, (int) classnum, cold, hot);
00163             v_pos += nb_class;
00164         }
00165     }
00166 }
00167 
00169 // makeDeepCopyFromShallowCopy //
00171 void GeneralizedOneHotVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00172 {
00173     inherited::makeDeepCopyFromShallowCopy(copies);
00174 
00175     // ### Call deepCopyField on all "pointer-like" fields.
00176     // ### that you wish to be deepCopied rather than.
00177     // ### shallow-copied.
00178     // ### ex:
00179     // deepCopyField(trainvec, copies);
00180 
00181     deepCopyField(index, copies);
00182     deepCopyField(nclasses, copies);
00183     deepCopyField(cold_value, copies);
00184     deepCopyField(hot_value, copies);
00185 }
00186 
00187 
00188 
00189 } // end of namespace PLearn
00190 
00191 
00192 /*
00193   Local Variables:
00194   mode:c++
00195   c-basic-offset:4
00196   c-file-style:"stroustrup"
00197   c-file-offsets:((innamespace . 0)(inline-open . 0))
00198   indent-tabs-mode:nil
00199   fill-column:79
00200   End:
00201 */
00202 // 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