PLearn 0.1
SampleVariable.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 
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 
00039 /* *******************************************************      
00040  * $Id: SampleVariable.cc 3994 2005-08-25 13:35:03Z chapados $
00041  * AUTHORS: Pascal Vincent & Yoshua Bengio
00042  * This file is part of the PLearn library.
00043  ******************************************************* */
00044 
00045 
00046 #include "SampleVariable.h"
00047 #include <plearn/math/random.h>
00048 
00049 namespace PLearn {
00050 using namespace std;
00051 
00052 /*** SourceSampleVariable ***/
00053 
00054 string SourceSampleVariable::classname() const
00055 { return "SourceSampleVariable"; }
00056 
00057 VarArray SourceSampleVariable::random_sources() 
00058 { 
00059     if (marked)
00060         return VarArray(0,0);
00061     marked = true;
00062     return Var(this); 
00063 }
00064 
00065 /*** UnarySampleVariable ***/
00066 
00067 string UnarySampleVariable::classname() const
00068 { return "UnarySampleVariable"; }
00069 
00070 VarArray UnarySampleVariable::random_sources() 
00071 { 
00072     if (marked)
00073         return VarArray(0,0);
00074     marked = true;
00075     return input->random_sources() & Var(this); 
00076 }
00077 
00078 /*** BinarySampleVariable ***/
00079 
00080 string BinarySampleVariable::classname() const
00081 { return "BinarySampleVariable"; }
00082 
00083 VarArray BinarySampleVariable::random_sources() 
00084 { 
00085     if (marked)
00086         return VarArray(0,0);
00087     marked = true;
00088     return input1->random_sources() & input2->random_sources() & Var(this); 
00089 }
00090 
00091 /*** UniformSampleVariable ***/
00092 
00093 string UniformSampleVariable::classname() const
00094 { return "UniformSampleVariable"; }
00095 
00096 UniformSampleVariable::UniformSampleVariable( int length, int width,
00097                                               real minvalue, 
00098                                               real maxvalue)
00099     :SourceSampleVariable(length,width),
00100      min_value(minvalue),max_value(maxvalue)
00101 {
00102     sprintf(name,"U[%f,%f]",min_value,max_value);
00103 }
00104 
00105 UniformSampleVariable* UniformSampleVariable::deepCopy(CopiesMap& copies) const
00106 {
00107     CopiesMap::iterator it = copies.find(this);
00108     if (it!=copies.end()) // a copy already exists, so return it
00109         return (UniformSampleVariable*)it->second;
00110   
00111     // Otherwise call the copy constructor to obtain a SHALLOW copy
00112     UniformSampleVariable* deep_copy = new UniformSampleVariable(*this); 
00113     // Put the copy in the map
00114     copies[this] = deep_copy;
00115     // Transform the shallow copy into a deep copy
00116     deep_copy->makeDeepCopyFromShallowCopy(copies);
00117     // return the completed deep_copy
00118     return deep_copy;
00119 }
00120 
00121 void UniformSampleVariable::fprop()
00122 {
00123     for (int k=0;k<nelems();k++)
00124         valuedata[k] = bounded_uniform(min_value,max_value);
00125 
00126 }
00127 
00128 /*** MultinomialSampleVariable ***/
00129 
00130 string MultinomialSampleVariable::classname() const
00131 { return "MultinomialSampleVariable"; }
00132 
00133 MultinomialSampleVariable::MultinomialSampleVariable(Variable* probabilities, 
00134                                                      int length, int width)
00135     :UnarySampleVariable(probabilities, length, width)
00136 {
00137     sprintf(name,"Multinomial[%dx%d]",length,width);
00138 }
00139 
00140 MultinomialSampleVariable* MultinomialSampleVariable::deepCopy(CopiesMap& copies) const
00141 {
00142     CopiesMap::iterator it = copies.find(this);
00143     if (it!=copies.end()) // a copy already exists, so return it
00144         return (MultinomialSampleVariable*)it->second;
00145   
00146     // Otherwise call the copy constructor to obtain a SHALLOW copy
00147     MultinomialSampleVariable* deep_copy = new MultinomialSampleVariable(*this); 
00148     // Put the copy in the map
00149     copies[this] = deep_copy;
00150     // Transform the shallow copy into a deep copy
00151     deep_copy->makeDeepCopyFromShallowCopy(copies);
00152     // return the completed deep_copy
00153     return deep_copy;
00154 }
00155 
00156 void MultinomialSampleVariable::fprop()
00157 {
00158     for (int k=0;k<nelems();k++)
00159         valuedata[k] = multinomial_sample(input->value);
00160 
00161 }
00162 
00163 /*** DiagonalNormalSampleVariable ***/
00164 
00165 string DiagonalNormalSampleVariable::classname() const
00166 { return "DiagonalNormalSampleVariable"; }
00167 
00168 DiagonalNormalSampleVariable::DiagonalNormalSampleVariable
00169 (Variable* mu, Variable* sigma)
00170     :BinarySampleVariable(mu, sigma, mu->length(), mu->width()) 
00171 {
00172     if (!sigma->isScalar() && (mu->length()!=sigma->length() || mu->width()!=sigma->width()) )
00173         PLERROR("DiagonalNormalSampleVariable: mu(%d,%d) incompatible with sigma(%d,%d)",
00174                 mu->length(),mu->width(),sigma->length(),sigma->width());
00175 }
00176 
00177 DiagonalNormalSampleVariable* DiagonalNormalSampleVariable::deepCopy(CopiesMap& copies) const
00178 {
00179     CopiesMap::iterator it = copies.find(this);
00180     if (it!=copies.end()) // a copy already exists, so return it
00181         return (DiagonalNormalSampleVariable*)it->second;
00182   
00183     // Otherwise call the copy constructor to obtain a SHALLOW copy
00184     DiagonalNormalSampleVariable* deep_copy = new DiagonalNormalSampleVariable(*this); 
00185     // Put the copy in the map
00186     copies[this] = deep_copy;
00187     // Transform the shallow copy into a deep copy
00188     deep_copy->makeDeepCopyFromShallowCopy(copies);
00189     // return the completed deep_copy
00190     return deep_copy;
00191 }
00192 
00193 void DiagonalNormalSampleVariable::fprop()
00194 {
00195     if (input2->isScalar())
00196     {
00197         real sigma = input2->valuedata[0];
00198         for (int k=0;k<length();k++)
00199             valuedata[k] = gaussian_mu_sigma(input1->valuedata[k],
00200                                              sigma);
00201     }
00202     else
00203         for (int k=0;k<length();k++)
00204             valuedata[k] = gaussian_mu_sigma(input1->valuedata[k],
00205                                              input2->valuedata[k]);
00206 }
00207 
00208 
00209 } // end of namespace PLearn
00210 
00211 
00212 /*
00213   Local Variables:
00214   mode:c++
00215   c-basic-offset:4
00216   c-file-style:"stroustrup"
00217   c-file-offsets:((innamespace . 0)(inline-open . 0))
00218   indent-tabs-mode:nil
00219   fill-column:79
00220   End:
00221 */
00222 // 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