summaryrefslogtreecommitdiff
path: root/src/CuTNetLib/cuObjectiveFunction.cc
blob: e2b0a1da0927334e52bc0e25f702b267267f3098 (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

#include "cuObjectiveFunction.h"

#include "Error.h"
#include "cumath.h"


namespace TNet
{



  CuObjectiveFunction*
  CuObjectiveFunction::
  Factory(ObjFunType type) {
    CuObjectiveFunction* ret = NULL;
    switch(type) {
      case MEAN_SQUARE_ERROR:  ret = new CuMeanSquareError;  break;
      case CROSS_ENTROPY:      ret = new CuCrossEntropy;     break;
      default: Error("Unknown ObjFun type");
    }
    return ret;
  }


  void 
  CuMeanSquareError::
  Evaluate(const CuMatrix<BaseFloat>& rNetOutput, const CuMatrix<BaseFloat>& rDesired, CuMatrix<BaseFloat>& rNetError)
  {
    //get the global error
    rNetError.CopyFrom(rNetOutput);
    rNetError.AddScaled(-1.0,rDesired,1.0);

    //calculate the MSE
    mAuxMat.CopyFrom(rNetError);
    mAuxMat.MulElem(mAuxMat);
    
    mAuxVec.Init(mAuxMat.Cols());
    mAuxVec.AddColSum(1.0,mAuxMat,0.0);
    mAuxVec.CopyTo(mAuxVecHost);

    mError += mAuxVecHost.Sum();
 
    //count the frames    
    mFrames += rNetError.Rows();
  }

  void 
  CuCrossEntropy::
  Evaluate(const CuMatrix<BaseFloat>& rNetOutput, const CuMatrix<BaseFloat>& rDesired, CuMatrix<BaseFloat>& rNetError)
  {
    if(rDesired.Cols() != rNetOutput.Cols()) {
      std::ostringstream os;
      os << "Non-matching dimensions of network output with training targets!!!"
         << " Netoutput:" << rNetOutput.Cols()
         << " Targets:" << rDesired.Cols();
      Error(os.str());
    }

    //get the global error
    //dXent/dSoftmax_in = y-d
    rNetError.CopyFrom(rNetOutput);
    rNetError.AddScaled(-1.0,rDesired,1.0);
   
    //check classification
    mClassifyVec.Init(rNetOutput.Rows());
    CuMath<BaseFloat>::CheckClass(rNetOutput,rDesired,mClassifyVec);
    mClassifyVec.CopyTo(mClassifyVecHost);
    mCorrect += mClassifyVecHost.Sum();

    //calculate Xent
    mAuxMat.CopyFrom(rNetOutput);
    mAuxMat.LogElem();
    mAuxMat.MulElem(rDesired);

    mAuxVec.Init(mAuxMat.Cols());
    mAuxVec.AddColSum(-1.0,mAuxMat,0.0);
    mAuxVec.CopyTo(mAuxVecHost);

    mError += mAuxVecHost.Sum();

    //count the frames    
    mFrames += rNetError.Rows();
  }


} // namespace TNet