PLearn 0.1
SelectInputSubsetLearner.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // SelectInputSubsetLearner.cc
00004 //
00005 // Copyright (C) 2004 Yoshua Bengio 
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 /* *******************************************************      
00036  * $Id: SelectInputSubsetLearner.cc 3994 2005-08-25 13:35:03Z chapados $
00037  ******************************************************* */
00038 
00039 // Authors: Yoshua Bengio
00040 
00043 #include "SelectInputSubsetLearner.h"
00044 #include <plearn/vmat/SelectColumnsVMatrix.h>
00045 #include <plearn/math/random.h>
00046 
00047 namespace PLearn {
00048 using namespace std;
00049 
00050 SelectInputSubsetLearner::SelectInputSubsetLearner() : random_fraction(0)
00051 /* ### Initialize all fields to their default value here */
00052 {
00053 }
00054 
00055 PLEARN_IMPLEMENT_OBJECT(SelectInputSubsetLearner, "PLearner which selects a subset of the inputs for an embedded learner.", 
00056                         "This learner class contains an embedded learner for which it selects a subset of the inputs.\n"
00057                         "The subset can be either selected explicitly or chosen randomly (the user chooses what fraction\n"
00058                         "of the original inputs will be selected).");
00059 
00060 void SelectInputSubsetLearner::declareOptions(OptionList& ol)
00061 {
00062     // ### Declare all of this object's options here
00063     // ### For the "flags" of each option, you should typically specify  
00064     // ### one of OptionBase::buildoption, OptionBase::learntoption or 
00065     // ### OptionBase::tuningoption. Another possible flag to be combined with
00066     // ### is OptionBase::nosave
00067 
00068     declareOption(ol, "selected_inputs", &SelectInputSubsetLearner::selected_inputs, OptionBase::buildoption,
00069                   "List of selected inputs. If this option is set then random_fraction should not be set (or set to 0).\n");
00070 
00071     declareOption(ol, "random_fraction", &SelectInputSubsetLearner::random_fraction, OptionBase::buildoption,
00072                   "Fraction of the original inputs that is randomly selected.\n"
00073                   "If 0 then the selected_inputs option should be set.\n"
00074                   "If selected_inputs is provided (length>0) then this option is ignored.\n");
00075 
00076     // Now call the parent class' declareOptions
00077     inherited::declareOptions(ol);
00078 }
00079 
00080 void SelectInputSubsetLearner::build_()
00081 {
00082     if (random_fraction>0 && learner_ && inputsize_>0 && selected_inputs.length()==0)
00083     {
00084         int n_selected = int(rint(random_fraction*inputsize_));
00085         selected_inputs.resize(inputsize_);
00086         for (int i=0;i<n_selected;i++) 
00087             selected_inputs[i]=i;
00088         shuffleElements(selected_inputs);
00089         selected_inputs.resize(n_selected);
00090     }
00091     learner_inputs.resize(selected_inputs.length());
00092 }
00093 
00094 // ### Nothing to add here, simply calls build_
00095 void SelectInputSubsetLearner::build()
00096 {
00097     inherited::build();
00098     build_();
00099 }
00100 
00101 
00102 void SelectInputSubsetLearner::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00103 {
00104     inherited::makeDeepCopyFromShallowCopy(copies);
00105 
00106     // ### Call deepCopyField on all "pointer-like" fields 
00107     // ### that you wish to be deepCopied rather than 
00108     // ### shallow-copied.
00109     // ### ex:
00110     deepCopyField(selected_inputs, copies);
00111     deepCopyField(all_indices, copies);
00112     deepCopyField(learner_inputs, copies);
00113 }
00114 
00115 int SelectInputSubsetLearner::inputsize() const
00116 { return inputsize_; }
00117 
00118 
00119 void SelectInputSubsetLearner::computeOutput(const Vec& input, Vec& output) const
00120 {
00121     for (int i=0;i<learner_inputs.length();i++)
00122         learner_inputs[i] = input[selected_inputs[i]];
00123     learner_->computeOutput(learner_inputs,output);
00124 }    
00125 
00126 void SelectInputSubsetLearner::computeCostsFromOutputs(const Vec& input, const Vec& output, 
00127                                                        const Vec& target, Vec& costs) const
00128 {
00129     // Compute the costs from *already* computed output. 
00130     for (int i=0;i<learner_inputs.length();i++)
00131         learner_inputs[i] = input[selected_inputs[i]];
00132     learner_->computeCostsFromOutputs(learner_inputs,output,target,costs);
00133 }                                
00134 
00135 void SelectInputSubsetLearner::computeOutputAndCosts(const Vec& input, const Vec& target,
00136                                                      Vec& output, Vec& costs) const
00137 { 
00138     for (int i=0;i<learner_inputs.length();i++)
00139         learner_inputs[i] = input[selected_inputs[i]];
00140     learner_->computeOutputAndCosts(learner_inputs, target, output, costs); 
00141 }
00142 
00143 void SelectInputSubsetLearner::setTrainingSet(VMat training_set, bool call_forget)
00144 {
00145     inherited::setTrainingSet(training_set,call_forget);
00146     int n_other_columns = training_set->width()-inputsize();
00147     all_indices.resize(selected_inputs.length()+n_other_columns);
00148     for (int i=0;i<selected_inputs.length();i++)
00149         all_indices[i]=selected_inputs[i];
00150     for (int j=0;j<n_other_columns;j++)
00151         all_indices[selected_inputs.length()+j]=inputsize()+j;
00152     VMat vm = new SelectColumnsVMatrix(training_set,all_indices);
00153     vm->defineSizes(selected_inputs.length(),training_set->targetsize(),training_set->weightsize());
00154     learner_->setTrainingSet(vm,call_forget);
00155 }
00156 
00157 } // end of namespace PLearn
00158 
00159 
00160 /*
00161   Local Variables:
00162   mode:c++
00163   c-basic-offset:4
00164   c-file-style:"stroustrup"
00165   c-file-offsets:((innamespace . 0)(inline-open . 0))
00166   indent-tabs-mode:nil
00167   fill-column:79
00168   End:
00169 */
00170 // 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