GaussianNoiseModel.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements a Model using a linear function.
5  *
6  *
7  *
8  * \author O. Krause
9  * \date 2014
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_MODELS_GAUSSIANNOISEMODEL_H
33 #define SHARK_MODELS_GAUSSIANNOISEMODEL_H
34 
36 #include <shark/Rng/GlobalRng.h>
37 #include <shark/Core/OpenMP.h>
38 namespace shark {
39 
40 /// \brief Model which corrupts the data using gaussian noise
41 ///
42 /// When training autoencoders, it proved beneficial to add noise to the input
43 /// and train the model to remove that noise again, instead of only larning a
44 /// identity transformation. This Model represents one choice of Noise: Gaussian Noise,
45 /// to do this. the formula of corruption of an input \f$ x=(x_1,\dots,x_n) \f$ with variances
46 /// \f$ \sigma = (\sigma_1, \dots, \sigma_n) \f$ is
47 /// \f[ x_i \leftarrow x_i + N(0,\sigma_i) \f]
48 ///
49 /// Usage is simple. given your autoencoder/decoder pair
50 /// ConvatenatedModel<RealVector,RealVector> autoencoder = encoder >> decoder;
51 /// we can just concatenate this model:
52 /// GaussianNoiseModel noise(0.1);//variance of noise
53 /// ConvatenatedModel<RealVector,RealVector> denoisingAutoencoder = noise>>autoencoder;
54 /// and train the model using the standard autoencoder error
55 class GaussianNoiseModel : public AbstractModel<RealVector,RealVector>
56 {
57 private:
58  RealVector m_variances;
59 public:
60 
61 
62  /// Default Constructor; use setStructure later
65  }
66  /// Constructor creating a model with given input size and the same variance for all inputs
67  GaussianNoiseModel(unsigned int inputs, double variance)
68  : m_variances(inputs,variance){
70  }
71 
72  /// \brief From INameable: return the class name.
73  std::string name() const
74  { return "GaussianNoiseModel"; }
75 
76  /// obtain the input dimension
77  size_t inputSize() const{
78  return m_variances.size();
79  }
80 
81  /// obtain the output dimension
82  size_t outputSize() const{
83  return m_variances.size();
84  }
85 
86  /// obtain the parameter vector
87  RealVector parameterVector() const{
88  return RealVector();
89  }
90 
91  /// overwrite the parameter vector
92  void setParameterVector(RealVector const& newParameters)
93  {
94  SIZE_CHECK(newParameters.size() == 0);
95  }
96 
97  /// return the number of parameter
98  size_t numberOfParameters() const{
99  return 0;
100  }
101 
102  /// overwrite structure and parameters
103  void setStructure(unsigned int inputs, double variance){
104  m_variances = RealVector(inputs,variance);
105  }
106 
107  /// overwrite structure and parameters
108  void setStructure(RealVector const& variances){
109  m_variances = variances;
110  }
111 
112  RealVector const& variances() const{
113  return m_variances;
114  }
115 
116  RealVector& variances(){
117  return m_variances;
118  }
119 
120  boost::shared_ptr<State> createState()const{
121  return boost::shared_ptr<State>(new EmptyState());
122  }
123 
124  /// \brief Add noise to the input
125  void eval(BatchInputType const& inputs, BatchOutputType& outputs)const{
126  SIZE_CHECK(inputs.size2() == inputSize());
127  //we use the global Rng here so if this is a threaded region, we might
128  //run into troubles when multiple threads run this. This should not be a bottle neck
129  //as this routine should be quite fast, while very expensive routines are likely to
130  //follow in the networks following this.
132  outputs = inputs;
133  for(std::size_t i = 0; i != outputs.size1(); ++i){
134  for(std::size_t j = 0; j != outputs.size2(); ++j){
135  outputs(i,j) += Rng::gauss(0,m_variances(j));
136  }
137  }
138  }
139  }
140  /// Evaluate the model: output = matrix * input + offset
141  void eval(BatchInputType const& inputs, BatchOutputType& outputs, State& state)const{
142  eval(inputs,outputs);
143  }
144 
146  BatchInputType const& patterns, RealVector const& coefficients, State const& state, RealVector& gradient
147  )const{
148  gradient.resize(0);
149  }
150 };
151 
152 
153 }
154 #endif