ImpulseNoiseModel.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements a Model which corrupts the input by setting values of the input to a given value
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_ImpulseNOISEMODEL_H
33 #define SHARK_MODELS_ImpulseNOISEMODEL_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 Impulse noise
41 ///
42 /// We define impulse noise as a noise which randomly sets the value of an element
43 /// in a vector to a given value, 0 by default. We chose the name as with for example
44 /// a noise of 1, impulses can be seen in the visualised vectors.
45 ///
46 /// very input dimension is tested independently.
47 ///
48 /// This noise can be used to implement denoising autoencoders for binary data.
49 class ImpulseNoiseModel : public AbstractModel<RealVector,RealVector>
50 {
51 private:
52  std::size_t m_numInputs;
53  double m_prob;
54  double m_value;
55 public:
56 
57 
58  /// Default Constructor; use setStructure later
61  }
62  /// Constructor creating a model with given input size and a probability to set values of the input to a given value.
63  ImpulseNoiseModel(unsigned int inputs, double prob, double value = 0.0)
64  : m_numInputs(inputs), m_prob(prob), m_value(value){
66  }
67 
68  /// \brief From INameable: return the class name.
69  std::string name() const
70  { return "ImpulseNoiseModel"; }
71 
72  /// obtain the input dimension
73  size_t inputSize() const{
74  return m_numInputs;
75  }
76 
77  /// obtain the output dimension
78  size_t outputSize() const{
79  return m_numInputs;
80  }
81 
82  /// obtain the parameter vector
83  RealVector parameterVector() const{
84  return RealVector();
85  }
86 
87  /// overwrite the parameter vector
88  void setParameterVector(RealVector const& newParameters)
89  {
90  SIZE_CHECK(newParameters.size() == 0);
91  }
92 
93  /// return the number of parameter
94  size_t numberOfParameters() const{
95  return 0;
96  }
97  boost::shared_ptr<State> createState()const{
98  return boost::shared_ptr<State>(new EmptyState());
99  }
100 
101  /// \brief Add noise to the input
102  void eval(BatchInputType const& inputs, BatchOutputType& outputs)const{
103  SIZE_CHECK(inputs.size2() == inputSize());
105  outputs = inputs;
106  for(std::size_t i = 0; i != outputs.size1(); ++i){
107  for(std::size_t j = 0; j != outputs.size2(); ++j){
108  if(Rng::coinToss(m_prob)){
109  outputs(i,j) = m_value;
110  }
111  }
112  }
113  }
114  }
115  /// Evaluate the model: output = matrix * input + offset
116  void eval(BatchInputType const& inputs, BatchOutputType& outputs, State& state)const{
117  eval(inputs,outputs);
118  }
119 
121  BatchInputType const& patterns, RealVector const& coefficients, State const& state, RealVector& gradient
122  )const{
123  gradient.resize(0);
124  }
125 };
126 
127 
128 }
129 #endif