PLearn 0.1
ArgmaxModule.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ArgmaxModule.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 "ArgmaxModule.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 PLEARN_IMPLEMENT_OBJECT(
00047     ArgmaxModule,
00048     "Compute the argmax of its input.",
00049     "The 'output' port is a single-column matrix, where the i-th element\n"
00050     "is the index of the column in the i-th row of the 'input' port that\n"
00051     "contains the largest number in that row.\n"
00052 );
00053 
00054 ArgmaxModule::ArgmaxModule()
00055 {
00056     output_size = 1;
00057 }
00058 
00059 void ArgmaxModule::declareOptions(OptionList& ol)
00060 {
00061     // ### Declare all of this object's options here.
00062     // ### For the "flags" of each option, you should typically specify
00063     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00064     // ### OptionBase::tuningoption. If you don't provide one of these three,
00065     // ### this option will be ignored when loading values from a script.
00066     // ### You can also combine flags, for example with OptionBase::nosave:
00067     // ### (OptionBase::buildoption | OptionBase::nosave)
00068 
00069     // ### ex:
00070     // declareOption(ol, "myoption", &ArgmaxModule::myoption,
00071     //               OptionBase::buildoption,
00072     //               "Help text describing this option");
00073     // ...
00074 
00075     // Now call the parent class' declareOptions
00076     inherited::declareOptions(ol);
00077 }
00078 
00079 void ArgmaxModule::build_()
00080 {
00081     // ### This method should do the real building of the object,
00082     // ### according to set 'options', in *any* situation.
00083     // ### Typical situations include:
00084     // ###  - Initial building of an object from a few user-specified options
00085     // ###  - Building of a "reloaded" object: i.e. from the complete set of
00086     // ###    all serialised options.
00087     // ###  - Updating or "re-building" of an object after a few "tuning"
00088     // ###    options have been modified.
00089     // ### You should assume that the parent class' build_() has already been
00090     // ### called.
00091 }
00092 
00093 // ### Nothing to add here, simply calls build_
00094 void ArgmaxModule::build()
00095 {
00096     inherited::build();
00097     build_();
00098 }
00099 
00100 
00101 void ArgmaxModule::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00102 {
00103     inherited::makeDeepCopyFromShallowCopy(copies);
00104 
00105     // ### Call deepCopyField on all "pointer-like" fields
00106     // ### that you wish to be deepCopied rather than
00107     // ### shallow-copied.
00108     // ### ex:
00109     // deepCopyField(trainvec, copies);
00110 
00111     // ### Remove this line when you have fully implemented this method.
00112     PLERROR("ArgmaxModule::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00113 }
00114 
00116 // fprop //
00118 void ArgmaxModule::fprop(const TVec<Mat*>& ports_value)
00119 {
00120     PLASSERT( ports_value.length() == nPorts() );
00121     Mat* input = ports_value[0];
00122     Mat* output = ports_value[1];
00123     PLCHECK( input && output && !input->isEmpty() && output->isEmpty() );
00124 
00125     output->resize(input->length(), 1);
00126     for (int i = 0; i < input->length(); i++)
00127         (*output)(i, 0) = argmax((*input)(i));
00128 
00129     // Ensure all required ports have been computed.
00130     checkProp(ports_value);
00131 }
00132 
00134 // bpropAccUpdate //
00136 void ArgmaxModule::bpropAccUpdate(const TVec<Mat*>& ports_value,
00137                                   const TVec<Mat*>& ports_gradient)
00138 {
00139     PLASSERT( ports_value.length() == nPorts() && ports_gradient.length() == nPorts());
00140 
00141     // Currently not implemented. Thus the check below should crash if we
00142     // actually ask to compute a gradient.
00143 
00144     // Ensure all required gradients have been computed.
00145     checkProp(ports_gradient);
00146 }
00147 
00149 // forget //
00151 void ArgmaxModule::forget()
00152 {
00153     // Nothing to forget.
00154 }
00155 
00157 // getPorts //
00159 const TVec<string>& ArgmaxModule::getPorts() {
00160     return inherited::getPorts();
00161 }
00162 
00163 }
00164 // end of namespace PLearn
00165 
00166 
00167 /*
00168   Local Variables:
00169   mode:c++
00170   c-basic-offset:4
00171   c-file-style:"stroustrup"
00172   c-file-offsets:((innamespace . 0)(inline-open . 0))
00173   indent-tabs-mode:nil
00174   fill-column:79
00175   End:
00176 */
00177 // 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