summaryrefslogtreecommitdiff
path: root/src/CuTNetLib/.svn/text-base/cuObjectiveFunction.h.svn-base
blob: 6b425e8f6f8272e85e366c97719c53e70c243b0f (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef _CUOBJ_FUN_I_
#define _CUOBJ_FUN_I_

#include <cassert>
#include <limits>
#include <cmath>
#include <sstream>

#include "Vector.h"
#include "cuvector.h"
#include "cumatrix.h"

namespace TNet 
{

  
  /**
   * General interface for objective functions
   */
  class CuObjectiveFunction
  {
    public:
      /// Enum with objective function types
      typedef enum { 
        OBJ_FUN_I = 0x0300, 
        MEAN_SQUARE_ERROR, 
        CROSS_ENTROPY, 
      } ObjFunType;

      /// Factory for creating objective function instances
      static CuObjectiveFunction* Factory(ObjFunType type);
    
    //////////////////////////////////////////////////////////////
    // Interface specification
    public:
      CuObjectiveFunction() 
      { } 

      virtual ~CuObjectiveFunction() 
      { }

      virtual ObjFunType GetTypeId() = 0; 
      virtual const char* GetTypeLabel() = 0; 

      /// evaluates the data, calculate global error
      virtual void Evaluate(const CuMatrix<BaseFloat>& rNetOutput, const CuMatrix<BaseFloat>& rDesired, CuMatrix<BaseFloat>& rNetError) = 0;
 
      ///get the average per frame error
      virtual double GetError() = 0;  
      ///the number of processed frames 
      virtual size_t GetFrames() = 0;
      ///report the error to std::cout 
      virtual std::string Report() = 0;
  };




  /**
   * Means square error, useful for autoencoders, RBMs et al.
   */
  class CuMeanSquareError : public CuObjectiveFunction
  {
    public:
      CuMeanSquareError() 
        : mError(0), mFrames(0)
      { }
    
      virtual ~CuMeanSquareError() 
      { }

      ObjFunType GetTypeId()
      { return CuObjectiveFunction::MEAN_SQUARE_ERROR; }

      const char* GetTypeLabel()
      { return "<mean_square_error>"; }

      void Evaluate(const CuMatrix<BaseFloat>& rNetOutput, const CuMatrix<BaseFloat>& rDesired, CuMatrix<BaseFloat>& rNetError);
      
      double GetError()
      { return mError; }  
      
      size_t GetFrames()
      { return mFrames; }
      
      std::string Report()
      { 
        std::ostringstream ss;
        ss << "Mse:" << mError << " frames:" << mFrames 
           << " err/frm:" << mError/mFrames << "\n";
        return ss.str();
      }

    private:
      double mError;
      size_t mFrames;

      CuMatrix<BaseFloat> mAuxMat;
      CuVector<BaseFloat> mAuxVec;
      Vector<BaseFloat> mAuxVecHost;

  };


 /**
   * Cross entropy, it assumes desired vectors as output values 
   */
  class CuCrossEntropy : public CuObjectiveFunction
  {
    public:
      CuCrossEntropy() 
        : mError(0), mFrames(0), mCorrect(0)
      { }
      
      ~CuCrossEntropy() 
      { }
      
      ObjFunType GetTypeId()
      { return CuObjectiveFunction::CROSS_ENTROPY; }

      const char* GetTypeLabel()
      { return "<cross_entropy>"; }

      void Evaluate(const CuMatrix<BaseFloat>& rNetOutput, const CuMatrix<BaseFloat>& rDesired, CuMatrix<BaseFloat>& rNetError);

      double GetError()
      { return mError; }

      size_t GetFrames()
      { return mFrames; }

      std::string Report()
      {
        std::ostringstream ss;
        //for compatibility with SNet
        //ss << " correct: >> " << 100.0*mCorrect/mFrames << "% <<\n";
        
        //current new format...
        ss << "Xent:" << mError << " frames:" << mFrames 
           << " err/frm:" << mError/mFrames 
           << " correct[" << 100.0*mCorrect/mFrames << "%]"
           << "\n";
        return ss.str();
      }

    private:
      double mError;
      size_t mFrames;
      size_t mCorrect;
      
      CuMatrix<BaseFloat> mAuxMat;
      CuVector<BaseFloat> mAuxVec;
      Vector<BaseFloat> mAuxVecHost;

      CuVector<int> mClassifyVec;
      Vector<int> mClassifyVecHost;
  };





} //namespace TNet


#endif