PLearn 0.1
NullModule.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // NullModule.cc
00004 //
00005 // Copyright (C) 2007 Olivier Delalleau
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: Olivier Delalleau
00036 
00041 #include "NullModule.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 PLEARN_IMPLEMENT_OBJECT(
00047     NullModule,
00048     "An OnlineLearningModule that does nothing with its unique port.",
00049     "Such a module can be useful if a port of another module needs to be\n"
00050     "stored without its value being used outside of that module (e.g. to\n"
00051     "store the 'state' of the module): by adding a connection from that\n"
00052     "port to a NullModule, we ensure the value is stored, even if it is\n"
00053     "not actually used.\n"
00054 );
00055 
00057 // NullModule //
00059 NullModule::NullModule(const string& name, bool call_build_):
00060     inherited(name.empty() ? classname() : name, call_build_)
00061 {
00062     if (call_build_)
00063         build_();
00064 }
00065 
00066 void NullModule::declareOptions(OptionList& ol)
00067 {
00068     // ### Declare all of this object's options here.
00069     // ### For the "flags" of each option, you should typically specify
00070     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00071     // ### OptionBase::tuningoption. If you don't provide one of these three,
00072     // ### this option will be ignored when loading values from a script.
00073     // ### You can also combine flags, for example with OptionBase::nosave:
00074     // ### (OptionBase::buildoption | OptionBase::nosave)
00075 
00076     // ### ex:
00077     // declareOption(ol, "myoption", &NullModule::myoption,
00078     //               OptionBase::buildoption,
00079     //               "Help text describing this option");
00080     // ...
00081 
00082     // Now call the parent class' declareOptions
00083     inherited::declareOptions(ol);
00084 }
00085 
00086 void NullModule::build_()
00087 {
00088     // ### This method should do the real building of the object,
00089     // ### according to set 'options', in *any* situation.
00090     // ### Typical situations include:
00091     // ###  - Initial building of an object from a few user-specified options
00092     // ###  - Building of a "reloaded" object: i.e. from the complete set of
00093     // ###    all serialised options.
00094     // ###  - Updating or "re-building" of an object after a few "tuning"
00095     // ###    options have been modified.
00096     // ### You should assume that the parent class' build_() has already been
00097     // ### called.
00098 }
00099 
00101 // build //
00103 void NullModule::build()
00104 {
00105     inherited::build();
00106     build_();
00107 }
00108 
00110 // makeDeepCopyFromShallowCopy //
00112 void NullModule::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00113 {
00114     inherited::makeDeepCopyFromShallowCopy(copies);
00115 }
00116 
00118 // fprop //
00120 void NullModule::fprop(const Vec& input, Vec& output) const
00121 {
00122     // Nothing to do.
00123 }
00124 
00125 void NullModule::fprop(const TVec<Mat*>& ports_value)
00126 {
00127     // Nothing to do.
00128     PLASSERT( ports_value.length() == nPorts() );
00129     // Ensure we are not asking for the value of the 'null' port.
00130     PLASSERT( !ports_value[0] || !ports_value[0]->isEmpty() );
00131 }
00132 
00134 // bpropAccUpdate //
00136 void NullModule::bpropAccUpdate(const TVec<Mat*>& ports_value,
00137                                 const TVec<Mat*>& ports_gradient)
00138 {
00139     PLASSERT( ports_gradient.length() == nPorts() );
00140     Mat* null_grad = ports_gradient[0];
00141     if (null_grad) {
00142         // Noone should ever provide us with a gradient w.r.t. 'null'.
00143         PLASSERT( null_grad->isEmpty() );
00144         // All we need to do is resize the gradient matrix.
00145         PLASSERT( ports_value[0] );
00146         PLASSERT( null_grad->width() == ports_value[0]->width() );
00147         null_grad->resize(ports_value[0]->length(), null_grad->width());
00148     }
00149 }
00150 
00152 // getPorts //
00154 const TVec<string>& NullModule::getPorts() {
00155     static TVec<string> null_port;
00156     if (null_port.isEmpty())
00157         null_port.append("null");
00158     return null_port;
00159 }
00160 
00162 // getPortSizes //
00164 const TMat<int>& NullModule::getPortSizes() {
00165     static TMat<int> null_size;
00166     if (null_size.isEmpty()) {
00167         null_size.resize(1, 2);
00168         null_size.fill(-1);
00169     }
00170     return null_size;
00171 }
00172 
00174 // bpropUpdate //
00176 /* THIS METHOD IS OPTIONAL
00177 void NullModule::bpropUpdate(const Vec& input, const Vec& output,
00178                                Vec& input_gradient,
00179                                const Vec& output_gradient,
00180                                bool accumulate)
00181 {
00182 }
00183 */
00184 
00185 /* THIS METHOD IS OPTIONAL
00186 void NullModule::bpropUpdate(const Vec& input, const Vec& output,
00187                                const Vec& output_gradient)
00188 {
00189 }
00190 */
00191 
00193 // bbpropUpdate //
00195 /* THIS METHOD IS OPTIONAL
00196 void NullModule::bbpropUpdate(const Vec& input, const Vec& output,
00197                                 Vec& input_gradient,
00198                                 const Vec& output_gradient,
00199                                 Vec& input_diag_hessian,
00200                                 const Vec& output_diag_hessian,
00201                                 bool accumulate)
00202 {
00203 }
00204 */
00205 
00206 /* THIS METHOD IS OPTIONAL
00207 void NullModule::bbpropUpdate(const Vec& input, const Vec& output,
00208                                 const Vec& output_gradient,
00209                                 const Vec& output_diag_hessian)
00210 {
00211 }
00212 */
00213 
00215 // forget //
00217 void NullModule::forget()
00218 {
00219 }
00220 
00222 // finalize //
00224 /* THIS METHOD IS OPTIONAL
00225 void NullModule::finalize()
00226 {
00227 }
00228 */
00229 
00231 // bpropDoesNothing //
00233 /* THIS METHOD IS OPTIONAL
00234 bool NullModule::bpropDoesNothing()
00235 {
00236 }
00237 */
00238 
00240 // setLearningRate //
00242 /* OPTIONAL
00243 void NullModule::setLearningRate(real dynamic_learning_rate)
00244 {
00245 }
00246 */
00247 
00248 } // end of namespace PLearn
00249 
00250 
00251 /*
00252   Local Variables:
00253   mode:c++
00254   c-basic-offset:4
00255   c-file-style:"stroustrup"
00256   c-file-offsets:((innamespace . 0)(inline-open . 0))
00257   indent-tabs-mode:nil
00258   fill-column:79
00259   End:
00260 */
00261 // 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