LinearModel.cpp

00001 #include "LinearModel.h"
00002 
00003 extern StreamOutput cout;
00004 
00008 LinearModel::LinearModel()
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 }
00017 
00021 LinearModel::~LinearModel()
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 }
00041 
00046 void LinearModel::readSpecificMaps()
00047 {
00048     m_reg = m_doubleMap["initReg"];
00049     m_tuneRigeModifiers = m_boolMap["tuneRigeModifiers"];
00050 }
00051 
00056 void LinearModel::modelInit()
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 }
00083 
00092 void LinearModel::predictAllOutputs ( REAL* rawInputs, REAL* outputs, uint nSamples, uint crossRun )
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 }
00108 
00118 void LinearModel::modelUpdate ( REAL* input, REAL* target, uint nSamples, uint crossRun )
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 }
00123 
00128 void LinearModel::saveWeights ( int cross )
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 }
00145 
00150 void LinearModel::loadWeights ( int cross )
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 }
00174 
00178 void LinearModel::loadMetaWeights ( int cross )
00179 {
00180     char buf[1024];
00181     sprintf ( buf,"%02d",cross );
00182     string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf;
00183     cout<<"LoadMeta:"<<name<<endl;
00184     fstream f ( name.c_str(), ios::in );
00185     if ( f.is_open() == false )
00186         assert ( false );
00187     f.read ( ( char* ) &m_nTrain, sizeof ( int ) );
00188     f.read ( ( char* ) &m_nFeatures, sizeof ( int ) );
00189     f.read ( ( char* ) &m_nClass, sizeof ( int ) );
00190     f.read ( ( char* ) &m_nDomain, sizeof ( int ) );
00191     REAL* tmp = new REAL[m_nFeatures*m_nClass*m_nDomain];
00192     f.read ( ( char* ) tmp, sizeof ( REAL ) *m_nFeatures*m_nClass*m_nDomain );
00193     delete[] tmp;
00194     f.read ( ( char* ) &m_reg, sizeof ( double ) );
00195     f.read ( ( char* ) &m_maxSwing, sizeof ( double ) );
00196     f.close();
00197 }
00198 
00204 string LinearModel::templateGenerator ( int id, string preEffect, int nameID, bool blendStop )
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 }

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