ModifiedKernelMatrix.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Modified Kernel Gram matrix
6  *
7  *
8  * \par
9  *
10  *
11  *
12  * \author T. Glasmachers
13  * \date 2007-2012
14  *
15  *
16  * \par Copyright 1995-2017 Shark Development Team
17  *
18  * <BR><HR>
19  * This file is part of Shark.
20  * <http://shark-ml.org/>
21  *
22  * Shark is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU Lesser General Public License as published
24  * by the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * Shark is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  *
32  * You should have received a copy of the GNU Lesser General Public License
33  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
34  *
35  */
36 //===========================================================================
37 
38 
39 #ifndef SHARK_LINALG_MODIFIEDKERNELMATRIX_H
40 #define SHARK_LINALG_MODIFIEDKERNELMATRIX_H
41 
42 #include <shark/Data/Dataset.h>
43 #include <shark/LinAlg/Base.h>
44 
45 #include <vector>
46 #include <cmath>
47 
48 
49 namespace shark {
50 
51 ///
52 /// \brief Modified Kernel Gram matrix
53 ///
54 /// \par
55 /// The ModifiedKernelMatrix represents the kernel matrix
56 /// multiplied element-wise with a factor depending on the
57 /// labels of the training examples. This is useful for the
58 /// MCMMR method (multi-class maximum margin regression).
59 template <class InputType, class CacheType>
61 {
62 private:
64 public:
65  typedef typename Matrix::QpFloatType QpFloatType;
66 
67  /// Constructor
68  /// \param kernelfunction kernel function
69  /// \param data data to evaluate the kernel function
70  /// \param modifierEq multiplier for same-class labels
71  /// \param modifierNe multiplier for different-class kernels
73  AbstractKernelFunction<InputType> const& kernelfunction,
75  QpFloatType modifierEq,
76  QpFloatType modifierNe
77  ): m_matrix(kernelfunction,data.inputs())
78  , m_labels(data.numberOfElements())
79  , m_modifierEq(modifierEq)
80  , m_modifierNe(modifierNe){
81  for(std::size_t i = 0; i != m_labels.size(); ++i){
82  m_labels[i] = data.element(i).label;
83  }
84  }
85 
86  /// return a single matrix entry
87  QpFloatType operator () (std::size_t i, std::size_t j) const
88  { return entry(i, j); }
89 
90  /// return a single matrix entry
91  QpFloatType entry(std::size_t i, std::size_t j) const
92  {
93  QpFloatType ret = m_matrix(i,j);
94  QpFloatType modifier = m_labels[i] == m_labels[j] ? m_modifierEq : m_modifierNe;
95  return modifier*ret;
96  }
97 
98  /// \brief Computes the i-th row of the kernel matrix.
99  ///
100  ///The entries start,...,end of the i-th row are computed and stored in storage.
101  ///There must be enough room for this operation preallocated.
102  void row(std::size_t i, std::size_t start,std::size_t end, QpFloatType* storage) const{
103  m_matrix.row(i,start,end,storage);
104  //apply modifiers
105  unsigned int labeli = m_labels[i];
106  for(std::size_t j = start; j < end; j++){
107  QpFloatType modifier = (labeli == m_labels[j]) ? m_modifierEq : m_modifierNe;
108  storage[j-start] *= modifier;
109  }
110  }
111 
112  /// \brief Computes the kernel-matrix
113  template<class M>
114  void matrix(
115  blas::matrix_expression<M, blas::cpu_tag> & storage
116  ) const{
117  m_matrix.matrix(storage);
118  for(std::size_t i = 0; i != size(); ++i){
119  unsigned int labeli = m_labels[i];
120  for(std::size_t j = 0; j != size(); ++j){
121  QpFloatType modifier = (labeli == m_labels[j]) ? m_modifierEq : m_modifierNe;
122  storage()(i,j) *= modifier;
123  }
124  }
125  }
126 
127  /// swap two variables
128  void flipColumnsAndRows(std::size_t i, std::size_t j){
130  std::swap(m_labels[i],m_labels[j]);
131  }
132 
133  /// return the size of the quadratic matrix
134  std::size_t size() const
135  { return m_matrix.size(); }
136 
137  /// query the kernel access counter
138  unsigned long long getAccessCount() const
139  { return m_matrix.getAccessCount(); }
140 
141  /// reset the kernel access counter
144 
145 protected:
146  /// Kernel matrix which computes the basic entries.
147  Matrix m_matrix;
148  std::vector<unsigned int> m_labels;
149 
150  /// modifier in case the labels are equal
151  QpFloatType m_modifierEq;
152 
153  /// modifier in case the labels differ
154  QpFloatType m_modifierNe;
155 };
156 
157 
158 }
159 #endif