PLearn 0.1
SourceVariable.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 and University of Montreal
00006 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 
00036  
00037 
00038 /* *******************************************************      
00039  * $Id: SourceVariable.cc 9095 2008-06-03 21:10:27Z plearner $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "SourceVariable.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00050 PLEARN_IMPLEMENT_OBJECT(
00051     SourceVariable,
00052     "Simple variable that views a source Mat data.",
00053     ""
00054 );
00055 
00057 // SourceVariable //
00059 SourceVariable::SourceVariable():
00060     build_length(-1),
00061     build_width(-1),
00062     random_type("none"),
00063     random_a(0.),
00064     random_b(1.),
00065     random_clear_first_row(false)
00066 {}
00067 
00068 SourceVariable::SourceVariable(int thelength, int thewidth, bool call_build_):
00069     inherited(thelength, thewidth, call_build_),
00070     build_length(-1),
00071     build_width(-1),
00072     random_type("none"),
00073     random_a(0.),
00074     random_b(1.),
00075     random_clear_first_row(false)
00076 {
00077     if (call_build_)
00078         build_();
00079 }
00080 
00081 SourceVariable::SourceVariable(int thelength, int thewidth, string random_type_, 
00082                                real random_a_, real random_b_, bool clear_first_row_,
00083                                bool call_build_): 
00084     inherited(thelength, thewidth, call_build_),
00085     build_length(thelength),
00086     build_width(thewidth),
00087     random_type(random_type_),
00088     random_a(random_a_),
00089     random_b(random_b_),
00090     random_clear_first_row(clear_first_row_)
00091 {
00092     if (call_build_)
00093         build_();
00094 }
00095 
00096 SourceVariable::SourceVariable(const Vec& v, bool vertical, bool call_build_):
00097     inherited(vertical ?v.toMat(v.length(),1) :v.toMat(1,v.length()),
00098               call_build_),
00099     build_length(-1),
00100     build_width(-1),
00101     random_type("none"),
00102     random_a(0.),
00103     random_b(1.),
00104     random_clear_first_row(false)
00105 {
00106     if (call_build_)
00107         build_();
00108 }
00109 
00110 SourceVariable::SourceVariable(const Mat& m, bool call_build_):
00111     inherited(m, call_build_),
00112     build_length(-1),
00113     build_width(-1),
00114     random_type("none"),
00115     random_a(0.),
00116     random_b(1.),
00117     random_clear_first_row(false)
00118 {
00119     if (call_build_)
00120         build_();
00121 }
00122 
00124 // declareOptions //
00126 void SourceVariable::declareOptions(OptionList& ol)
00127 {
00128     declareOption(ol, "build_length", &SourceVariable::build_length, OptionBase::buildoption, 
00129                   "Forced value of the variable's length.");
00130 
00131     declareOption(ol, "build_width", &SourceVariable::build_width, OptionBase::buildoption, 
00132                   "Forced value of the variable's width.");
00133 
00134     declareOption(ol, "random_type", &SourceVariable::random_type, OptionBase::buildoption, 
00135                   "Type of random generation to use:\n"
00136                   " - none    = no random initialization\n"
00137                   " - fill    = fill with 'random_a'\n"
00138                   " - uniform = uniform distribution in [random_a, random_b]\n"
00139                   " - normal  = normal distribution with mean 'random_a' and\n"
00140                   "             standard deviation 'random_b'\n"
00141         "Note that the variable is not filled randomly at build time: random\n"
00142         "generation requires an explicit call to randomInitialize(..).");
00143 
00144     declareOption(ol, "random_a", &SourceVariable::random_a, OptionBase::buildoption, 
00145                   "A first parameter for random generation");
00146 
00147     declareOption(ol, "random_b", &SourceVariable::random_b, OptionBase::buildoption, 
00148                   "A second parameter for random generation");
00149 
00150     declareOption(ol, "random_clear_first_row", &SourceVariable::random_clear_first_row, OptionBase::buildoption, 
00151                   "Indicates if we assign 0 to the elements of the first "
00152                   "row when doing random generation");
00153 
00154     inherited::declareOptions(ol);
00155 }
00156 
00157 
00159 // build_ //
00161 void SourceVariable::build_()
00162 { 
00163     if(build_length>0 && build_width>0)
00164         resize(build_length, build_width);
00165 }
00166 
00168 // build //
00170 void SourceVariable::build()
00171 {
00172     inherited::build();
00173     build_();
00174 }
00175 
00176 
00177 void SourceVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00178 {
00179     inherited::makeDeepCopyFromShallowCopy(copies);
00180     deepCopyField(rows_to_update, copies);
00181 }
00182 
00183 void SourceVariable::fprop() {} // No input: nothing to fprop
00184 void SourceVariable::bprop() {} // No input: nothing to bprop
00185 void SourceVariable::bbprop() {} // No input: nothing to bbprop
00186 void SourceVariable::rfprop() {} // No input: nothing to rfprop
00187 void SourceVariable::symbolicBprop() {} // No input: nothing to bprop
00188 
00189 VarArray SourceVariable::sources() 
00190 { 
00191     if (!marked)
00192     {
00193         setMark();
00194         return Var(this);
00195     }
00196     return VarArray(0,0);
00197 }
00198 
00199 VarArray SourceVariable::random_sources() 
00200 { 
00201     if (!marked)
00202         setMark();
00203     return VarArray(0,0);
00204 }
00205 
00206 VarArray SourceVariable::ancestors() 
00207 { 
00208     if (marked)
00209         return VarArray(0,0);
00210     setMark();
00211     return Var(this);
00212 }
00213 
00214 void SourceVariable::unmarkAncestors()
00215 { 
00216     if (marked)
00217         clearMark();
00218 }
00219 
00220 VarArray SourceVariable::parents()
00221 { return VarArray(0,0); }
00222 
00223 bool SourceVariable::markPath()
00224 { return marked; }
00225 
00226 void SourceVariable::buildPath(VarArray& proppath)
00227 {
00228     if(marked)
00229     {
00230         //cout<<"add:"<<this->getName()<<endl;
00231         proppath.append(Var(this));
00232         clearMark();
00233     }
00234 }
00235 
00237 // randomInitialize //
00239 void SourceVariable::randomInitialize(PP<PRandom> random_gen)
00240 {
00241     Mat values = matValue;
00242     if(random_clear_first_row)
00243         values = values.subMatRows(1, values.length()-1);
00244 
00245     if (random_type == "fill") {
00246         values.fill(random_a);
00247     } else if (random_type == "uniform") {
00248         random_gen->fill_random_uniform(values, random_a, random_b);
00249     } else if (random_type == "normal") {
00250         random_gen->fill_random_normal(values, random_a, random_b);
00251     } else {
00252         PLERROR("In SourceVariable::randomInitialize - Invalid value for "
00253                 "'random_type': %s", random_type.c_str());
00254     }
00255 }
00256 
00257 } // end of namespace PLearn
00258 
00259 
00260 /*
00261   Local Variables:
00262   mode:c++
00263   c-basic-offset:4
00264   c-file-style:"stroustrup"
00265   c-file-offsets:((innamespace . 0)(inline-open . 0))
00266   indent-tabs-mode:nil
00267   fill-column:79
00268   End:
00269 */
00270 // 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