PLearn 0.1
BasisSelectionRegressor.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // BasisSelectionRegressor.h
00004 //
00005 // Copyright (C) 2006 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 #ifndef BasisSelectionRegressor_INC
00041 #define BasisSelectionRegressor_INC
00042 
00043 #include <plearn_learners/generic/PLearner.h>
00044 #include <plearn/math/RealFunction.h>
00045 #include <plearn/ker/Kernel.h>
00046 
00047 namespace PLearn {
00048 
00060 class BasisSelectionRegressor : public PLearner
00061 {
00062     typedef PLearner inherited;
00063 
00064 public:
00065     //#####  Public Build Options  ############################################
00066     bool consider_constant_function;
00067     TVec<RealFunc> explicit_functions;
00068     TVec<RealFunc> explicit_interaction_functions;
00069     TVec<string> explicit_interaction_variables;
00070     TVec<RealFunc> mandatory_functions;
00071     bool consider_raw_inputs;
00072     bool consider_normalized_inputs;
00073     bool consider_input_range_indicators;
00074     bool fixed_min_range;
00075     real indicator_desired_prob;
00076     real indicator_min_prob;
00077     TVec<Ker> kernels;
00078     mutable Mat kernel_centers;
00079     int n_kernel_centers_to_pick;
00080     bool consider_interaction_terms;
00081     int max_interaction_terms;
00082     int consider_n_best_for_interaction;
00083     int interaction_max_order;
00084     bool consider_sorted_encodings;
00085     int max_n_vals_for_sorted_encodings;
00086     bool normalize_features;
00087     PP<PLearner> learner;
00088     bool precompute_features;
00089     int n_threads;
00090     int thread_subtrain_length;
00091     bool use_all_basis;
00092 
00093     //#####  Public Learnt Options  ############################################
00094     TVec<RealFunc> selected_functions;
00095     Vec alphas;
00096     mutable Mat scores;
00097 
00098 
00099     struct thread_wawr;
00100 
00101 public:
00102     //#####  Public Member Functions  #########################################
00103 
00105     // ### Make sure the implementation in the .cc
00106     // ### initializes all fields to reasonable default values.
00107     BasisSelectionRegressor();
00108 
00109     void printModelFunction(PStream& out) const;
00110 
00111     //#####  PLearner Member Functions  #######################################
00112 
00115     virtual int outputsize() const;
00116 
00120     virtual void forget();
00121 
00125     virtual void train();
00126 
00128     virtual void computeOutput(const Vec& input, Vec& output) const;
00129 
00131     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output,
00132                                          const Vec& target, Vec& costs) const;
00133 
00136     virtual TVec<std::string> getTestCostNames() const;
00137 
00138 
00140     virtual void setTrainStatsCollector(PP<VecStatsCollector> statscol);
00141 
00144     virtual TVec<std::string> getTrainCostNames() const;
00145 
00147     virtual void setTrainingSet(VMat training_set, bool call_forget=true);
00148 
00149 
00150     //#####  PLearn::Object Protocol  #########################################
00151 
00152     // Declares other standard object methods.
00153     // ### If your class is not instantiatable (it has pure virtual methods)
00154     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS
00155     PLEARN_DECLARE_OBJECT(BasisSelectionRegressor);
00156 
00157     // Simply calls inherited::build() then build_()
00158     virtual void build();
00159 
00161     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00162 
00163     virtual void setExperimentDirectory(const PPath& the_expdir);
00164 
00165 protected:
00166     //#####  Protected Member Functions  ######################################
00167 
00169     static void declareOptions(OptionList& ol);
00170 
00171 private:
00172     //#####  Private Member Functions  ########################################
00173 
00175     void build_();
00176 
00177     void appendCandidateFunctionsOfSingleField(int fieldnum, TVec<RealFunc>& functions) const;
00178     void appendKernelFunctions(TVec<RealFunc>& functions) const;
00179     void appendConstantFunction(TVec<RealFunc>& functions) const;
00180 
00182     void buildSimpleCandidateFunctions();
00183 
00188     void buildAllCandidateFunctions();
00189 
00191     TVec<RealFunc> buildTopCandidateFunctions();
00192 
00194     void findBestCandidateFunction(int& best_candidate_index, real& best_score) const;
00195 
00197     void addInteractionFunction(RealFunc& f1, RealFunc& f2, TVec<RealFunc>& all_functions);
00198 
00200     void computeOrder(RealFunc& func, int& order);
00201 
00202     void computeWeightedAveragesWithResidue(const TVec<RealFunc>& functions,   
00203                                             real& wsum,
00204                                             Vec& E_x, Vec& E_xx,
00205                                             real& E_y, real& E_yy,
00206                                             Vec& E_xy) const;
00207 
00208     /*
00209     void computeWeightedCorrelationsWithY(const TVec<RealFunc>& functions, const Vec& Y,  
00210                                           real& wsum,
00211                                           Vec& E_x, Vec& V_x,
00212                                           real& E_y, real& V_y,
00213                                           Vec& E_xy, Vec& V_xy,
00214                                           Vec& covar, Vec& correl,
00215                                           real min_variance = 1e-6) const;
00216     */
00217     void appendFunctionToSelection(int candidate_index);
00218     void retrainLearner();
00219     void initTargetsResidueWeight();
00220     void recomputeFeatures();
00221     void recomputeResidue();
00222     void computeOutputFromFeaturevec(const Vec& featurevec, Vec& output) const;
00223 
00224 protected:
00225     //#####  Protected Data Members  ##########################################
00226 
00227     // Template learner.  Each train step is done with a copy of this one.
00228     PP<PLearner> template_learner;
00229 
00230 private:
00231     //#####  Private Data Members  ############################################
00232 
00233     TVec<RealFunc> simple_candidate_functions;
00234     TVec<RealFunc> candidate_functions;
00235     Mat features;
00236     Vec residue;
00237     Vec targets;
00238     Vec weights;
00239     double residue_sum;
00240     double residue_sum_sq;
00241 
00242     mutable Vec input;
00243     mutable Vec targ;
00244     mutable Vec featurevec;
00245 };
00246 
00247 // Declares a few other classes and functions related to this class
00248 DECLARE_OBJECT_PTR(BasisSelectionRegressor);
00249 
00250 } // end of namespace PLearn
00251 
00252 #endif
00253 
00254 
00255 /*
00256   Local Variables:
00257   mode:c++
00258   c-basic-offset:4
00259   c-file-style:"stroustrup"
00260   c-file-offsets:((innamespace . 0)(inline-open . 0))
00261   indent-tabs-mode:nil
00262   fill-column:79
00263   End:
00264 */
00265 // 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