LogisticRegression.cpp

00001 #include "LogisticRegression.h"
00002 
00003 extern StreamOutput cout;
00004 
00008 LogisticRegression::LogisticRegression()
00009 {
00010     cout<<"LogisticRegression"<<endl;
00011     // init member vars
00012     m_x = 0;
00013     m_reg = 0;
00014     m_tuneOffsetScale = 0;
00015 }
00016 
00020 LogisticRegression::~LogisticRegression()
00021 {
00022     cout<<"descructor LogisticRegression"<<endl;
00023     for ( int i=0;i<m_nCross+1;i++ )
00024     {
00025         if ( m_x )
00026         {
00027             if ( m_x[i] )
00028                 delete[] m_x[i];
00029             m_x[i] = 0;
00030         }
00031     }
00032     if ( m_x )
00033         delete[] m_x;
00034     m_x = 0;
00035 }
00036 
00041 void LogisticRegression::readSpecificMaps()
00042 {
00043     m_reg = m_doubleMap["initReg"];
00044     m_offset = m_doubleMap["initOffset"];
00045     m_scale = m_doubleMap["initScale"];
00046     m_tuneOffsetScale = m_boolMap["tuneOffsetScale"];
00047 }
00048 
00053 void LogisticRegression::modelInit()
00054 {
00055     if(m_nDomain != 1)
00056         assert(false); // do not allow this
00057     
00058     // add the tunable parameter
00059     paramDoubleValues.push_back ( &m_reg );
00060     paramDoubleNames.push_back ( "reg" );
00061     if ( m_tuneOffsetScale )
00062     {
00063         paramDoubleValues.push_back ( &m_offset );
00064         paramDoubleNames.push_back ( "offset" );
00065         paramDoubleValues.push_back ( &m_scale );
00066         paramDoubleNames.push_back ( "scale" );
00067     }
00068     // alloc mem for weights
00069     if ( m_x == 0 )
00070     {
00071         m_x = new REAL*[m_nCross+1];
00072         for ( int i=0;i<m_nCross+1;i++ )
00073             m_x[i] = new REAL[m_nFeatures * (m_nClass - 1)];
00074     }
00075 }
00076 
00085 void LogisticRegression::predictAllOutputs ( REAL* rawInputs, REAL* outputs, uint nSamples, uint crossRun )
00086 {
00087     for ( int i=0;i<nSamples;i++ )
00088     {
00089         /* MATLAB
00090         function p = predictLogistic(A,x)
00091         N = size(A,1);
00092         k = size(x,2)+1;
00093         p0 = [ones(size(A,1),1) A]*x;
00094         p1 = [exp(p0) ones(N,1)];
00095         p2 = p1 ./ repmat(sum(p1,2),1,k);
00096         p = p2;
00097         */
00098         
00099         for(int j=0;j<m_nClass-1;j++)
00100         {
00101             REAL sum = 0.0;
00102             for(int k=0;k<m_nFeatures;k++)
00103                 sum += rawInputs[i*m_nFeatures+k] * m_x[crossRun][k*(m_nClass-1) + j];
00104             outputs[i*m_nClass+j] = sum;
00105         }
00106         
00107         for(int j=0;j<m_nClass-1;j++)
00108             outputs[i*m_nClass+j] = exp(outputs[i*m_nClass+j]);
00109         outputs[i*m_nClass+m_nClass-1] = 1.0;
00110         
00111         REAL sum = 0.0;
00112         for(int j=0;j<m_nClass;j++)
00113             sum += outputs[i*m_nClass+j];
00114         
00115         for(int j=0;j<m_nClass;j++)
00116             outputs[i*m_nClass+j] /= sum;
00117     }
00118 }
00119 
00129 void LogisticRegression::modelUpdate ( REAL* input, REAL* target, uint nSamples, uint crossRun )
00130 {
00131     // solve the linear system
00132     solver.LogisticRegressionMultisolutionSinglecall ( input, target, m_x[crossRun], nSamples, m_nFeatures, m_nClass-1, m_reg, m_offset, m_scale );
00133 }
00134 
00139 void LogisticRegression::saveWeights ( int cross )
00140 {
00141     char buf[1024];
00142     sprintf ( buf,"%02d",cross );
00143     string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf;
00144     if ( m_inRetraining )
00145         cout<<"Save:"<<name<<endl;
00146     fstream f ( name.c_str(), ios::out );
00147     f.write ( ( char* ) &m_nTrain, sizeof ( int ) );
00148     f.write ( ( char* ) &m_nFeatures, sizeof ( int ) );
00149     f.write ( ( char* ) &m_nClass, sizeof ( int ) );
00150     f.write ( ( char* ) &m_nDomain, sizeof ( int ) );
00151     f.write ( ( char* ) m_x[cross], sizeof ( REAL ) *m_nFeatures*(m_nClass-1) );
00152     f.write ( ( char* ) &m_reg, sizeof ( double ) );
00153     f.write ( ( char* ) &m_offset, sizeof ( double ) );
00154     f.write ( ( char* ) &m_scale, sizeof ( double ) );
00155     f.write ( ( char* ) &m_maxSwing, sizeof ( double ) );
00156     f.close();
00157 }
00158 
00163 void LogisticRegression::loadWeights ( int cross )
00164 {
00165     char buf[1024];
00166     sprintf ( buf,"%02d",cross );
00167     string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf;
00168     cout<<"Load:"<<name<<endl;
00169     fstream f ( name.c_str(), ios::in );
00170     if ( f.is_open() == false )
00171         assert ( false );
00172     f.read ( ( char* ) &m_nTrain, sizeof ( int ) );
00173     f.read ( ( char* ) &m_nFeatures, sizeof ( int ) );
00174     f.read ( ( char* ) &m_nClass, sizeof ( int ) );
00175     f.read ( ( char* ) &m_nDomain, sizeof ( int ) );
00176 
00177     m_x = new REAL*[m_nCross+1];
00178     for ( int i=0;i<m_nCross+1;i++ )
00179         m_x[i] = 0;
00180     m_x[cross] = new REAL[m_nFeatures * (m_nClass-1)];
00181 
00182     f.read ( ( char* ) m_x[cross], sizeof ( REAL ) *m_nFeatures*(m_nClass-1) );
00183     f.read ( ( char* ) &m_reg, sizeof ( double ) );
00184     f.read ( ( char* ) &m_offset, sizeof ( double ) );
00185     f.read ( ( char* ) &m_scale, sizeof ( double ) );
00186     f.read ( ( char* ) &m_maxSwing, sizeof ( double ) );
00187     f.close();
00188 }
00189 
00193 void LogisticRegression::loadMetaWeights ( int cross )
00194 {
00195     char buf[1024];
00196     sprintf ( buf,"%02d",cross );
00197     string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf;
00198     cout<<"LoadMeta:"<<name<<endl;
00199     fstream f ( name.c_str(), ios::in );
00200     if ( f.is_open() == false )
00201         assert ( false );
00202     f.read ( ( char* ) &m_nTrain, sizeof ( int ) );
00203     f.read ( ( char* ) &m_nFeatures, sizeof ( int ) );
00204     f.read ( ( char* ) &m_nClass, sizeof ( int ) );
00205     f.read ( ( char* ) &m_nDomain, sizeof ( int ) );
00206     REAL* tmp = new REAL[m_nFeatures*m_nClass*m_nDomain];
00207     f.read ( ( char* ) tmp, sizeof ( REAL ) *m_nFeatures*(m_nClass-1) );
00208     delete[] tmp;
00209     f.read ( ( char* ) &m_reg, sizeof ( double ) );
00210     f.read ( ( char* ) &m_offset, sizeof ( double ) );
00211     f.read ( ( char* ) &m_scale, sizeof ( double ) );
00212     f.read ( ( char* ) &m_maxSwing, sizeof ( double ) );
00213     f.close();
00214 }
00215 
00221 string LogisticRegression::templateGenerator ( int id, string preEffect, int nameID, bool blendStop )
00222 {
00223     stringstream s;
00224     s<<"ALGORITHM=LogisticRegression"<<endl;
00225     s<<"ID="<<id<<endl;
00226     s<<"TRAIN_ON_FULLPREDICTOR="<<preEffect<<endl;
00227     s<<"DISABLE=0"<<endl;
00228     s<<endl;
00229     s<<"[int]"<<endl;
00230     s<<"maxTuninigEpochs=20"<<endl;
00231     s<<endl;
00232     s<<"[double]"<<endl;
00233     s<<"initMaxSwing=1.0"<<endl;
00234     s<<"initReg=0.01"<<endl;
00235     s<<"initOffset=0.5"<<endl;
00236     s<<"initScale=2.0"<<endl;
00237     s<<endl;
00238     s<<"[bool]"<<endl;
00239     s<<"tuneOffsetScale=1"<<endl;
00240     s<<"enableClipping=1"<<endl;
00241     s<<"enableTuneSwing=0"<<endl;
00242     s<<endl;
00243     s<<"minimzeProbe="<< ( !blendStop ) <<endl;
00244     s<<"minimzeProbeClassificationError=0"<<endl;
00245     s<<"minimzeBlend="<<blendStop<<endl;
00246     s<<"minimzeBlendClassificationError=0"<<endl;
00247     s<<endl;
00248     s<<"[string]"<<endl;
00249     s<<"weightFile="<<"LogisticRegression_"<<nameID<<"_weights.dat"<<endl;
00250     s<<"fullPrediction=LogisticRegression_"<<nameID<<".dat"<<endl;
00251 
00252     return s.str();
00253 }

Generated on Tue Jan 26 09:20:59 2010 for ELF by  doxygen 1.5.8