28 #ifndef SHARK_MODELS_TIEDAUTOENCODER_H 29 #define SHARK_MODELS_TIEDAUTOENCODER_H 34 #include <boost/serialization/vector.hpp> 44 template<
class H
iddenNeuron,
class OutputNeuron>
47 struct InternalState:
public State{
49 RealMatrix outputResponses;
61 return "TiedAutoencoder";
99 return m_weightMatrix;
103 return m_weightMatrix;
109 return trans(m_weightMatrix);
115 return trans(m_weightMatrix);
125 return to_vector(m_weightMatrix) | m_hiddenBias | m_outputBias;
130 std::size_t endWeights = m_weightMatrix.size1() * m_weightMatrix.size2();
131 std::size_t endBias = endWeights + m_hiddenBias.size();
134 noalias(m_outputBias) =
subrange(newParameters,endBias,endBias+m_outputBias.size());
139 return m_hiddenNeuron;
143 return m_outputNeuron;
148 return m_hiddenNeuron;
152 return m_outputNeuron;
160 InternalState
const& s = state.
toState<InternalState>();
161 return s.hiddenResponses;
165 return boost::shared_ptr<State>(
new InternalState());
168 void evalLayer(std::size_t layer,RealMatrix
const& patterns,RealMatrix& outputs)
const{
170 std::size_t numPatterns = patterns.size1();
175 outputs.resize(numPatterns,numOutputs);
177 noalias(outputs) = m_hiddenNeuron(outputs);
182 outputs.resize(numPatterns,numOutputs);
184 noalias(outputs) = m_outputNeuron(outputs);
210 template<
class Label>
217 template<
class Label>
224 void eval(RealMatrix
const& patterns,RealMatrix& output,
State& state)
const{
225 InternalState& s = state.
toState<InternalState>();
227 evalLayer(1,s.hiddenResponses,s.outputResponses);
228 output = s.outputResponses;
233 BatchInputType const& patterns, RealMatrix
const& coefficients,
State const& state, RealVector& gradient
236 SIZE_CHECK(coefficients.size1() == patterns.size1());
238 RealMatrix outputDelta = coefficients;
239 RealMatrix hiddenDelta;
240 computeDelta(state,outputDelta,hiddenDelta);
241 computeParameterDerivative(patterns,outputDelta,hiddenDelta,state,gradient);
248 SIZE_CHECK(coefficients.size1() == patterns.size1());
250 RealMatrix outputDelta = coefficients;
251 RealMatrix hiddenDelta;
252 computeDelta(state,outputDelta,hiddenDelta,inputDerivative);
259 RealVector& parameterDerivative,
263 SIZE_CHECK(coefficients.size1() == patterns.size1());
265 RealMatrix outputDelta = coefficients;
266 RealMatrix hiddenDelta;
267 computeDelta(state,outputDelta,hiddenDelta,inputDerivative);
268 computeParameterDerivative(patterns,outputDelta,hiddenDelta,state,parameterDerivative);
272 std::size_t in,std::size_t hidden
274 m_weightMatrix.resize(hidden,in);
275 m_hiddenBias.resize(hidden);
276 m_outputBias.resize(in);
281 archive>>m_weightMatrix;
282 archive>>m_hiddenBias;
283 archive>>m_outputBias;
288 archive<<m_weightMatrix;
289 archive<<m_hiddenBias;
290 archive<<m_outputBias;
297 State const& state, RealMatrix& outputDelta, RealMatrix& hiddenDelta
299 InternalState
const& s = state.
toState<InternalState>();
301 noalias(outputDelta) *= m_outputNeuron.derivative(s.outputResponses);
304 noalias(hiddenDelta) *= m_hiddenNeuron.derivative(s.hiddenResponses);
308 State const& state, RealMatrix& outputDelta, RealMatrix& hiddenDelta, RealMatrix& inputDelta
310 computeDelta(state,outputDelta,hiddenDelta);
311 inputDelta.resize(outputDelta.size1(),
inputSize());
315 void computeParameterDerivative(
316 RealMatrix
const& patterns, RealMatrix
const& outputDelta, RealMatrix
const& hiddenDelta,
317 State const& state, RealVector& gradient
319 InternalState
const& s = state.
toState<InternalState>();
327 std::size_t hiddenBiasPos = hiddenParams;
328 std::size_t outputBiasPos = hiddenBiasPos+numHidden;
334 RealMatrix m_weightMatrix;
336 RealVector m_hiddenBias;
338 RealVector m_outputBias;
341 HiddenNeuron m_hiddenNeuron;
343 OutputNeuron m_outputNeuron;