PLearn 0.1
VMatKernel.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // VMatKernel.cc
00004 //
00005 // Copyright (C) 2005 Benoit Cromp
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: VMatKernel.cc 8130 2007-10-02 20:59:36Z louradou $ 
00037  ******************************************************* */
00038 
00039 // Authors: Benoit Cromp, Jerome Louradour
00040 
00044 #include "VMatKernel.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00050 // VMatKernel //
00052 // ### Initialize all fields to their default value here
00053 VMatKernel::VMatKernel() 
00054 {
00055     // ...
00056 
00057     // ### You may or may not want to call build_() to finish building the object
00058     // build_();
00059 }
00060 
00061 PLEARN_IMPLEMENT_OBJECT(VMatKernel,
00062                         "Kernel that is given its Gram matrix.",
00063                         "This kernel can only be applied on examples that are integers, and that\n"
00064                         "correspond to indices in the matrix.\n"
00065     );
00066 
00068 // declareOptions //
00070 void VMatKernel::declareOptions(OptionList& ol)
00071 {
00072     declareOption(ol,"source",&VMatKernel::source,
00073                   OptionBase::buildoption,
00074         "Gram matrix");
00075 
00076     declareOption(ol,"train_indices",&VMatKernel::train_indices,
00077                   OptionBase::learntoption,
00078         "List of (real)indices corresponding to training samples");
00079 
00080     // Now call the parent class' declareOptions
00081     inherited::declareOptions(ol);
00082 }
00083 
00085 // build //
00087 void VMatKernel::build()
00088 {
00089     // ### Nothing to add here, simply calls build_
00090     inherited::build();
00091     build_();
00092 }
00093 
00095 // build_ //
00097 void VMatKernel::build_()
00098 {
00099     PLASSERT( !(source) || ( source->length() == source->width() ) );
00100     if ( !specify_dataset )
00101         train_indices.resize(0);
00102 }
00103 
00105 // evaluate //
00107 real VMatKernel::evaluate(const Vec& x1, const Vec& x2) const
00108 {
00109     PLASSERT( x1.size()==1 && x2.size()==1 );
00110     return evaluate( x1[0], x2[0] );
00111 }
00112 
00113 real VMatKernel::evaluate(real x1, real x2) const
00114 {
00115     PLASSERT( fabs(x1-(real)((int)x1)) < 0.1 );
00116     PLASSERT( fabs(x2-(real)((int)x2)) < 0.1 );
00117     return evaluate( int(x1), int(x2) );
00118 }
00119 
00120 real VMatKernel::evaluate(int x1, int x2) const
00121 {
00122     PLASSERT( source );
00123     PLASSERT( x1 >= 0 );
00124     PLASSERT( x1 < source->length() );
00125     PLASSERT( x2 >= 0 );
00126     PLASSERT( x2 < source->width() );
00127     return source->get( x1, x2);
00128 }
00129 
00131 // evaluate_i_j //
00133 real VMatKernel::evaluate_i_j(int i, int j) const
00134 {
00135     PLASSERT( source );
00136     if( train_indices.length() == 0 )
00137         return evaluate( i, j );
00138     PLASSERT( i >= 0 );
00139     PLASSERT( i < n_examples );
00140     PLASSERT( j >= 0 );
00141     PLASSERT( j < n_examples );
00142     return evaluate( train_indices[i], train_indices[j] );
00143 }
00144 
00146 // evaluate_i_x //
00148 real VMatKernel::evaluate_i_x(int i, const Vec& x, real squared_norm_of_x) const
00149 {
00150     if( train_indices.length() == 0 )
00151         return evaluate( i, (int)x[0] );
00152     PLASSERT( i >= 0 );
00153     PLASSERT( i < n_examples );
00154     PLASSERT( x.size() == 1 );
00155     return evaluate( train_indices[i], x[0] );
00156 }
00157 
00159 // evaluate_x_i //
00161 real VMatKernel::evaluate_x_i(const Vec& x, int i, real squared_norm_of_x) const
00162 {
00163 //    if( is_symmetric )
00164 //        return evaluate_i_x( i, x, squared_norm_of_x);
00165     if( train_indices.length() == 0 )
00166         return evaluate( (int)x[0], i);
00167     PLASSERT( i >= 0 );
00168     PLASSERT( i < n_examples );
00169     PLASSERT( x.size() == 1 );
00170     return evaluate( x[0], train_indices[i]);
00171 }
00172 
00174 // computeGramMatrix //
00176 void VMatKernel::computeGramMatrix(Mat K) const
00177 {
00178     PLASSERT( source );
00179     if( train_indices.length() > 0 )
00180     {
00181         K.resize(n_examples, n_examples);
00182         if( is_symmetric )
00183             for(int i = 0; i < n_examples; i++ )
00184             {
00185                 K(i,i) = evaluate( train_indices[i], train_indices[i] );
00186                 for(int j = 0; j < i; j++ )
00187                 {
00188                     K(i,j) = evaluate( train_indices[i], train_indices[j] );
00189                     K(j,i) = K(i,j);
00190                 }
00191             }
00192         else
00193             for(int i = 0; i < n_examples; i++ )
00194                 for(int j = 0; j < n_examples; j++ )
00195                     K(i,j) = evaluate( train_indices[i], train_indices[j] );
00196     }
00197     else
00198         K << source->toMat();
00199 }
00200 
00201 
00203 // makeDeepCopyFromShallowCopy //
00205 void VMatKernel::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00206 {
00207     inherited::makeDeepCopyFromShallowCopy(copies);
00208 
00209     deepCopyField(source, copies);
00210     deepCopyField(train_indices, copies);
00211 }
00212 
00214 // setDataForKernelMatrix //
00216 void VMatKernel::setDataForKernelMatrix(VMat the_data)
00217 {
00218     inherited::setDataForKernelMatrix(the_data);
00219 
00220     if( n_examples > 1 )
00221     {
00222         PLASSERT( data_inputsize == 1 );
00223         train_indices.resize(n_examples);
00224         for(int i = 0; i < n_examples; i++)
00225         {
00226             PLASSERT( the_data->get(i,0) >= 0 );
00227             PLASSERT( !(source) || ( the_data->get(i,0) < (real)source->width() ) );
00228             train_indices[i] = the_data->get(i,0);
00229         }
00230     }
00231     else
00232     {
00233         PLASSERT( source );
00234         PLWARNING("in VMatKernel::setDataForKernelMatrix: all values in the VMatKernel source are taken into acount for training");
00235         n_examples = source->width();
00236         train_indices.resize(0);
00237     }
00238 }
00239 
00241 // addDataForKernelMatrix //
00243 void VMatKernel::addDataForKernelMatrix(const Vec& newRow)
00244 {
00245     PLASSERT( newRow.size() == 1 );
00246     inherited::addDataForKernelMatrix( newRow );
00247     if( train_indices.length() == 0 )
00248     {
00249         PLASSERT( source );
00250         n_examples = source->width();
00251         train_indices.resize( n_examples );
00252         for(int i = 0; i < n_examples; i++)
00253             train_indices[i] = (real)i;
00254     }
00255     PLASSERT( newRow[0] > 0 );
00256     PLASSERT( !(source) || ( newRow[0] < source->width() ) );
00257     train_indices.resize( n_examples + 1 );
00258     train_indices[ n_examples ] = newRow[0];
00259     n_examples += 1;
00260 }
00261 
00262 
00263 } // end of namespace PLearn
00264 
00265 
00266 /*
00267   Local Variables:
00268   mode:c++
00269   c-basic-offset:4
00270   c-file-style:"stroustrup"
00271   c-file-offsets:((innamespace . 0)(inline-open . 0))
00272   indent-tabs-mode:nil
00273   fill-column:79
00274   End:
00275 */
00276 // 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