NormalizeComponentsZCA.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Data normalization to zero mean, unit variance and zero covariance while keping the original coordinate system
6  *
7  *
8  *
9  *
10  * \author T. Glasmachers
11  * \date 2010
12  *
13  *
14  * \par Copyright 1995-2017 Shark Development Team
15  *
16  * <BR><HR>
17  * This file is part of Shark.
18  * <http://shark-ml.org/>
19  *
20  * Shark is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU Lesser General Public License as published
22  * by the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * Shark is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public License
31  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
32  *
33  */
34 //===========================================================================
35 
36 
37 #ifndef SHARK_ALGORITHMS_TRAINERS_NORMALIZECOMPONENTSZCA_H
38 #define SHARK_ALGORITHMS_TRAINERS_NORMALIZECOMPONENTSZCA_H
39 
40 
43 #include <shark/Data/Statistics.h>
44 
45 namespace shark {
46 
47 
48 /// \brief Train a linear model to whiten the data
49 ///
50 /// ZCA does whitening in the sense that it sets the mean to zero and the covariance to the Identity.
51 /// However in contrast to NormalizeComponentsWhitening it makes sure that the initial and end coordinate
52 /// system are the same and just rescales the data. The effect is, that image data still resembles images
53 /// after applying ZCA in contrast to other methods which rotate the data randomly.
54 class NormalizeComponentsZCA : public AbstractUnsupervisedTrainer<LinearModel<RealVector> >
55 {
57 public:
58 
60  NormalizeComponentsZCA(double targetVariance = 1.0){
61  SHARK_RUNTIME_CHECK(targetVariance > 0.0, "Target variance must be positive");
62  m_targetVariance = targetVariance;
63  }
64 
65  /// \brief From INameable: return the class name.
66  std::string name() const
67  { return "NormalizeComponentsZCA"; }
68 
69  void train(ModelType& model, UnlabeledData<RealVector> const& input){
70  std::size_t dc = dataDimension(input);
71  SHARK_RUNTIME_CHECK(input.numberOfElements() >= dc + 1, "Input needs to contain more points than there are input dimensions");
72 
73  // dense model with bias having input and output dimension equal to data dimension
74  model.setStructure(dc, dc, true);
75 
76  RealVector mean;
77  RealMatrix covariance;
78  meanvar(input, mean, covariance);
79 
80  blas::symm_eigenvalue_decomposition<RealMatrix> eigen(covariance);
81  covariance=RealMatrix(); //covariance not needed anymore
82 
83 
84  RealMatrix ZCAMatrix = eigen.Q() % to_diagonal(elem_inv(sqrt(eigen.D()))) % trans(eigen.Q());
85  ZCAMatrix *= std::sqrt(m_targetVariance);
86 
87  RealVector offset = -prod(ZCAMatrix,mean);
88 
89  model.setStructure(ZCAMatrix, offset);
90  }
91 };
92 
93 
94 }
95 #endif