summaryrefslogtreecommitdiff
path: root/src/TNetLib/Activation.h
blob: 90263d090cdc7cc55c44eca14d79b4789ec99551 (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
104

#ifndef _ACT_FUN_I_
#define _ACT_FUN_I_


#include "Component.h"


namespace TNet
{

  /**
   * Sigmoid activation function
   */
  class Sigmoid : public Component
  {
    public:
      Sigmoid(size_t nInputs, size_t nOutputs, Component *pPred)
       : Component(nInputs,nOutputs,pPred)
      { }

      ComponentType GetType() const
      { return SIGMOID; }

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

      Component* Clone() const
      { return new Sigmoid(GetNInputs(),GetNOutputs(),NULL); }

    protected:
      void PropagateFnc(const BfMatrix& X, BfMatrix& Y);
      void BackpropagateFnc(const BfMatrix& X, BfMatrix& Y);
  };
    

  /**
   * Softmax activation function
   */
  class Softmax : public Component
  {
    public:
      Softmax(size_t nInputs, size_t nOutputs, Component *pPred)
       : Component(nInputs,nOutputs,pPred)
      { }

      ComponentType GetType() const
      { return SOFTMAX; }

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

      Component* Clone() const
      { return new Softmax(GetNInputs(),GetNOutputs(),NULL); }

    protected:
      void PropagateFnc(const BfMatrix& X, BfMatrix& Y);
      void BackpropagateFnc(const BfMatrix& X, BfMatrix& Y);
  };


  /**
   * BlockSoftmax activation function.
   * It is several softmaxes in one.
   * The dimensions of softmaxes are given by integer vector.
   * During backpropagation: 
   *  If the derivatives sum up to 0, they are backpropagated. 
   *  If the derivatives sup up to 1, they are discarded
   *  (like this we know that the softmax was 'inactive').
   */
  class BlockSoftmax : public Component
  {
    public:
      BlockSoftmax(size_t nInputs, size_t nOutputs, Component *pPred)
       : Component(nInputs,nOutputs,pPred)
      { }

      ComponentType GetType() const
      { return BLOCK_SOFTMAX; }

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

      Component* Clone() const
      { return new BlockSoftmax(*this); }

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

    protected:
      void PropagateFnc(const BfMatrix& X, BfMatrix& Y);
      void BackpropagateFnc(const BfMatrix& X, BfMatrix& Y);

    private:
      Vector<int> mDim;
      Vector<int> mDimOffset;
  };


  
} //namespace


#endif