Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Algorithms
Trainers
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
* <https://shark-ml.github.io/Shark/>
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
41
#include <
shark/Models/LinearModel.h
>
42
#include <
shark/Algorithms/Trainers/AbstractTrainer.h
>
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
/// \ingroup unsupervised_trainer
55
class
NormalizeComponentsZCA
:
public
AbstractUnsupervisedTrainer
<LinearModel<RealVector> >
56
{
57
typedef
AbstractUnsupervisedTrainer<LinearModel<RealVector>
>
base_type
;
58
public
:
59
60
double
m_targetVariance
;
61
NormalizeComponentsZCA
(
double
targetVariance = 1.0){
62
SHARK_RUNTIME_CHECK
(targetVariance > 0.0,
"Target variance must be positive"
);
63
m_targetVariance
= targetVariance;
64
}
65
66
/// \brief From INameable: return the class name.
67
std::string
name
()
const
68
{
return
"NormalizeComponentsZCA"
; }
69
70
void
train
(
ModelType
& model,
UnlabeledData<RealVector>
const
& input){
71
std::size_t dc =
dataDimension
(input);
72
SHARK_RUNTIME_CHECK
(input.
numberOfElements
() >= dc + 1,
"Input needs to contain more points than there are input dimensions"
);
73
74
// dense model with bias having input and output dimension equal to data dimension
75
model.
setStructure
(dc, dc,
true
);
76
77
RealVector
mean
;
78
RealMatrix
covariance
;
79
meanvar
(input,
mean
,
covariance
);
80
81
blas::symm_eigenvalue_decomposition<RealMatrix> eigen(
covariance
);
82
covariance
=RealMatrix();
//covariance not needed anymore
83
84
85
RealMatrix ZCAMatrix = eigen.Q() % to_diagonal(elem_inv(sqrt(eigen.D()))) % trans(eigen.Q());
86
ZCAMatrix *= std::sqrt(
m_targetVariance
);
87
88
RealVector offset = -prod(ZCAMatrix,
mean
);
89
90
model.
setStructure
(ZCAMatrix, offset);
91
}
92
};
93
94
95
}
96
#endif