PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // AdditiveGaussianNoiseVariable.cc 00004 // 00005 // Copyright (C) 2010 Pascal Vincent 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 // Authors: Pascal Vincent 00036 00040 #include "AdditiveGaussianNoiseVariable.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00047 PLEARN_IMPLEMENT_OBJECT( 00048 AdditiveGaussianNoiseVariable, 00049 "Adds an isotropic Gaussian noise of given standard deviation", 00050 "" 00051 ); 00052 00053 AdditiveGaussianNoiseVariable::AdditiveGaussianNoiseVariable() 00054 :sigma(1) 00055 { 00056 // ### You may (or not) want to call build_() to finish building the object 00057 // ### (doing so assumes the parent classes' build_() have been called too 00058 // ### in the parent classes' constructors, something that you must ensure) 00059 } 00060 00061 // constructor from input variable and parameters 00062 // AdditiveGaussianNoiseVariable::AdditiveGaussianNoiseVariable(Variable* input, param_type the_parameter,...) 00063 // ### replace with actual parameters 00064 // : inherited(input, this_variable_length, this_variable_width), 00065 // parameter(the_parameter), 00066 // ... 00067 //{ 00068 // // ### You may (or not) want to call build_() to finish building the 00069 // // ### object 00070 //} 00071 00072 void AdditiveGaussianNoiseVariable::recomputeSize(int& l, int& w) const 00073 { 00074 if (input) 00075 { 00076 l = input.length(); 00077 w = input.width(); // the computed width 00078 } 00079 else 00080 l = w = 0; 00081 } 00082 00083 // ### computes value from input's value 00084 void AdditiveGaussianNoiseVariable::fprop() 00085 { 00086 checkContiguity(); 00087 00088 if(random_gen.isNull()) 00089 random_gen = PRandom::common(false); 00090 00091 int n = value.length(); 00092 for(int i=0; i<n; i++) 00093 { 00094 valuedata[i] = random_gen->gaussian_mu_sigma(input->valuedata[i], sigma); 00095 } 00096 } 00097 00098 // ### computes input's gradient from gradient 00099 void AdditiveGaussianNoiseVariable::bprop() 00100 { 00101 } 00102 00103 // ### You can implement these methods: 00104 // void AdditiveGaussianNoiseVariable::bbprop() {} 00105 // void AdditiveGaussianNoiseVariable::symbolicBprop() {} 00106 // void AdditiveGaussianNoiseVariable::rfprop() {} 00107 00108 00109 // ### Nothing to add here, simply calls build_ 00110 void AdditiveGaussianNoiseVariable::build() 00111 { 00112 inherited::build(); 00113 build_(); 00114 } 00115 00116 void AdditiveGaussianNoiseVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00117 { 00118 inherited::makeDeepCopyFromShallowCopy(copies); 00119 00120 // ### Call deepCopyField on all "pointer-like" fields 00121 // ### that you wish to be deepCopied rather than 00122 // ### shallow-copied. 00123 // ### ex: 00124 deepCopyField(random_gen, copies); 00125 00126 // ### If you want to deepCopy a Var field: 00127 // varDeepCopyField(somevariable, copies); 00128 } 00129 00130 void AdditiveGaussianNoiseVariable::declareOptions(OptionList& ol) 00131 { 00132 // ### Declare all of this object's options here. 00133 // ### For the "flags" of each option, you should typically specify 00134 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00135 // ### OptionBase::tuningoption. If you don't provide one of these three, 00136 // ### this option will be ignored when loading values from a script. 00137 // ### You can also combine flags, for example with OptionBase::nosave: 00138 // ### (OptionBase::buildoption | OptionBase::nosave) 00139 00140 declareOption(ol, "sigma", &AdditiveGaussianNoiseVariable::sigma, 00141 OptionBase::buildoption, 00142 "The standard deviation of the isotropic Gaussian noise to be added to the input"); 00143 declareOption(ol, "random_gen", &AdditiveGaussianNoiseVariable::random_gen, 00144 OptionBase::buildoption, 00145 "Random number generator. If null, the PRandom::common(false) generator will be used."); 00146 00147 // Now call the parent class' declareOptions 00148 inherited::declareOptions(ol); 00149 } 00150 00151 void AdditiveGaussianNoiseVariable::build_() 00152 { 00153 // ### This method should do the real building of the object, 00154 // ### according to set 'options', in *any* situation. 00155 // ### Typical situations include: 00156 // ### - Initial building of an object from a few user-specified options 00157 // ### - Building of a "reloaded" object: i.e. from the complete set of 00158 // ### all serialised options. 00159 // ### - Updating or "re-building" of an object after a few "tuning" 00160 // ### options have been modified. 00161 // ### You should assume that the parent class' build_() has already been 00162 // ### called. 00163 } 00164 00165 00166 } // end of namespace PLearn 00167 00168 00169 /* 00170 Local Variables: 00171 mode:c++ 00172 c-basic-offset:4 00173 c-file-style:"stroustrup" 00174 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00175 indent-tabs-mode:nil 00176 fill-column:79 00177 End: 00178 */ 00179 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :