source: branches/0.4-stable/yat/classifier/KernelLookup.h @ 1392

Last change on this file since 1392 was 1392, checked in by Peter, 13 years ago

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 10.3 KB
RevLine 
[680]1#ifndef _theplu_yat_classifier_kernel_lookup_
2#define _theplu_yat_classifier_kernel_lookup_
[330]3
[675]4// $Id$
5
6/*
[831]7  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
[1275]8  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2008 Peter Johansson
[675]11
[1392]12  This file is part of the yat library, http://dev.thep.lu.se/yat
[675]13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 2 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
[680]30#include "Kernel.h"
[1110]31#include "yat/utility/Container2DIterator.h"
[1134]32#include "yat/utility/Index.h"
[1080]33#include "yat/utility/iterator_traits.h"
[1133]34#include "yat/utility/SmartPtr.h"
[1066]35#include "yat/utility/StrideIterator.h"
[675]36
[330]37namespace theplu {
[680]38namespace yat {
[450]39namespace classifier {
[330]40
41  class KernelFunction;
[1133]42  class MatrixLookup;
43  class MatrixLookupWeighted;
[330]44
[517]45  ///
[592]46  /// @brief Lookup into Kernel
[517]47  ///
[592]48  /// This is the KernelLookup class to be used together with kernel
49  /// methods such as Support Vector Machines (SVM). The class does
50  /// not contain any data or values, but rather is a lookup into a
51  /// Kernel object. Each row and each column corresponds to a row and
[1206]52  /// a column in the Kernel, respectively. This design allows for fast
[592]53  /// creation of sub-kernels, which is a common operation in most
54  /// traning/validation procedures.
55  ///
[1206]56  /// A KernelLookup can be created directly from a Kernel or from
57  /// another KernelLookup. In the latter case, the resulting
[592]58  /// KernelLookup is looking directly into the underlying Kernel to
59  /// avoid multiple lookups.
60  ///
61  /// There is a possibility to set the KernelLookup as owner of the
[826]62  /// underlying kernel. This implies that underlying kernel is deleted
63  /// in destructor of MatrixLookup, but only if there is no other
64  /// owner of the underlying kernel. A reference counter is used to
65  /// keep track of number of owners. Ownership is copied in copy
66  /// constructors and assignments.
[592]67  ///
[1132]68  class KernelLookup
[517]69  {
70
71  public:
[1066]72    /// 'Read Only' iterator
[1080]73    typedef utility::StrideIterator<
[1110]74    utility::Container2DIterator<const KernelLookup, const double, void, 
75                                 const double> >
[1066]76    const_iterator;
[720]77
[1125]78    /**
79       'Read only' iterator intended to iterate over a column
80     */
[1105]81    typedef const_iterator const_column_iterator;
[1125]82
83    /**
84       'Read only' iterator intended to iterate over a row
85     */
[1105]86    typedef const_iterator const_row_iterator;
87
[463]88    ///
[592]89    /// @brief Constructor a Lookup into a Kernel
[463]90    ///
[592]91    /// Constructs a KernelLookup corresponding to the Kernel @a
92    /// kernel. By default @a owner is set to false, which means
[826]93    /// KernelLookup does not own the underlying Kernel.
[545]94    ///
[536]95    /// @note If underlying Kernel goes out of scope or is deleted, the
[592]96    /// KernelLookup becomes invalid and the result of further use is
[536]97    /// undefined.
98    ///
[640]99    /// @note Do not construct two KernelLookups from the same @a
100    /// kernel with @a owner set to true because that will cause
101    /// multiple deletion of @a kernel.
102    ///
[592]103    KernelLookup(const Kernel& kernel, const bool owner=false);
[463]104
[446]105    ///
[592]106    /// @brief Constructing a Lookup into a subKernel
[330]107    ///
[592]108    /// Creating a Lookup into parts of the Kernel. In the created
[597]109    /// Lookup the element in the \f$ i \f$ th row in the \f$ j \f$ th
[592]110    /// column is identical to the element in row row[i] and columns
111    /// column[j] in the underlying @a kernel. If @a owner is set to
112    /// true yhe underlying @a kernel is destroyed in the destructor.
113    ///
[536]114    /// @note If @a kernel goes out of scope or is deleted, the
115    /// returned pointer becomes invalid and the result of further use is
116    /// undefined.
117    ///
[1127]118    /// @note For training usage row index shall always be equal to
119    /// column index.
[461]120    ///
[1134]121    KernelLookup(const Kernel& kernel, const utility::Index& row, 
122                 const utility::Index& column, const bool owner=false);
[720]123
[446]124    ///
[592]125    /// @brief Copy constructor.
[537]126    ///
[592]127    /// A Lookup is created looking into the
[826]128    /// same underlying Kernel as @a kl is looking into.
[592]129    ///
[826]130    /// If \a kl is owner of underlying data, constructed
131    /// KernelLookup will also be set as owner of underlying data.
132    ///
[592]133    KernelLookup(const KernelLookup& kl);
[537]134
135
136    ///
[592]137    /// @brief Contructing a sub-KernelLookup.
138    ///
[482]139    /// Contructor building a sub-KernelLookup from a KernelLookup
[1127]140    /// defined by row index vector and column index vector. In the
[597]141    /// created Lookup the element in the \f$ i \f$ th row in the
142    /// \f$ j \f$ th column is identical to the element in row row[i] and
[592]143    /// columns column[j] in the copied @a kl. The resulting
144    /// KernelLookup is independent of the old KernelLookup, but is
145    /// undefined in case underlying Kernel is destroyed.
[482]146    ///
[826]147    /// If \a kl is owner of underlying data, constructed
148    /// KernelLookup will also be set as owner of underlying data.
149    ///
[482]150    /// @note For training usage row index shall always be equal to
151    /// column index.
152    ///
[1134]153    KernelLookup(const KernelLookup& kl, const utility::Index& row, 
154                 const utility::Index& column);
[720]155
[654]156    ///
[1206]157    /// Constructor taking the column (default) or row index as
[654]158    /// input. If @a row is false the created KernelLookup will have
159    /// equally many rows as @a kernel.
160    ///
[826]161    /// If \a kl is owner of underlying data, constructed
162    /// KernelLookup will also be set as owner of underlying data.
163    ///
[592]164    /// @note If underlying kernel goes out of scope or is deleted, the
[559]165    /// KernelLookup becomes invalid and the result of further use is
166    /// undefined.
[330]167    ///
[1206]168    KernelLookup(const KernelLookup& kl, const utility::Index&, 
[559]169                 const bool row=false);
170
171    ///
[537]172    /// @brief Destructor
173    ///
[826]174    /// Deletes underlying Kernel if KernelLookup owns it and there is
175    /// no other owner.
[537]176    ///
177    virtual ~KernelLookup(void);
[330]178
[1066]179    /**
180       Iterator iterates along a row. When end of row is reached it
181       jumps to beginning of next row.
182
183       \return const_iterator pointing to upper-left element.
184     */
185    const_iterator begin(void) const;
186
187    /**
188       Iterator iterates along a column.
189
190       \return iterator pointing to first element of column \a i.
191     */
[1105]192    const_column_iterator begin_column(size_t) const;
[1066]193
194    /**
195       Iterator iterates along a column.
196
197       \return const_iterator pointing to first element of column \a i.
198     */
[1105]199    const_row_iterator begin_row(size_t) const;
[1066]200
[1132]201    /**
202       \return number of columns
203    */
204    size_t columns(void) const;
205
[330]206    ///
[826]207    /// Each column in returned DataLookup corresponds to the column
[720]208    /// in KernelLookup.
[482]209    ///
[826]210    /// \return data that KernelLookup is built upon.
211    ///
[1165]212    /// \throw if KernelLookup is weighted
[482]213    ///
[1206]214    MatrixLookup data(void) const;
[482]215
[1165]216    ///
217    /// Each column in returned DataLookup corresponds to the column
218    /// in KernelLookup.
219    ///
220    /// \return data that KernelLookup is built upon.
221    ///
222    /// \throw if KernelLookup is unweighted
223    ///
[1206]224    MatrixLookupWeighted data_weighted(void) const;
[1165]225
[720]226    /**
227       Function to calculate a new Kernel element using the underlying
[826]228       KernelFunction. The value is calculated between @a vec and the
229       data vector of the \a i th sample, in other words, the
230       sample corresponding to the \a i th row.
[720]231    */
232    double element(const DataLookup1D& vec, size_t i) const;
[482]233
[720]234    /**
235       Function to calculate a new Kernel element using the underlying
236       KernelFunction. The value is calulated between @a vec and the
237       data vector of the \f$ i \f$ th sample, in other words, the
238       sample corresponding to the \f$ i \f$ th row or \f$ i \f$ th
239       column. In case KernelLookup is a sub-Kernel and not symmetric,
240       the kernel value is calculated between @a vec and the data
241       vector corresponding to \f$ i \f$ th row.
242    */
243    double element(const DataLookupWeighted1D& vec, size_t i) const;
[482]244
[658]245    /**
[1066]246       \return const_iterator pointing to end of matrix
247     */
248    const_iterator end(void) const;
249
250    /**
251       \return const_iterator pointing to end of column \a i
252     */
[1105]253    const_column_iterator end_column(size_t) const;
[1066]254
255    /**
256       \return const_iterator pointing to end of row \a i
257     */
[1105]258    const_row_iterator end_row(size_t) const;
[1066]259
260    /**
[1132]261       \return number of rows
262    */
263    size_t rows(void) const;
264
265    /**
[720]266       Each element in returned KernelLookup is calculated using only
267       selected features (defined by @a index). Each element
268       corresponds to the same pair of samples as in the original
269       KernelLookup.
270    */
[1206]271    KernelLookup selected(const utility::Index& index) const;
[720]272   
273    /**
[826]274       This function is useful when predicting on an independent data
[658]275       set using a kernel-based classifier. In returned KernelLookup
276       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
277       data. Row \f$ i \f$ in returned KernelLookup corresponds to
278       same sample as row \f$ i \f$ in @a this. In other words, this
279       function returns a KernelLookup containing the kernel elements
280       between the passed @a data and the internal underlying data @a
281       this was built from.
282    */
[1206]283    KernelLookup test_kernel(const MatrixLookup& data) const;
[607]284
[658]285    /**
[826]286       This function is useful when predicting on an independent data
[658]287       set using a kernel-based classifier. In returned KernelLookup
288       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
289       data. Row \f$ i \f$ in returned KernelLookup corresponds to
290       same sample as row \f$ i \f$ in @a this. In other words, this
291       function returns a KernelLookup containing the kernel elements
292       between the passed @a data and the internal underlying data @a
293       this was built from.
294    */
[1206]295    KernelLookup test_kernel(const MatrixLookupWeighted& data) const;
[658]296
[720]297    /**
298       \return true if underlying Kernel is weighted
299    */
300    bool weighted(void) const;
[545]301
[720]302    /**
303       \return element at position (\a row, \a column) in the Kernel
304       matrix
305    */
306    double operator()(size_t row, size_t column) const;
[545]307
[446]308  private:
[555]309    const KernelLookup& operator=(const KernelLookup&);
[1134]310    bool validate(const utility::Index&) const;
[446]311
[1134]312    utility::Index column_index_;
[1133]313    utility::SmartPtr<const Kernel> kernel_;
[1134]314    utility::Index row_index_;
[555]315   
[720]316  }; // class KernelLookup
[330]317
[680]318}}} // of namespace classifier, yat, and theplu
[330]319
320#endif
Note: See TracBrowser for help on using the repository browser.