#include <LinearModelNonNeg.h>
Public Member Functions | |
LinearModelNonNeg () | |
~LinearModelNonNeg () | |
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_bias |
int | m_maxNonNegSolverIterations |
NumericalTools | solver |
double | m_initReg |
double | m_initScale |
double | m_initBias |
Tunable parameters are the regularization constant and the target offsets
Definition at line 15 of file LinearModelNonNeg.h.
LinearModelNonNeg::LinearModelNonNeg | ( | ) |
Constructor
Definition at line 8 of file LinearModelNonNeg.cpp.
00009 { 00010 cout<<"LinearModelNonNeg"<<endl; 00011 // init member vars 00012 m_x = 0; 00013 m_reg = 0; 00014 m_scale = 0; 00015 m_bias = 0; 00016 m_maxNonNegSolverIterations = 0; 00017 m_initReg = 0; 00018 m_initScale = 0; 00019 m_initBias = 0; 00020 }
LinearModelNonNeg::~LinearModelNonNeg | ( | ) |
Destructor
Definition at line 25 of file LinearModelNonNeg.cpp.
00026 { 00027 cout<<"descructor LinearModelNonNeg"<<endl; 00028 for ( int i=0;i<m_nCross+1;i++ ) 00029 { 00030 if ( m_x ) 00031 { 00032 if ( m_x[i] ) 00033 delete[] m_x[i]; 00034 m_x[i] = 0; 00035 } 00036 } 00037 if ( m_x ) 00038 delete[] m_x; 00039 m_x = 0; 00040 }
void LinearModelNonNeg::loadWeights | ( | int | cross | ) | [virtual] |
Load the weights and all other parameters and make the model ready to predict
Implements StandardAlgorithm.
Definition at line 172 of file LinearModelNonNeg.cpp.
00173 { 00174 char buf[1024]; 00175 sprintf ( buf,"%02d",cross ); 00176 string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf; 00177 cout<<"Load:"<<name<<endl; 00178 fstream f ( name.c_str(), ios::in ); 00179 if ( f.is_open() == false ) 00180 assert ( false ); 00181 f.read ( ( char* ) &m_nTrain, sizeof ( int ) ); 00182 f.read ( ( char* ) &m_nFeatures, sizeof ( int ) ); 00183 f.read ( ( char* ) &m_nClass, sizeof ( int ) ); 00184 f.read ( ( char* ) &m_nDomain, sizeof ( int ) ); 00185 00186 m_x = new REAL*[m_nCross+1]; 00187 for ( int i=0;i<m_nCross+1;i++ ) 00188 m_x[i] = 0; 00189 m_x[cross] = new REAL[m_nFeatures * m_nClass * m_nDomain]; 00190 00191 f.read ( ( char* ) m_x[cross], sizeof ( REAL ) *m_nFeatures*m_nClass*m_nDomain ); 00192 f.read ( ( char* ) &m_maxSwing, sizeof ( double ) ); 00193 f.read ( ( char* ) &m_reg, sizeof ( double ) ); 00194 f.read ( ( char* ) &m_bias, sizeof ( double ) ); 00195 f.read ( ( char* ) &m_scale, sizeof ( double ) ); 00196 f.close(); 00197 }
void LinearModelNonNeg::modelInit | ( | ) | [virtual] |
Init the Linear Model
Implements StandardAlgorithm.
Definition at line 59 of file LinearModelNonNeg.cpp.
00060 { 00061 // add the tunable parameter 00062 m_reg = m_initReg; 00063 paramDoubleValues.push_back ( &m_reg ); 00064 paramDoubleNames.push_back ( "reg" ); 00065 00066 m_bias = m_initBias; 00067 paramDoubleValues.push_back ( &m_bias ); 00068 paramDoubleNames.push_back ( "bias" ); 00069 00070 m_scale = m_initScale; 00071 paramDoubleValues.push_back ( &m_scale ); 00072 paramDoubleNames.push_back ( "scale" ); 00073 00074 // alloc mem for weights 00075 if ( m_x == 0 ) 00076 { 00077 m_x = new REAL*[m_nCross+1]; 00078 for ( int i=0;i<m_nCross+1;i++ ) 00079 m_x[i] = new REAL[m_nFeatures * m_nClass * m_nDomain]; 00080 } 00081 }
void LinearModelNonNeg::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
input | Pointer to input (can be cross validation set, or whole training set) (rows x nFeatures) [col0;col1..] | |
target | The targets (can be cross validation targets), [row0;row1..] | |
nSamples | The sample size (rows) in input | |
crossRun | The cross validation run (for training) |
Implements StandardAlgorithm.
Definition at line 117 of file LinearModelNonNeg.cpp.
00118 { 00119 REAL* tmpTargets = new REAL[nSamples*m_nClass*m_nDomain]; 00120 REAL* x = new REAL[m_nFeatures]; 00121 00122 // set targets 00123 for ( int i=0;i<m_nClass*m_nDomain;i++ ) 00124 for ( int j=0;j<nSamples;j++ ) 00125 tmpTargets[i * nSamples + j] = target[j * m_nClass * m_nDomain + i] * m_scale + m_bias; 00126 00127 // solve the linear systems (per output class) 00128 for ( int i=0;i<m_nClass*m_nDomain;i++ ) 00129 { 00130 int nIter = solver.RidgeRegressionNonNegSinglecall ( input, tmpTargets+i*nSamples, x, nSamples, m_nFeatures, m_reg, 1e-10, m_maxNonNegSolverIterations ); 00131 00132 for ( int j=0;j<m_nFeatures;j++ ) 00133 m_x[crossRun][j*m_nClass*m_nDomain+i] = x[j]; 00134 } 00135 00136 if ( tmpTargets ) 00137 delete[] tmpTargets; 00138 tmpTargets = 0; 00139 if ( x ) 00140 delete[] x; 00141 x = 0; 00142 }
void LinearModelNonNeg::predictAllOutputs | ( | REAL * | rawInputs, | |
REAL * | outputs, | |||
uint | nSamples, | |||
uint | crossRun | |||
) | [virtual] |
Prediction for outside use, predicts outputs based on raw input values
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 91 of file LinearModelNonNeg.cpp.
00092 { 00093 for ( int i=0;i<nSamples;i++ ) 00094 { 00095 for ( int j=0;j<m_nClass*m_nDomain;j++ ) 00096 { 00097 REAL sum = 0.0; 00098 for ( int k=0;k<m_nFeatures;k++ ) 00099 { 00100 REAL x = rawInputs[i*m_nFeatures + k]; 00101 sum += x * m_x[crossRun][k*m_nClass*m_nDomain + j]; 00102 } 00103 outputs[i*m_nClass*m_nDomain + j] = ( sum - m_bias ) / m_scale; 00104 } 00105 } 00106 }
void LinearModelNonNeg::readSpecificMaps | ( | ) | [virtual] |
Read the Algorithm specific values from the description file
Implements StandardAlgorithm.
Definition at line 46 of file LinearModelNonNeg.cpp.
00047 { 00048 m_initReg = m_doubleMap["initReg"]; 00049 m_initScale = m_doubleMap["initScale"]; 00050 m_initBias = m_doubleMap["initBias"]; 00051 00052 m_maxNonNegSolverIterations = m_intMap["maxNonNegSolverIterations"]; 00053 }
void LinearModelNonNeg::saveWeights | ( | int | cross | ) | [virtual] |
Save the weights and all other parameters for load the complete prediction model
Implements StandardAlgorithm.
Definition at line 148 of file LinearModelNonNeg.cpp.
00149 { 00150 char buf[1024]; 00151 sprintf ( buf,"%02d",cross ); 00152 string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf; 00153 if ( m_inRetraining ) 00154 cout<<"Save:"<<name<<endl; 00155 fstream f ( name.c_str(), ios::out ); 00156 f.write ( ( char* ) &m_nTrain, sizeof ( int ) ); 00157 f.write ( ( char* ) &m_nFeatures, sizeof ( int ) ); 00158 f.write ( ( char* ) &m_nClass, sizeof ( int ) ); 00159 f.write ( ( char* ) &m_nDomain, sizeof ( int ) ); 00160 f.write ( ( char* ) m_x[cross], sizeof ( REAL ) *m_nFeatures*m_nClass*m_nDomain ); 00161 f.write ( ( char* ) &m_maxSwing, sizeof ( double ) ); 00162 f.write ( ( char* ) &m_reg, sizeof ( double ) ); 00163 f.write ( ( char* ) &m_bias, sizeof ( double ) ); 00164 f.write ( ( char* ) &m_scale, sizeof ( double ) ); 00165 f.close(); 00166 }
string LinearModelNonNeg::templateGenerator | ( | int | id, | |
string | preEffect, | |||
int | nameID, | |||
bool | blendStop | |||
) | [static] |
Generates a template of the description file
Definition at line 231 of file LinearModelNonNeg.cpp.
00232 { 00233 stringstream s; 00234 s<<"ALGORITHM=LinearModelNonNeg"<<endl; 00235 s<<"ID="<<id<<endl; 00236 s<<"TRAIN_ON_FULLPREDICTOR="<<preEffect<<endl; 00237 s<<"DISABLE=0"<<endl; 00238 s<<endl; 00239 s<<"[int]"<<endl; 00240 s<<"maxTuninigEpochs=20"<<endl; 00241 s<<"maxNonNegSolverIterations=1000"<<endl; 00242 s<<endl; 00243 s<<"[double]"<<endl; 00244 s<<"initMaxSwing=0.5"<<endl; 00245 s<<"initReg=0.01"<<endl; 00246 s<<"initScale=1.0"<<endl; 00247 s<<"initBias=3.0"<<endl; 00248 s<<endl; 00249 s<<"[bool]"<<endl; 00250 s<<"enableClipping="<< ( !blendStop ) <<endl; 00251 s<<"enableTuneSwing=0"<<endl; 00252 s<<endl; 00253 s<<"minimzeProbe=0"<<endl; 00254 s<<"minimzeProbeClassificationError=0"<<endl; 00255 s<<"minimzeBlend="<<blendStop<<endl; 00256 s<<"minimzeBlendClassificationError=0"<<endl; 00257 s<<endl; 00258 s<<"[string]"<<endl; 00259 s<<"weightFile="<<"LinearModelNonNeg_"<<nameID<<"_weights.dat"<<endl; 00260 s<<"fullPrediction=LinearModelNonNeg_"<<nameID<<".dat"<<endl; 00261 00262 return s.str(); 00263 }