summaryrefslogtreecommitdiff
path: root/src/TNetLib/.svn/text-base/SharedLinearity.h.svn-base
blob: 83feeeec91dfe058382340553cba8a4c16c1d128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef _CUSHARED_LINEARITY_H_
#define _CUSHARED_LINEARITY_H_


#include "Component.h"

#include "Matrix.h"
#include "Vector.h"


namespace TNet {

class SharedLinearity : public UpdatableComponent
{
 public:
  SharedLinearity(size_t nInputs, size_t nOutputs, Component *pPred); 
  ~SharedLinearity();  
  
  ComponentType GetType() const 
  { return SHARED_LINEARITY; }

  const char* GetName() const
  { return "<SharedLinearity>"; }

  Component* Clone() const;

  void PropagateFnc(const Matrix<BaseFloat>& X, Matrix<BaseFloat>& Y);
  void BackpropagateFnc(const Matrix<BaseFloat>& X, Matrix<BaseFloat>& Y);

  void ReadFromStream(std::istream& rIn);
  void WriteToStream(std::ostream& rOut);

  /// calculate gradient
  void Gradient(); 
  /// accumulate gradient from other components
  void AccuGradient(const UpdatableComponent& src, int thr, int thrN);
  /// update weights, reset the accumulator
  void Update(int thr, int thrN);

protected:
  Matrix<BaseFloat> mLinearity;  ///< Matrix with neuron weights
  Vector<BaseFloat> mBias;       ///< Vector with biases

  Matrix<BaseFloat>* mpLinearity;
  Vector<BaseFloat>* mpBias;

  Matrix<BaseFloat> mLinearityCorrection; ///< Matrix for linearity updates
  Vector<BaseFloat> mBiasCorrection;      ///< Vector for bias updates

  Matrix<double> mLinearityCorrectionAccu; ///< Accumulator for linearity updates
  Vector<double> mBiasCorrectionAccu;      ///< Accumulator for bias updates
  
  int mNInstances;
};




////////////////////////////////////////////////////////////////////////////
// INLINE FUNCTIONS 
// SharedLinearity::
inline 
SharedLinearity::
SharedLinearity(size_t nInputs, size_t nOutputs, Component *pPred)
  : UpdatableComponent(nInputs, nOutputs, pPred),
    mpLinearity(&mLinearity), mpBias(&mBias), 
    mNInstances(0)
{ }


inline
SharedLinearity::
~SharedLinearity()
{ }


inline
Component*
SharedLinearity::
Clone() const
{
  SharedLinearity* ptr = new SharedLinearity(GetNInputs(),GetNOutputs(),NULL);
  ptr->mpLinearity = mpLinearity;
  ptr->mpBias = mpBias;

  ptr->mLinearityCorrection.Init(mpLinearity->Rows(),mpLinearity->Cols());
  ptr->mBiasCorrection.Init(mpBias->Dim());

  ptr->mNInstances = mNInstances;

  ptr->mLearningRate = mLearningRate;


  return ptr;
}



} //namespace



#endif