30 this->useScaling = useScaling;
34 classType =
"LinearRegression";
35 regressifierType = classType;
36 debugLog.setProceedingText(
"[DEBUG LinearRegression]");
37 errorLog.setProceedingText(
"[ERROR LinearRegression]");
38 trainingLog.setProceedingText(
"[TRAINING LinearRegression]");
39 warningLog.setProceedingText(
"[WARNING LinearRegression]");
59 if( regressifier == NULL )
return false;
80 trainingResults.clear();
83 errorLog <<
"train_(RegressionData &trainingData) - Training data has zero samples!" << std::endl;
88 errorLog <<
"train_(RegressionData &trainingData) - The number of target dimensions is not 1!" << std::endl;
92 numInputDimensions = N;
93 numOutputDimensions = 1;
94 inputVectorRanges.clear();
95 targetVectorRanges.clear();
106 trainingData.
scale(inputVectorRanges,targetVectorRanges,0.0,1.0);
113 for(UINT j=0; j<N; j++){
121 bool keepTraining =
true;
123 TrainingResult result;
124 trainingResults.reserve(M);
129 for(UINT i=0; i<M; i++){
130 randomTrainingOrder[i] = i;
132 std::random_shuffle(randomTrainingOrder.begin(), randomTrainingOrder.end());
135 while( keepTraining ){
138 totalSquaredTrainingError = 0;
139 for(UINT m=0; m<M; m++){
142 UINT i = randomTrainingOrder[m];
148 for(UINT j=0; j<N; j++){
152 totalSquaredTrainingError += SQR( error );
155 for(UINT j=0; j<N; j++){
156 w[j] += learningRate * error * x[j];
158 w0 += learningRate * error;
162 delta = fabs( totalSquaredTrainingError-lastError );
163 lastError = totalSquaredTrainingError;
166 if( delta <= minChange ){
167 keepTraining =
false;
170 if( grt_isinf( totalSquaredTrainingError ) || grt_isnan( totalSquaredTrainingError ) ){
171 errorLog <<
"train_(RegressionData &trainingData) - Training failed! Total squared training error is NAN. If scaling is not enabled then you should try to scale your data and see if this solves the issue." << std::endl;
175 if( ++iter >= maxNumEpochs ){
176 keepTraining =
false;
180 rootMeanSquaredTrainingError = sqrt( totalSquaredTrainingError / Float(M) );
181 result.setRegressionResult(iter,totalSquaredTrainingError,rootMeanSquaredTrainingError,
this);
182 trainingResults.push_back( result );
185 trainingResultsObserverManager.notifyObservers( result );
187 trainingLog <<
"Epoch: " << iter <<
" SSE: " << totalSquaredTrainingError <<
" Delta: " << delta << std::endl;
191 regressionData.
resize(1,0);
199 errorLog <<
"predict_(VectorFloat &inputVector) - Model Not Trained!" << std::endl;
203 if( !trained )
return false;
205 if( inputVector.size() != numInputDimensions ){
206 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input Vector (" << int( inputVector.size() ) <<
") does not match the num features in the model (" << numInputDimensions << std::endl;
211 for(UINT n=0; n<numInputDimensions; n++){
212 inputVector[n] =
scale(inputVector[n], inputVectorRanges[n].minValue, inputVectorRanges[n].maxValue, 0, 1);
216 regressionData[0] = w0;
217 for(UINT j=0; j<numInputDimensions; j++){
218 regressionData[0] += inputVector[j] * w[j];
222 for(UINT n=0; n<numOutputDimensions; n++){
223 regressionData[n] =
scale(regressionData[n], 0, 1, targetVectorRanges[n].minValue, targetVectorRanges[n].maxValue);
234 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
239 file<<
"GRT_LINEAR_REGRESSION_MODEL_FILE_V2.0\n";
243 errorLog <<
"saveModelToFile(fstream &file) - Failed to save Regressifier base settings to file!" << std::endl;
250 for(UINT j=0; j<numInputDimensions; j++){
265 errorLog <<
"loadModelFromFile( fstream &file ) - Could not open file to load model" << std::endl;
275 if( word ==
"GRT_LINEAR_REGRESSION_MODEL_FILE_V1.0" ){
279 if( word !=
"GRT_LINEAR_REGRESSION_MODEL_FILE_V2.0" ){
280 errorLog <<
"loadModelFromFile( fstream &file ) - Could not find Model File Header" << std::endl;
286 errorLog <<
"loadModelFromFile( fstream &file ) - Failed to save Regressifier base settings to file!" << std::endl;
293 w.
resize(numInputDimensions);
297 if(word !=
"Weights:"){
298 errorLog <<
"loadModelFromFile( fstream &file ) - Could not find the Weights!" << std::endl;
303 for(UINT j=0; j<numInputDimensions; j++){
325 if(word !=
"NumFeatures:"){
326 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find NumFeatures!" << std::endl;
329 file >> numInputDimensions;
332 if(word !=
"NumOutputDimensions:"){
333 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find NumOutputDimensions!" << std::endl;
336 file >> numOutputDimensions;
339 if(word !=
"UseScaling:"){
340 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find UseScaling!" << std::endl;
348 inputVectorRanges.
resize(numInputDimensions);
349 targetVectorRanges.
resize(numOutputDimensions);
353 if(word !=
"InputVectorRanges:"){
355 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Failed to find InputVectorRanges!" << std::endl;
358 for(UINT j=0; j<inputVectorRanges.size(); j++){
359 file >> inputVectorRanges[j].minValue;
360 file >> inputVectorRanges[j].maxValue;
364 if(word !=
"OutputVectorRanges:"){
366 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Failed to find OutputVectorRanges!" << std::endl;
369 for(UINT j=0; j<targetVectorRanges.size(); j++){
370 file >> targetVectorRanges[j].minValue;
371 file >> targetVectorRanges[j].maxValue;
376 w.
resize(numInputDimensions);
380 if(word !=
"Weights:"){
381 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find the Weights!" << std::endl;
386 for(UINT j=0; j<numInputDimensions; j++){
392 regressionData.
resize(1,0);
This class implements the Linear Regression algorithm. Linear Regression is a simple but effective re...
Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)
virtual bool saveModelToFile(std::fstream &file) const
virtual bool predict_(VectorFloat &inputVector)
LinearRegression(bool useScaling=false)
Vector< MinMax > getInputRanges() const
virtual bool resize(const unsigned int size)
virtual ~LinearRegression(void)
bool copyBaseVariables(const Regressifier *regressifier)
UINT getNumInputDimensions() const
Vector< MinMax > getTargetRanges() const
bool saveBaseSettingsToFile(std::fstream &file) const
bool scale(const Float minTarget, const Float maxTarget)
UINT getMaxNumEpochs() const
UINT getNumTargetDimensions() const
std::string getRegressifierType() const
bool loadBaseSettingsFromFile(std::fstream &file)
LinearRegression & operator=(const LinearRegression &rhs)
bool setMaxNumIterations(const UINT maxNumIterations)
virtual bool train_(RegressionData &trainingData)
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
virtual bool deepCopyFrom(const Regressifier *regressifier)
bool loadLegacyModelFromFile(std::fstream &file)
bool setMaxNumEpochs(const UINT maxNumEpochs)
UINT getMaxNumIterations() const
virtual bool loadModelFromFile(std::fstream &file)
UINT getNumSamples() const