LinearModel Class Reference

#include <LinearModel.h>

Inheritance diagram for LinearModel:

StandardAlgorithm Framework Algorithm AutomaticParameterTuner AUC Framework Framework Data Framework Framework Framework

List of all members.

Public Member Functions

 LinearModel ()
 ~LinearModel ()
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
bool m_tuneRigeModifiers
NumericalTools solver
double * m_ridgeModifiers


Detailed Description

Linear prediction model Based on simple linear regression on targets

Tunable parameters are the regularization constant It offers the possibility to tune the regularizer per feature

Definition at line 16 of file LinearModel.h.


Constructor & Destructor Documentation

LinearModel::LinearModel (  ) 

Constructor

Definition at line 8 of file LinearModel.cpp.

00009 {
00010     cout<<"LinearModel"<<endl;
00011     // init member vars
00012     m_x = 0;
00013     m_reg = 0;
00014     m_tuneRigeModifiers = false;
00015     m_ridgeModifiers = 0;
00016 }

LinearModel::~LinearModel (  ) 

Destructor

Definition at line 21 of file LinearModel.cpp.

00022 {
00023     cout<<"descructor LinearModel"<<endl;
00024     for ( int i=0;i<m_nCross+1;i++ )
00025     {
00026         if ( m_x )
00027         {
00028             if ( m_x[i] )
00029                 delete[] m_x[i];
00030             m_x[i] = 0;
00031         }
00032     }
00033     if ( m_x )
00034         delete[] m_x;
00035     m_x = 0;
00036 
00037     if ( m_ridgeModifiers )
00038         delete[] m_ridgeModifiers;
00039     m_ridgeModifiers = 0;
00040 }


Member Function Documentation

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

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

Implements StandardAlgorithm.

Definition at line 150 of file LinearModel.cpp.

00151 {
00152     char buf[1024];
00153     sprintf ( buf,"%02d",cross );
00154     string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf;
00155     cout<<"Load:"<<name<<endl;
00156     fstream f ( name.c_str(), ios::in );
00157     if ( f.is_open() == false )
00158         assert ( false );
00159     f.read ( ( char* ) &m_nTrain, sizeof ( int ) );
00160     f.read ( ( char* ) &m_nFeatures, sizeof ( int ) );
00161     f.read ( ( char* ) &m_nClass, sizeof ( int ) );
00162     f.read ( ( char* ) &m_nDomain, sizeof ( int ) );
00163 
00164     m_x = new REAL*[m_nCross+1];
00165     for ( int i=0;i<m_nCross+1;i++ )
00166         m_x[i] = 0;
00167     m_x[cross] = new REAL[m_nFeatures * m_nClass * m_nDomain];
00168 
00169     f.read ( ( char* ) m_x[cross], sizeof ( REAL ) *m_nFeatures*m_nClass*m_nDomain );
00170     f.read ( ( char* ) &m_reg, sizeof ( double ) );
00171     f.read ( ( char* ) &m_maxSwing, sizeof ( double ) );
00172     f.close();
00173 }

void LinearModel::modelInit (  )  [virtual]

Init the Linear Model

Implements StandardAlgorithm.

Definition at line 56 of file LinearModel.cpp.

00057 {
00058     // add the tunable parameter
00059     paramDoubleValues.push_back ( &m_reg );
00060     paramDoubleNames.push_back ( "reg" );
00061 
00062     // alloc mem for weights
00063     if ( m_x == 0 )
00064     {
00065         m_x = new REAL*[m_nCross+1];
00066         for ( int i=0;i<m_nCross+1;i++ )
00067             m_x[i] = new REAL[m_nFeatures * m_nClass * m_nDomain];
00068     }
00069 
00070     if ( m_tuneRigeModifiers )
00071     {
00072         m_ridgeModifiers = new double[m_nFeatures];
00073         for ( int i=0;i<m_nFeatures;i++ )
00074         {
00075             m_ridgeModifiers[i] = 1.0;
00076             paramDoubleValues.push_back ( & ( m_ridgeModifiers[i] ) );
00077             char buf[1024];
00078             sprintf ( buf,"%d",i );
00079             paramDoubleNames.push_back ( "r"+string ( buf ) );
00080         }
00081     }
00082 }

