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
|
#ifndef _TNET_OBJ_FUN_H
#define _TNET_OBJ_FUN_H
#include <cassert>
#include <limits>
#include <cmath>
#include "Matrix.h"
#include "Vector.h"
namespace TNet {
/**
* General interface for objective functions
*/
class ObjectiveFunction
{
public:
/// Enum with objective function types
typedef enum {
OBJ_FUN_I = 0x0300,
MEAN_SQUARE_ERROR,
CROSS_ENTROPY,
} ObjFunType;
public:
/// Factory for creating objective function instances
static ObjectiveFunction* Factory(ObjFunType type);
//////////////////////////////////////////////////////////////
// Interface specification
protected:
ObjectiveFunction() { }; /// constructor
public:
virtual ~ObjectiveFunction() { }; /// destructor
virtual ObjFunType GetType() = 0;
virtual const char* GetName() = 0;
virtual ObjectiveFunction* Clone() = 0;
///calculate error of network output
virtual void Evaluate(const Matrix<BaseFloat>& net_out, const Matrix<BaseFloat>& target, Matrix<BaseFloat>* err) = 0;
///get the accumulated error
virtual double GetError() = 0;
///the number of processed frames
virtual size_t GetFrames() = 0;
///report the error to string
virtual std::string Report() = 0;
///sum the frame counts from more instances
virtual void MergeStats(const ObjectiveFunction& inst) = 0;
};
/**
* Mean square error function
*/
class MeanSquareError : public ObjectiveFunction
{
public:
MeanSquareError()
: ObjectiveFunction(), frames_(0), error_(0)
{ }
~MeanSquareError()
{ }
ObjFunType GetType()
{ return MEAN_SQUARE_ERROR; }
const char* GetName()
{ return "<MeanSquareError>"; }
ObjectiveFunction* Clone()
{ return new MeanSquareError(*this); }
void Evaluate(const Matrix<BaseFloat>& net_out, const Matrix<BaseFloat>& target, Matrix<BaseFloat>* err);
size_t GetFrames()
{ return frames_; }
double GetError()
{ return error_; }
std::string Report();
void MergeStats(const ObjectiveFunction& inst) {
const MeanSquareError& mse = dynamic_cast<const MeanSquareError&>(inst);
frames_ += mse.frames_; error_ += mse.error_;
}
private:
size_t frames_;
double error_;
};
/**
* Cross entropy error function
*/
class CrossEntropy : public ObjectiveFunction
{
public:
enum ConfusionMode { NO_CONF=0, MAX_CONF, SOFT_CONF, DIAG_MAX_CONF, DIAG_SOFT_CONF };
public:
CrossEntropy()
: ObjectiveFunction(), frames_(0), error_(0), corr_(0), confusion_mode_(NO_CONF), output_label_map_(NULL)
{ }
~CrossEntropy()
{ }
ObjFunType GetType()
{ return CROSS_ENTROPY; }
const char* GetName()
{ return "<cross_entropy>"; }
ObjectiveFunction* Clone()
{ return new CrossEntropy(*this); }
void Evaluate(const Matrix<BaseFloat>& net_out, const Matrix<BaseFloat>& target, Matrix<BaseFloat>* err);
size_t GetFrames()
{ return frames_; }
double GetError()
{ return error_; }
void SetConfusionMode(enum ConfusionMode m)
{ confusion_mode_ = m; }
void SetOutputLabelMap(const char* map)
{ output_label_map_ = map; }
std::string Report();
void MergeStats(const ObjectiveFunction& inst);
private:
size_t frames_;
double error_;
size_t corr_;
ConfusionMode confusion_mode_;
Matrix<float> confusion_;
Vector<int> confusion_count_;
Vector<double> diag_confusion_;
const char* output_label_map_;
};
} //namespace TNet
#endif
|