PLearn 0.1
CostModule.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // CostModule.h
00004 //
00005 // Copyright (C) 2006 Pascal Lamblin
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 Lamblin
00036 
00040 #ifndef CostModule_INC
00041 #define CostModule_INC
00042 
00043 #include <plearn_learners/online/OnlineLearningModule.h>
00044 
00045 namespace PLearn {
00046 
00053 class CostModule : public OnlineLearningModule
00054 {
00055     typedef OnlineLearningModule inherited;
00056 
00057 public:
00058     //#####  Public Build Options  ############################################
00059 
00061     int target_size;
00062 
00063 public:
00064     //#####  Public Member Functions  #########################################
00065 
00067     CostModule();
00068 
00069     // Your other public member functions go here
00070 
00072     virtual void fprop(const Vec& input, const Vec& target, real& cost ) const;
00073 
00075     virtual void fprop(const Mat& inputs, const Mat& targets, Vec& costs );
00076 
00078     virtual void fprop(const Vec& input, const Vec& target, Vec& cost ) const;
00079 
00081     virtual void fprop(const Mat& inputs, const Mat& targets, Mat& costs)
00082         const;
00083 
00085     virtual void fprop(const Vec& input_and_target, Vec& output) const;
00086 
00088     virtual void bpropUpdate(const Vec& input, const Vec& target, real cost,
00089                              Vec& input_gradient, bool accumulate=false);
00090 
00093     virtual void bpropUpdate(const Mat& inputs, const Mat& targets,
00094             const Vec& costs, Mat& input_gradients, bool accumulate = false)
00095     {
00096         PLERROR("bpropUpdate on mini-batches not implemented in class %s",
00097                 classname().c_str());
00098     }
00099 
00101     virtual void bpropUpdate(const Vec& input, const Vec& target, real cost );
00102 
00103     virtual void bpropUpdate(const Mat& inputs, const Mat& targets,
00104             const Vec& costs);
00105 
00107     virtual void bpropUpdate(const Vec& input_and_target, const Vec& output,
00108                              Vec& input_and_target_gradient,
00109                              const Vec& output_gradient,
00110                              bool accumulate=false);
00111 
00112 
00113     // TODO Had to override to compile, this is weird.
00114     virtual void bpropUpdate(const Mat& input, const Mat& output,
00115                              Mat& input_gradient, const Mat& output_gradient,
00116                              bool accumulate=false)
00117     {
00118         inherited::bpropUpdate(input, output, input_gradient, output_gradient,
00119                 accumulate);
00120     }
00121 
00124     virtual void bbpropUpdate(const Vec& input, const Vec& target, real cost,
00125                               Vec& input_gradient, Vec& input_diag_hessian,
00126                               bool accumulate=false);
00127 
00129     virtual void bbpropUpdate(const Vec& input, const Vec& target, real cost );
00130 
00132     virtual void bbpropUpdate(const Vec& input_and_target, const Vec& output,
00133                               Vec& input_and_target_gradient,
00134                               const Vec& output_gradient,
00135                               Vec& input_and_target_diag_hessian,
00136                               const Vec& output_diag_hessian,
00137                               bool accumulate=false);
00138     virtual void forget();
00139 
00141     virtual TVec<string> costNames();
00142 
00145     virtual const TVec<string>& getPorts();
00146 
00149     virtual const TMat<int>& getPortSizes();
00150 
00152     virtual void fprop(const TVec<Mat*>& ports_value);
00153 
00155     virtual void bpropAccUpdate(const TVec<Mat*>& ports_value,
00156                                 const TVec<Mat*>& ports_gradient);
00157 
00158     //#####  PLearn::Object Protocol  #########################################
00159 
00160     // Declares other standard object methods.
00161     PLEARN_DECLARE_OBJECT(CostModule);
00162 
00163     // Simply calls inherited::build() then build_()
00164     virtual void build();
00165 
00167     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00168 
00169 
00170 protected:
00171     //#####  Protected Member Functions  ######################################
00172 
00174     static void declareOptions(OptionList& ol);
00175 
00176 protected:
00177     mutable Vec tmp_costs;
00178     mutable Vec tmp_input_and_target;
00179     mutable Vec tmp_input_and_target_gradient;
00180     mutable Vec tmp_input_and_target_diag_hessian;
00181     Mat tmp_costs_mat;
00182     Mat tmp_input_gradients;
00183     Vec store_costs; 
00184 
00185 private:
00186     //#####  Private Member Functions  ########################################
00187 
00189     void build_();
00190 
00191 private:
00192     //#####  Private Data Members  ############################################
00193 
00194     // The rest of the private stuff goes here
00195 };
00196 
00197 // Declares a few other classes and functions related to this class
00198 DECLARE_OBJECT_PTR(CostModule);
00199 
00200 } // end of namespace PLearn
00201 
00202 #endif
00203 
00204 
00205 /*
00206   Local Variables:
00207   mode:c++
00208   c-basic-offset:4
00209   c-file-style:"stroustrup"
00210   c-file-offsets:((innamespace . 0)(inline-open . 0))
00211   indent-tabs-mode:nil
00212   fill-column:79
00213   End:
00214 */
00215 // 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