void LinearModel::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 118 of file LinearModel.cpp.

00119 {
00120     // solve the linear system
00121     solver.RidgeRegressionMultisolutionSinglecall ( input, target, m_x[crossRun], nSamples, m_nFeatures, m_nClass*m_nDomain, m_reg, true, m_ridgeModifiers );
00122 }

void LinearModel::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 92 of file LinearModel.cpp.

00093 {
00094     for ( int i=0;i<nSamples;i++ )
00095     {
00096         for ( int j=0;j<m_nClass*m_nDomain;j++ )
00097         {
00098             REAL sum = 0.0;
00099             for ( int k=0;k<m_nFeatures;k++ )
00100             {
00101                 REAL x = rawInputs[i*m_nFeatures + k];
00102                 sum += x * m_x[crossRun][k*m_nClass*m_nDomain + j];
00103             }
00104             outputs[i*m_nClass*m_nDomain + j] = sum;
00105         }
00106     }
00107 }

void LinearModel::readSpecificMaps (  )  [virtual]

Read the Algorithm specific values from the description file

Implements StandardAlgorithm.

Definition at line 46 of file LinearModel.cpp.

00047 {
00048     m_reg = m_doubleMap["initReg"];
00049     m_tuneRigeModifiers = m_boolMap["tuneRigeModifiers"];
00050 }

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

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

Implements StandardAlgorithm.

Definition at line 128 of file LinearModel.cpp.

00129 {
00130     char buf[1024];
00131     sprintf ( buf,"%02d",cross );
00132     string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf;
00133     if ( m_inRetraining )
00134         cout<<"Save:"<<name<<endl;
00135     fstream f ( name.c_str(), ios::out );
00136     f.write ( ( char* ) &m_nTrain, sizeof ( int ) );
00137     f.write ( ( char* ) &m_nFeatures, sizeof ( int ) );
00138     f.write ( ( char* ) &m_nClass, sizeof ( int ) );
00139     f.write ( ( char* ) &m_nDomain, sizeof ( int ) );
00140     f.write ( ( char* ) m_x[cross], sizeof ( REAL ) *m_nFeatures*m_nClass*m_nDomain );
00141     f.write ( ( char* ) &m_reg, sizeof ( double ) );
00142     f.write ( ( char* ) &m_maxSwing, sizeof ( double ) );
00143     f.close();
00144 }

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

Generates a template of the description file

Returns:
The template string

Definition at line 204 of file LinearModel.cpp.

00205 {
00206     stringstream s;
00207     s<<"ALGORITHM=LinearModel"<<endl;
00208     s<<"ID="<<id<<endl;
00209     s<<"TRAIN_ON_FULLPREDICTOR="<<preEffect<<endl;
00210     s<<"DISABLE=0"<<endl;
00211     s<<endl;
00212     s<<"[int]"<<endl;
00213     s<<"maxTuninigEpochs=20"<<endl;
00214     s<<endl;
00215     s<<"[double]"<<endl;
00216     s<<"initMaxSwing=1.0"<<endl;
00217     s<<"initReg=0.01"<<endl;
00218     s<<endl;
00219     s<<"[bool]"<<endl;
00220     s<<"tuneRigeModifiers=0"<<endl;
00221     s<<"enableClipping=1"<<endl;
00222     s<<"enableTuneSwing=0"<<endl;
00223     s<<endl;
00224     s<<"minimzeProbe="<< ( !blendStop ) <<endl;
00225     s<<"minimzeProbeClassificationError=0"<<endl;
00226     s<<"minimzeBlend="<<blendStop<<endl;
00227     s<<"minimzeBlendClassificationError=0"<<endl;
00228     s<<endl;
00229     s<<"[string]"<<endl;
00230     s<<"weightFile="<<"LinearModel_"<<nameID<<"_weights.dat"<<endl;
00231     s<<"fullPrediction=LinearModel_"<<nameID<<".dat"<<endl;
00232 
00233     return s.str();
00234 }


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

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