PLearn 0.1
ReIndexedTargetVariable.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-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal
00006 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal
00007 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036 
00037 
00038 /* *******************************************************      
00039  * $Id: ReIndexedTargetVariable.cc 3994 2005-08-25 13:35:03Z chapados $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "ReIndexedTargetVariable.h"
00044 #include "ColumnIndexVariable.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00049 
00052 PLEARN_IMPLEMENT_OBJECT(ReIndexedTargetVariable,
00053                         "Variable that reindexes a variable according to a reference list of possible values.",
00054                         "Inspired from ReIndexedTargetVMatrix, this variable reindexes a\n"
00055                         "variable (input1) according to the getValues(row,target_col,values) function of\n"
00056                         "a given VMatrix. The user must specify the input part of the VMatrix\n"
00057                         "(input2), and the columns numbers corresponding to input1's fields.\n"
00058                         "In order to construct a row when calling getValues(row,target_col,values),\n"
00059                         "input2 is used for the input part, and the rest of the row is filled\n"
00060                         "with NaN.\n"
00061                         "The user can, alternatively, directly give a Dictionary object\n"
00062                         "that will give the possible values. Then, input2 is ignored.\n");
00063 
00064 
00065 ReIndexedTargetVariable::ReIndexedTargetVariable()
00066 {}
00067   
00068 ReIndexedTargetVariable::ReIndexedTargetVariable(Variable* input1, Variable* input2, VMat the_source, TVec<int> the_target_cols)
00069     : inherited(input1, input2, input1->length(), input1->width()), source(the_source), target_cols(the_target_cols)
00070 {
00071     build_();
00072 }
00073 
00074 ReIndexedTargetVariable::ReIndexedTargetVariable(Variable* input1, Variable* input2, PP<Dictionary> the_dict, TVec<int> the_target_cols)
00075     : inherited(input1, input2, input1->length(), input1->width()), target_cols(the_target_cols), dict(the_dict)
00076 {
00077     build_();
00078 }
00079 
00080 void
00081 ReIndexedTargetVariable::build()
00082 {
00083     inherited::build();
00084     build_();
00085 }
00086 
00087 void
00088 ReIndexedTargetVariable::build_()
00089 {
00090     if (input1 && input2 && source) {
00091         if(!input1->isVec()) 
00092             PLERROR("In ReIndexedTargetVariable: input1 must be a vector var");
00093         if(!input2->isVec())
00094             PLERROR("In ReIndexedTargetVariable: input2 must be a vector var");
00095 
00096         if(input1->size() != target_cols.length())
00097             PLERROR("In ReIndexedTargetVariable: input1 should be of size target_cols.length()");        
00098         if(input2->size() != source->inputsize())
00099             PLERROR("In ReIndexedTargetVariable: input2 should be of size source->inputsize()");
00100 
00101         row.resize(source->width());
00102         row.subVec(source->inputsize(),source->width()-source->inputsize()).fill(MISSING_VALUE);        
00103     }
00104     else if (input1 && dict)
00105     {
00106         if(!input1->isVec()) 
00107             PLERROR("In ReIndexedTargetVariable: input1 must be a vector var");
00108 
00109         if(input1->size() != target_cols.length())
00110             PLERROR("In ReIndexedTargetVariable: input1 should be of size target_cols.length()");        
00111     }
00112     options.resize(0);
00113 }
00114 
00115 void
00116 ReIndexedTargetVariable::declareOptions(OptionList &ol)
00117 {
00118     declareOption(ol, "source", &ReIndexedTargetVariable::source, OptionBase::buildoption, "");
00119     declareOption(ol, "dict", &ReIndexedTargetVariable::dict, OptionBase::buildoption, "");
00120     declareOption(ol, "target_cols", &ReIndexedTargetVariable::target_cols, OptionBase::buildoption, "");
00121     inherited::declareOptions(ol);
00122 }
00123 
00124 void ReIndexedTargetVariable::recomputeSize(int& l, int& w) const
00125 { l=input1->length(); w=input1->width(); }
00126 
00127 
00128 void ReIndexedTargetVariable::fprop()
00129 {
00130     if(source) row.subVec(0,source->inputsize()) << input2->value;
00131     
00132     for (int j=0; j<input1->size(); j++)
00133     {          
00134         if(source)
00135         {
00136             source->getValues(row,target_cols[j],values);
00137             valuedata[j] = values.find((int)input1->valuedata[j]);
00138         }
00139         else
00140         {
00141             dict->getValues(options,values);
00142             valuedata[j] = values.find((int)input1->valuedata[j]);
00143         }
00144         
00145         if(valuedata[j] < 0) PLERROR("In ReIndexedTargetVariable::fprop(): target %d is not among possible values",input1->valuedata[j]);
00146     }
00147 }
00148 
00149 
00150 void ReIndexedTargetVariable::bprop()
00151 {}
00152 
00153 
00154 void ReIndexedTargetVariable::symbolicBprop()
00155 {
00156     PLERROR("In ReIndexedTargetVariable::symbolicBprop(): not implemented");
00157 }
00158 
00159 
00160 void ReIndexedTargetVariable::rfprop()
00161 {
00162     PLERROR("In ReIndexedTargetVariable::rfprop(): not implemented.");
00163 }
00164 
00165 //#ifdef __INTEL_COMPILER
00166 //#pragma warning(disable:1419)  // Get rid of compiler warning.
00167 //#endif
00168 //extern void varDeepCopyField(Var& field, CopiesMap& copies);
00169 //#ifdef __INTEL_COMPILER
00170 //#pragma warning(default:1419)
00171 //#endif
00172 
00173 void ReIndexedTargetVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00174 {
00175     inherited::makeDeepCopyFromShallowCopy(copies);
00176     deepCopyField(source, copies);
00177     deepCopyField(row, copies);
00178     deepCopyField(values, copies);
00179     deepCopyField(options, copies);
00180     deepCopyField(target_cols, copies);
00181     deepCopyField(dict, copies);
00182 }
00183 
00184 
00185 
00186 } // end of namespace PLearn
00187 
00188 
00189 /*
00190   Local Variables:
00191   mode:c++
00192   c-basic-offset:4
00193   c-file-style:"stroustrup"
00194   c-file-offsets:((innamespace . 0)(inline-open . 0))
00195   indent-tabs-mode:nil
00196   fill-column:79
00197   End:
00198 */
00199 // 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