28 #ifndef SHARK_MODELS_AUTOENCODER_H 29 #define SHARK_MODELS_AUTOENCODER_H 33 #include <boost/serialization/vector.hpp> 45 template<
class H
iddenNeuron,
class OutputNeuron>
48 struct InternalState:
public State{
50 RealMatrix outputResponses;
100 return m_encoderMatrix;
104 return m_encoderMatrix;
109 return m_decoderMatrix;
113 return m_decoderMatrix;
123 return to_vector(m_encoderMatrix) |
to_vector(m_decoderMatrix) | m_hiddenBias | m_outputBias;
129 std::size_t endWeights1 = m_encoderMatrix.size1() * m_encoderMatrix.size2();
130 std::size_t endWeights2 = endWeights1 + m_decoderMatrix.size1() * m_decoderMatrix.size2();
131 std::size_t endBias = endWeights2 + m_hiddenBias.size();
135 noalias(m_outputBias) =
subrange(newParameters,endBias,endBias+m_outputBias.size());
143 InternalState
const& s = state.
toState<InternalState>();
144 return s.hiddenResponses;
149 return m_hiddenNeuron;
153 return m_outputNeuron;
158 return m_hiddenNeuron;
162 return m_outputNeuron;
166 return boost::shared_ptr<State>(
new InternalState());
169 void evalLayer(std::size_t layer,RealMatrix
const& patterns,RealMatrix& outputs)
const{
171 std::size_t numPatterns = patterns.size1();
176 outputs.resize(numPatterns,numOutputs);
179 noalias(outputs) = m_hiddenNeuron(outputs);
184 outputs.resize(numPatterns,numOutputs);
187 noalias(outputs) = m_outputNeuron(outputs);
213 template<
class Label>
220 template<
class Label>
228 void eval(RealMatrix
const& patterns,RealMatrix& output,
State& state)
const{
229 InternalState& s = state.
toState<InternalState>();
231 evalLayer(1,s.hiddenResponses,s.outputResponses);
232 output = s.outputResponses;
237 BatchInputType const& patterns, RealMatrix
const& coefficients,
State const& state, RealVector& gradient
240 SIZE_CHECK(coefficients.size1() == patterns.size1());
242 RealMatrix outputDelta = coefficients;
243 RealMatrix hiddenDelta;
244 computeDelta(state,outputDelta,hiddenDelta);
245 computeParameterDerivative(patterns,outputDelta,hiddenDelta,state,gradient);
252 SIZE_CHECK(coefficients.size1() == patterns.size1());
254 RealMatrix outputDelta = coefficients;
255 RealMatrix hiddenDelta;
256 computeDelta(state,outputDelta,hiddenDelta,inputDerivative);
263 RealVector& parameterDerivative,
267 SIZE_CHECK(coefficients.size1() == patterns.size1());
269 RealMatrix outputDelta = coefficients;
270 RealMatrix hiddenDelta;
271 computeDelta(state,outputDelta,hiddenDelta,inputDerivative);
272 computeParameterDerivative(patterns,outputDelta,hiddenDelta,state,parameterDerivative);
276 std::size_t in,std::size_t hidden
278 m_encoderMatrix.resize(hidden,in);
279 m_decoderMatrix.resize(in,hidden);
280 m_hiddenBias.resize(hidden);
281 m_outputBias.resize(in);
286 archive>>m_encoderMatrix;
287 archive>>m_decoderMatrix;
288 archive>>m_hiddenBias;
289 archive>>m_outputBias;
294 archive<<m_encoderMatrix;
295 archive<<m_decoderMatrix;
296 archive<<m_hiddenBias;
297 archive<<m_outputBias;
304 State const& state, RealMatrix& outputDelta, RealMatrix& hiddenDelta
306 InternalState
const& s = state.
toState<InternalState>();
308 noalias(outputDelta) *= m_outputNeuron.derivative(s.outputResponses);
311 noalias(hiddenDelta) *= m_hiddenNeuron.derivative(s.hiddenResponses);
315 State const& state, RealMatrix& outputDelta, RealMatrix& hiddenDelta, RealMatrix& inputDelta
317 computeDelta(state,outputDelta,hiddenDelta);
318 inputDelta.resize(outputDelta.size1(),
inputSize());
322 void computeParameterDerivative(
323 RealMatrix
const& patterns, RealMatrix
const& outputDelta, RealMatrix
const& hiddenDelta,
324 State const& state, RealVector& gradient
326 InternalState
const& s = state.
toState<InternalState>();
335 std::size_t hiddenBiasPos = 2*hiddenParams;
336 std::size_t outputBiasPos = hiddenBiasPos+numHidden;
342 RealMatrix m_encoderMatrix;
344 RealMatrix m_decoderMatrix;
346 RealVector m_hiddenBias;
348 RealVector m_outputBias;
351 HiddenNeuron m_hiddenNeuron;
353 OutputNeuron m_outputNeuron;