LogisticRegression Class Reference

#include <LogisticRegression.h>

Inheritance diagram for LogisticRegression:

StandardAlgorithm Framework Algorithm AutomaticParameterTuner AUC Framework Framework Data Framework Framework Framework

List of all members.

Public Member Functions

 LogisticRegression ()
 ~LogisticRegression ()
virtual void modelInit ()
virtual void modelUpdate (REAL *input, REAL *target, uint nSamples, uint crossRun)
virtual void predictAllOutputs (REAL *rawInputs, REAL *outputs, uint nSamples, uint crossRun)
virtual void readSpecificMaps ()
virtual void saveWeights (int cross)
virtual void loadWeights (int cross)
virtual void loadMetaWeights (int cross)

Static Public Member Functions

static string templateGenerator (int id, string preEffect, int nameID, bool blendStop)

Private Attributes

REAL ** m_x
double m_reg
double m_scale
double m_offset
bool m_tuneOffsetScale
NumericalTools solver


Detailed Description

EXPERIMENTAL ALGORITHM !!

THIS CODE IS NOT fully CORRECT!! or too slow.. Please do not use this class

Logistic regression prediction model Based on a sigmoid wrapper function with a linear model Look at: http://www.cs.cmu.edu/~ggordon/IRLS-example/

Tunable parameters are the regularization constant

Definition at line 21 of file LogisticRegression.h.


Constructor & Destructor Documentation

LogisticRegression::LogisticRegression (  ) 

Constructor

Definition at line 8 of file LogisticRegression.cpp.

00009 {
00010     cout<<"LogisticRegression"<<endl;
00011     // init member vars
00012     m_x = 0;
00013     m_reg = 0;
00014     m_tuneOffsetScale = 0;
00015 }

LogisticRegression::~LogisticRegression (  ) 

Destructor

Definition at line 20 of file LogisticRegression.cpp.

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 }


Member Function Documentation

void LogisticRegression::loadWeights ( int  cross  )  [virtual]

Load the weights and all other parameters and make the model ready to predict

Implements StandardAlgorithm.

Definition at line 163 of file LogisticRegression.cpp.

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 }

void LogisticRegression::modelInit (  )  [virtual]

Init the Linear Model

Implements StandardAlgorithm.

Definition at line 53 of file LogisticRegression.cpp.

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 }

void LogisticRegression::modelUpdate ( REAL *  input,
REAL *  target,
uint  nSamples,
uint  crossRun 
) [virtual]

Make a model update, set the new cross validation set or set the whole training set for retraining

Parameters:
input Pointer to input (can be cross validation set, or whole training set) (rows x nFeatures)
target The targets (can be cross validation targets)
nSamples The sample size (rows) in input
crossRun The cross validation run (for training)

Implements StandardAlgorithm.

Definition at line 129 of file LogisticRegression.cpp.

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 }

void LogisticRegression::predictAllOutputs ( REAL *  rawInputs,
REAL *  outputs,
uint  nSamples,
uint  crossRun 
) [virtual]

Prediction for outside use, predicts outputs based on raw input values

Parameters:
rawInputs The input feature, without normalization (raw)
outputs The output value (prediction of target)
nSamples The input size (number of rows)
crossRun Number of cross validation run (in training)

Implements StandardAlgorithm.

Definition at line 85 of file LogisticRegression.cpp.

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 }

void LogisticRegression::readSpecificMaps (  )  [virtual]

Read the Algorithm specific values from the description file

Implements StandardAlgorithm.

Definition at line 41 of file LogisticRegression.cpp.

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 }

void LogisticRegression::saveWeights ( int  cross  )  [virtual]

Save the weights and all other parameters for load the complete prediction model

Implements StandardAlgorithm.

Definition at line 139 of file LogisticRegression.cpp.

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 }

string LogisticRegression::templateGenerator ( int  id,
string  preEffect,
int  nameID,
bool  blendStop 
) [static]

Generates a template of the description file

Returns:
The template string

Definition at line 221 of file LogisticRegression.cpp.

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 }


The documentation for this class was generated from the following files:

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