|
| 1 | +// -*- C++ -*- |
| 2 | + |
| 3 | +// AdditiveGaussianNoiseVariable.cc |
| 4 | +// |
| 5 | +// Copyright (C) 2010 Pascal Vincent |
| 6 | +// |
| 7 | +// Redistribution and use in source and binary forms, with or without |
| 8 | +// modification, are permitted provided that the following conditions are met: |
| 9 | +// |
| 10 | +// 1. Redistributions of source code must retain the above copyright |
| 11 | +// notice, this list of conditions and the following disclaimer. |
| 12 | +// |
| 13 | +// 2. Redistributions in binary form must reproduce the above copyright |
| 14 | +// notice, this list of conditions and the following disclaimer in the |
| 15 | +// documentation and/or other materials provided with the distribution. |
| 16 | +// |
| 17 | +// 3. The name of the authors may not be used to endorse or promote |
| 18 | +// products derived from this software without specific prior written |
| 19 | +// permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 22 | +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 23 | +// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| 24 | +// NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 26 | +// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +// |
| 32 | +// This file is part of the PLearn library. For more information on the PLearn |
| 33 | +// library, go to the PLearn Web site at www.plearn.org |
| 34 | + |
| 35 | +// Authors: Pascal Vincent |
| 36 | + |
| 37 | +/*! \file AdditiveGaussianNoiseVariable.cc */ |
| 38 | + |
| 39 | + |
| 40 | +#include "AdditiveGaussianNoiseVariable.h" |
| 41 | + |
| 42 | +namespace PLearn { |
| 43 | +using namespace std; |
| 44 | + |
| 45 | +/** AdditiveGaussianNoiseVariable **/ |
| 46 | + |
| 47 | +PLEARN_IMPLEMENT_OBJECT( |
| 48 | + AdditiveGaussianNoiseVariable, |
| 49 | + "Adds an isotropic Gaussian noise of given standard deviation", |
| 50 | + "" |
| 51 | + ); |
| 52 | + |
| 53 | +AdditiveGaussianNoiseVariable::AdditiveGaussianNoiseVariable() |
| 54 | + :sigma(1) |
| 55 | +{ |
| 56 | + // ### You may (or not) want to call build_() to finish building the object |
| 57 | + // ### (doing so assumes the parent classes' build_() have been called too |
| 58 | + // ### in the parent classes' constructors, something that you must ensure) |
| 59 | +} |
| 60 | + |
| 61 | +// constructor from input variable and parameters |
| 62 | +// AdditiveGaussianNoiseVariable::AdditiveGaussianNoiseVariable(Variable* input, param_type the_parameter,...) |
| 63 | +// ### replace with actual parameters |
| 64 | +// : inherited(input, this_variable_length, this_variable_width), |
| 65 | +// parameter(the_parameter), |
| 66 | +// ... |
| 67 | +//{ |
| 68 | +// // ### You may (or not) want to call build_() to finish building the |
| 69 | +// // ### object |
| 70 | +//} |
| 71 | + |
| 72 | +void AdditiveGaussianNoiseVariable::recomputeSize(int& l, int& w) const |
| 73 | +{ |
| 74 | + if (input) |
| 75 | + { |
| 76 | + l = input.length(); |
| 77 | + w = input.width(); // the computed width |
| 78 | + } |
| 79 | + else |
| 80 | + l = w = 0; |
| 81 | +} |
| 82 | + |
| 83 | +// ### computes value from input's value |
| 84 | +void AdditiveGaussianNoiseVariable::fprop() |
| 85 | +{ |
| 86 | + checkContiguity(); |
| 87 | + |
| 88 | + if(random_gen.isNull()) |
| 89 | + random_gen = PRandom::common(false); |
| 90 | + |
| 91 | + int n = value.length(); |
| 92 | + for(int i=0; i<n; i++) |
| 93 | + { |
| 94 | + valuedata[i] = random_gen->gaussian_mu_sigma(input->valuedata[i], sigma); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// ### computes input's gradient from gradient |
| 99 | +void AdditiveGaussianNoiseVariable::bprop() |
| 100 | +{ |
| 101 | +} |
| 102 | + |
| 103 | +// ### You can implement these methods: |
| 104 | +// void AdditiveGaussianNoiseVariable::bbprop() {} |
| 105 | +// void AdditiveGaussianNoiseVariable::symbolicBprop() {} |
| 106 | +// void AdditiveGaussianNoiseVariable::rfprop() {} |
| 107 | + |
| 108 | + |
| 109 | +// ### Nothing to add here, simply calls build_ |
| 110 | +void AdditiveGaussianNoiseVariable::build() |
| 111 | +{ |
| 112 | + inherited::build(); |
| 113 | + build_(); |
| 114 | +} |
| 115 | + |
| 116 | +void AdditiveGaussianNoiseVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) |
| 117 | +{ |
| 118 | + inherited::makeDeepCopyFromShallowCopy(copies); |
| 119 | + |
| 120 | + // ### Call deepCopyField on all "pointer-like" fields |
| 121 | + // ### that you wish to be deepCopied rather than |
| 122 | + // ### shallow-copied. |
| 123 | + // ### ex: |
| 124 | + deepCopyField(random_gen, copies); |
| 125 | + |
| 126 | + // ### If you want to deepCopy a Var field: |
| 127 | + // varDeepCopyField(somevariable, copies); |
| 128 | +} |
| 129 | + |
| 130 | +void AdditiveGaussianNoiseVariable::declareOptions(OptionList& ol) |
| 131 | +{ |
| 132 | + // ### Declare all of this object's options here. |
| 133 | + // ### For the "flags" of each option, you should typically specify |
| 134 | + // ### one of OptionBase::buildoption, OptionBase::learntoption or |
| 135 | + // ### OptionBase::tuningoption. If you don't provide one of these three, |
| 136 | + // ### this option will be ignored when loading values from a script. |
| 137 | + // ### You can also combine flags, for example with OptionBase::nosave: |
| 138 | + // ### (OptionBase::buildoption | OptionBase::nosave) |
| 139 | + |
| 140 | + declareOption(ol, "sigma", &AdditiveGaussianNoiseVariable::sigma, |
| 141 | + OptionBase::buildoption, |
| 142 | + "The standard deviation of the isotropic Gaussian noise to be added to the input"); |
| 143 | + declareOption(ol, "random_gen", &AdditiveGaussianNoiseVariable::random_gen, |
| 144 | + OptionBase::buildoption, |
| 145 | + "Random number generator. If null, the PRandom::common(false) generator will be used."); |
| 146 | + |
| 147 | + // Now call the parent class' declareOptions |
| 148 | + inherited::declareOptions(ol); |
| 149 | +} |
| 150 | + |
| 151 | +void AdditiveGaussianNoiseVariable::build_() |
| 152 | +{ |
| 153 | + // ### This method should do the real building of the object, |
| 154 | + // ### according to set 'options', in *any* situation. |
| 155 | + // ### Typical situations include: |
| 156 | + // ### - Initial building of an object from a few user-specified options |
| 157 | + // ### - Building of a "reloaded" object: i.e. from the complete set of |
| 158 | + // ### all serialised options. |
| 159 | + // ### - Updating or "re-building" of an object after a few "tuning" |
| 160 | + // ### options have been modified. |
| 161 | + // ### You should assume that the parent class' build_() has already been |
| 162 | + // ### called. |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +} // end of namespace PLearn |
| 167 | + |
| 168 | + |
| 169 | +/* |
| 170 | + Local Variables: |
| 171 | + mode:c++ |
| 172 | + c-basic-offset:4 |
| 173 | + c-file-style:"stroustrup" |
| 174 | + c-file-offsets:((innamespace . 0)(inline-open . 0)) |
| 175 | + indent-tabs-mode:nil |
| 176 | + fill-column:79 |
| 177 | + End: |
| 178 | +*/ |
| 179 | +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 : |
0 commit comments