00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <limits>
00025 #include "IO/OutputInfo.h"
00026
00027 namespace MultiBoost {
00028
00029
00030
00031 OutputInfo::OutputInfo(const string& outputInfoFile)
00032 {
00033
00034 _outStream.open(outputInfoFile.c_str());
00035
00036
00037 if ( !_outStream.is_open() )
00038 {
00039 cerr << "ERROR: cannot open the output steam (<"
00040 << outputInfoFile << ">) for the step-by-step info!" << endl;
00041 exit(1);
00042 }
00043 }
00044
00045
00046
00047 void OutputInfo::outputIteration(const int t)
00048 {
00049 _outStream << t;
00050 }
00051
00052
00053
00054 void OutputInfo::outputError(InputData* pData, BaseLearner* pWeakHypothesis)
00055 {
00056 const int numClasses = ClassMappings::getNumClasses();
00057 const int numExamples = pData->getNumExamples();
00058
00059 if ( _gTableMap.find(pData) == _gTableMap.end() )
00060 {
00061
00062
00063
00064 table& g = _gTableMap[pData];
00065 g.resize(numExamples);
00066
00067 for ( int i = 0; i < numExamples; ++i )
00068 g[i].resize(numClasses, 0);
00069 }
00070
00071 table& g = _gTableMap[pData];
00072
00073 int numWrongs = 0;
00074
00075
00076 for (int i = 0; i < numExamples; ++i)
00077 {
00078
00079 int maxClassIdx = -1;
00080
00081
00082 double maxClass = -numeric_limits<double>::max();
00083
00084 for (int l = 0; l < numClasses; ++l)
00085 {
00086
00087 g[i][l] += pWeakHypothesis->getAlpha() *
00088 pWeakHypothesis->classify( pData, i, l );
00089
00090
00091 if (g[i][l] > maxClass)
00092 {
00093 maxClass = g[i][l];
00094 maxClassIdx = l;
00095 }
00096
00097 }
00098
00099
00100
00101 if (maxClassIdx != pData->getClass(i))
00102 ++numWrongs;
00103 }
00104
00105
00106 _outStream << '\t' << (double)(numWrongs)/(double)(numExamples);
00107 }
00108
00109
00110
00111 void OutputInfo::outputMargins(InputData* pData, BaseLearner* pWeakHypothesis)
00112 {
00113 const int numClasses = ClassMappings::getNumClasses();
00114 const int numExamples = pData->getNumExamples();
00115
00116 if ( _margins.find(pData) == _margins.end() )
00117 {
00118
00119
00120
00121 table& margins = _margins[pData];
00122 margins.resize(numExamples);
00123
00124 for ( int i = 0; i < numExamples; ++i )
00125 margins[i].resize(numClasses, 0);
00126 }
00127
00128
00129
00130 if ( _alphaSums.find(pData) == _alphaSums.end() )
00131 _alphaSums[pData] = 0;
00132
00133 table& margins = _margins[pData];
00134
00135 double minMargin = numeric_limits<double>::max();
00136 double belowZeroMargin = 0;
00137
00138 for (int i = 0; i < numExamples; ++i)
00139 {
00140 for (int l = 0; l < numClasses; ++l)
00141 {
00142
00143 double hy = pWeakHypothesis->classify(pData, i, l) *
00144 pData->getBinaryClass(i, l);
00145
00146 double w = pData->getWeight(i, l);
00147
00148
00149 margins[i][l] += pWeakHypothesis->getAlpha() * hy;
00150
00151
00152 if ( margins[i][l] < 0 )
00153 {
00154 if (l == pData->getClass(i))
00155 belowZeroMargin += ( 1.0 / (double)(2*numExamples) );
00156 else
00157 belowZeroMargin += ( 1.0 / (double)(2*numExamples * (numClasses - 1)) );
00158
00159 }
00160
00161
00162 if (margins[i][l] < minMargin)
00163 minMargin = margins[i][l];
00164 }
00165 }
00166
00167
00168 _alphaSums[pData] += pWeakHypothesis->getAlpha();
00169
00170 _outStream << '\t' << minMargin/_alphaSums[pData] << "\t"
00171 << belowZeroMargin;
00172 }
00173
00174
00175
00176 void OutputInfo::outputEdge(InputData* pData, BaseLearner* pWeakHypothesis)
00177 {
00178 const int numClasses = ClassMappings::getNumClasses();
00179 const int numExamples = pData->getNumExamples();
00180
00181 double gamma = 0;
00182
00183 for (int i = 0; i < numExamples; ++i)
00184 {
00185 for (int l = 0; l < numClasses; ++l)
00186 {
00187
00188 double hy = pWeakHypothesis->classify(pData, i, l) *
00189 pData->getBinaryClass(i, l);
00190
00191 double w = pData->getWeight(i, l);
00192
00193 gamma += w * hy;
00194 }
00195 }
00196
00197 _outStream << '\t' << gamma;
00198
00199 }
00200
00201
00202
00203 }