00001 #include "LinearModelNonNeg.h"
00002
00003 extern StreamOutput cout;
00004
00008 LinearModelNonNeg::LinearModelNonNeg()
00009 {
00010 cout<<"LinearModelNonNeg"<<endl;
00011
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 }
00021
00025 LinearModelNonNeg::~LinearModelNonNeg()
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 }
00041
00046 void LinearModelNonNeg::readSpecificMaps()
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 }
00054
00059 void LinearModelNonNeg::modelInit()
00060 {
00061
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
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 }
00082
00091 void LinearModelNonNeg::predictAllOutputs ( REAL* rawInputs, REAL* outputs, uint nSamples, uint crossRun )
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 }
00107
00117 void LinearModelNonNeg::modelUpdate ( REAL* input, REAL* target, uint nSamples, uint crossRun )
00118 {
00119 REAL* tmpTargets = new REAL[nSamples*m_nClass*m_nDomain];
00120 REAL* x = new REAL[m_nFeatures];
00121
00122
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
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 }
00143
00148 void LinearModelNonNeg::saveWeights ( int cross )
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 }
00167
00172 void LinearModelNonNeg::loadWeights ( int cross )
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 }
00198
00202 void LinearModelNonNeg::loadMetaWeights ( int cross )
00203 {
00204 char buf[1024];
00205 sprintf ( buf,"%02d",cross );
00206 string name = m_datasetPath + "/" + m_tempPath + "/" + m_weightFile + "." + buf;
00207 cout<<"LoadMeta:"<<name<<endl;
00208 fstream f ( name.c_str(), ios::in );
00209 if ( f.is_open() == false )
00210 assert ( false );
00211 f.read ( ( char* ) &m_nTrain, sizeof ( int ) );
00212 f.read ( ( char* ) &m_nFeatures, sizeof ( int ) );
00213 f.read ( ( char* ) &m_nClass, sizeof ( int ) );
00214 f.read ( ( char* ) &m_nDomain, sizeof ( int ) );
00215
00216 REAL* tmp = new REAL[m_nFeatures*m_nClass*m_nDomain];
00217 f.read ( ( char* ) tmp, sizeof ( REAL ) *m_nFeatures*m_nClass*m_nDomain );
00218 delete[] tmp;
00219 f.read ( ( char* ) &m_maxSwing, sizeof ( double ) );
00220 f.read ( ( char* ) &m_reg, sizeof ( double ) );
00221 f.read ( ( char* ) &m_bias, sizeof ( double ) );
00222 f.read ( ( char* ) &m_scale, sizeof ( double ) );
00223 f.close();
00224 }
00225
00231 string LinearModelNonNeg::templateGenerator ( int id, string preEffect, int nameID, bool blendStop )
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 }