PLearn 0.1
GaussMixLocalProjections.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // GaussMixLocalProjections.cc
00004 //
00005 // Copyright (C) 2005 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 /* *******************************************************      
00036  * $Id: GaussMixLocalProjections.cc 4536 2005-11-15 16:25:41Z tihocan $ 
00037  ******************************************************* */
00038 
00039 // Authors: Olivier Delalleau
00040 
00044 #include "GaussMixLocalProjections.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00050 // GaussMixLocalProjections //
00052 GaussMixLocalProjections::GaussMixLocalProjections() 
00053     : n_components(-1)
00054 {
00055 }
00056 
00057 PLEARN_IMPLEMENT_OBJECT(GaussMixLocalProjections,
00058                         "Train a Gaussian mixture and output the corresponding local projections.",
00059                         "This learner just takes a mixture of Gaussians and gives in output a vector\n"
00060                         "which is the concatenation of the local coordinates computed for each Gaussian\n"
00061                         "in the mixture and weighted by the posterior P(k|x).\n"
00062                         "At the beginning of each of those coordinates a bias (1) is added, then the\n"
00063                         "coordinate is multiplied by the 'responsibility' of the Gaussian, i.e. its\n"
00064                         "posterior P(k|x).\n"
00065                         "The local coordinates are obtained by projecting on the eigenvectors associated\n"
00066                         "to non-zero eigenvalues in the covariance matrix.\n"
00067     );
00068 
00070 // declareOptions //
00072 void GaussMixLocalProjections::declareOptions(OptionList& ol)
00073 {
00074     // ### For the "flags" of each option, you should typically specify  
00075     // ### one of OptionBase::buildoption, OptionBase::learntoption or 
00076     // ### OptionBase::tuningoption. Another possible flag to be combined with
00077     // ### is OptionBase::nosave
00078 
00079     // Build options.
00080 
00081     // declareOption(ol, "myoption", &GaussMixLocalProjections::myoption, OptionBase::buildoption,
00082     //               "Help text describing this option");
00083     // ...
00084 
00085     // Learnt options.
00086 
00087     declareOption(ol, "n_components", &GaussMixLocalProjections::n_components, OptionBase::learntoption,
00088                   "Equal to learner->L, i.e. the number of components in the mixture.");
00089 
00090     declareOption(ol, "outputsizes", &GaussMixLocalProjections::outputsizes, OptionBase::learntoption,
00091                   "The size of the projection for each Gaussian, i.e. learner->n_eigen.");
00092 
00093     // Now call the parent class' declareOptions.
00094     inherited::declareOptions(ol);
00095 
00096     redeclareOption(ol, "seed", &GaussMixLocalProjections::seed_, OptionBase::nosave,
00097                     "No need for a seed.");
00098 
00099     redeclareOption(ol, "nstages", &GaussMixLocalProjections::nstages, OptionBase::nosave,
00100                     "One only needs to specify mixture->nstages.");
00101 
00102 }
00103 
00105 // build //
00107 void GaussMixLocalProjections::build()
00108 {
00109     inherited::build();
00110     build_();
00111 }
00112 
00114 // build_ //
00116 void GaussMixLocalProjections::build_()
00117 {
00118     // ### This method should do the real building of the object,
00119     // ### according to set 'options', in *any* situation. 
00120     // ### Typical situations include:
00121     // ###  - Initial building of an object from a few user-specified options
00122     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00123     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00124     // ### You should assume that the parent class' build_() has already been called.
00125     if (learner_) {
00126         if (learner_->classname() != "GaussMix")
00127             PLERROR("In GaussMixLocalProjections::build_ - A GaussMix learner is needed");
00128         gauss_mix = (GaussMix*) (PLearner*) learner_;
00129         if (gauss_mix->type != "general")
00130             PLERROR("In GaussMixLocalProjections::build_ - The underlying GaussMix "
00131                     "distribution must be of type 'general'");
00132         n_components = gauss_mix->L;
00133         outputsizes.resize(n_components);
00134         outputsizes.fill(gauss_mix->n_eigen >= 0 ? gauss_mix->n_eigen : inputsize());
00135     }
00136 }
00137 
00139 // computeCostsFromOutputs //
00141 void GaussMixLocalProjections::computeCostsFromOutputs(const Vec& input, const Vec& output, 
00142                                                        const Vec& target, Vec& costs) const
00143 {
00144     // No cost computed.
00145 }                                
00146 
00148 // computeOutput //
00150 void GaussMixLocalProjections::computeOutput(const Vec& input, Vec& output) const
00151 {
00152     Mat eigen_vec;
00153     int size_k = gauss_mix->n_eigen > 0 ? gauss_mix->n_eigen : input.length();
00154     output.resize((size_k + 1) * gauss_mix->L);
00155     int index = 0;
00156     // Compute densities in order to be able to get posteriors.
00157     real log_density = gauss_mix->log_density(input);
00158     Vec log_likelihood_dens;
00159     // log_likelihood_dens = gauss_mix->getLogLikelihoodDens();
00160     PLERROR("In GaussMixLocalProjections::computeOutput - The line above must "
00161             "be uncommented (will not work with the modified GaussMix)");
00162     for (int k = 0; k < gauss_mix->L; k++) {
00163         // Obtain the (right number of) eigenvectors.
00164         output[index] = 1.0;
00165         // eigen_vec = gauss_mix->getEigenvectors(k).subMatRows(0, size_k);
00166         PLERROR("In GaussMixLocalProjections::computeOutput - The line above "
00167                 "must be uncommented (will not work with the modified "
00168                 "GaussMix)");
00169         // Compute local coordinates.
00170         product(output.subVec(index+1, size_k), eigen_vec, input);
00171         // Scale by the responsibility.
00172         output.subVec(index, size_k + 1) *= exp(log_likelihood_dens[k] - log_density);
00173         index += size_k + 1;
00174     }
00175 }    
00176 
00178 // getTestCostNames //
00180 TVec<string> GaussMixLocalProjections::getTestCostNames() const
00181 {
00182     static TVec<string> noCost;
00183     return noCost;
00184 }
00185 
00187 // getTrainCostNames //
00189 TVec<string> GaussMixLocalProjections::getTrainCostNames() const
00190 {
00191     static TVec<string> noCost;
00192     return noCost;
00193 }
00194 
00196 // makeDeepCopyFromShallowCopy //
00198 void GaussMixLocalProjections::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00199 {
00200     inherited::makeDeepCopyFromShallowCopy(copies);
00201 
00202     // ### ex:
00203     // deepCopyField(trainvec, copies);
00204 
00205     // ### Remove this line when you have fully implemented this method.
00206     PLERROR("GaussMixLocalProjections::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00207 }
00208 
00210 // outputsize //
00212 int GaussMixLocalProjections::outputsize() const
00213 {
00214     return sum(outputsizes) + n_components;
00215 }
00216 
00217 } // end of namespace PLearn
00218 
00219 
00220 /*
00221   Local Variables:
00222   mode:c++
00223   c-basic-offset:4
00224   c-file-style:"stroustrup"
00225   c-file-offsets:((innamespace . 0)(inline-open . 0))
00226   indent-tabs-mode:nil
00227   fill-column:79
00228   End:
00229 */
00230 // 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