30 this->useScaling = useScaling;
34 classType =
"LogisticRegression";
35 regressifierType = classType;
36 debugLog.setProceedingText(
"[DEBUG LogisticRegression]");
37 errorLog.setProceedingText(
"[ERROR LogisticRegression]");
38 trainingLog.setProceedingText(
"[TRAINING LogisticRegression]");
39 warningLog.setProceedingText(
"[WARNING LogisticRegression]");
59 if( regressifier == NULL )
return false;
79 trainingResults.clear();
82 errorLog <<
"train_(RegressionData trainingData) - Training data has zero samples!" << std::endl;
87 errorLog <<
"train_(RegressionData trainingData) - The number of target dimensions is not 1!" << std::endl;
91 numInputDimensions = N;
92 numOutputDimensions = 1;
93 inputVectorRanges.clear();
94 targetVectorRanges.clear();
105 trainingData.
scale(inputVectorRanges,targetVectorRanges,0.0,1.0);
112 for(UINT j=0; j<N; j++){
117 Float lastSquaredError = 0;
120 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++){
151 error = y[0] - sigmoid( h );
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-lastSquaredError );
163 lastSquaredError = totalSquaredTrainingError;
166 if( delta <= minChange ){
167 keepTraining =
false;
170 if( ++iter >= maxNumEpochs ){
171 keepTraining =
false;
174 if( grt_isinf( totalSquaredTrainingError ) || grt_isnan( totalSquaredTrainingError ) ){
175 errorLog <<
"train_(RegressionData &trainingData) - Training failed! Total squared 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;
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.
getSize() != numInputDimensions ){
206 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input Vector (" << inputVector.
getSize() <<
") does not match the num features in the model (" << numInputDimensions << std::endl;
211 for(UINT n=0; n<numInputDimensions; n++){
212 inputVector[n] = grt_scale(inputVector[n], inputVectorRanges[n].minValue, inputVectorRanges[n].maxValue, 0.0, 1.0);
216 regressionData[0] =
w0;
217 for(UINT j=0; j<numInputDimensions; j++){
218 regressionData[0] += inputVector[j] *
w[j];
220 Float sum = regressionData[0];
221 regressionData[0] = sigmoid( regressionData[0] );
222 std::cout <<
"reg sum: " << sum <<
" sig: " << regressionData[0] << std::endl;
224 for(UINT n=0; n<numOutputDimensions; n++){
225 regressionData[n] = grt_scale(regressionData[n], 0.0, 1.0, targetVectorRanges[n].minValue, targetVectorRanges[n].maxValue);
236 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
241 file<<
"GRT_LOGISTIC_REGRESSION_MODEL_FILE_V2.0\n";
245 errorLog <<
"saveModelToFile(fstream &file) - Failed to save Regressifier base settings to file!" << std::endl;
252 for(UINT j=0; j<numInputDimensions; j++){
264 numInputDimensions = 0;
270 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << std::endl;
280 if( word ==
"GRT_LOGISTIC_REGRESSION_MODEL_FILE_V1.0" ){
284 if( word !=
"GRT_LOGISTIC_REGRESSION_MODEL_FILE_V2.0" ){
285 errorLog <<
"loadModelFromFile( fstream &file ) - Could not find Model File Header" << std::endl;
291 errorLog <<
"loadModelFromFile( fstream &file ) - Failed to save Regressifier base settings to file!" << std::endl;
302 if(word !=
"Weights:"){
303 errorLog <<
"loadModelFromFile( fstream &file ) - Could not find the Weights!" << std::endl;
308 for(UINT j=0; j<numInputDimensions; j++){
325 Float LogisticRegression::sigmoid(
const Float x)
const{
326 return 1.0 / (1 + exp(-x));
334 if(word !=
"NumFeatures:"){
335 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find NumFeatures!" << std::endl;
338 file >> numInputDimensions;
341 if(word !=
"NumOutputDimensions:"){
342 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find NumOutputDimensions!" << std::endl;
345 file >> numOutputDimensions;
348 if(word !=
"UseScaling:"){
349 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find UseScaling!" << std::endl;
357 inputVectorRanges.
resize(numInputDimensions);
358 targetVectorRanges.
resize(numOutputDimensions);
362 if(word !=
"InputVectorRanges:"){
364 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Failed to find InputVectorRanges!" << std::endl;
367 for(UINT j=0; j<inputVectorRanges.
getSize(); j++){
368 file >> inputVectorRanges[j].minValue;
369 file >> inputVectorRanges[j].maxValue;
373 if(word !=
"OutputVectorRanges:"){
375 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Failed to find OutputVectorRanges!" << std::endl;
378 for(UINT j=0; j<targetVectorRanges.
getSize(); j++){
379 file >> targetVectorRanges[j].minValue;
380 file >> targetVectorRanges[j].maxValue;
389 if(word !=
"Weights:"){
390 errorLog <<
"loadLegacyModelFromFile( fstream &file ) - Could not find the Weights!" << std::endl;
395 for(UINT j=0; j<numInputDimensions; j++){
401 regressionData.
resize(1,0);
UINT getMaxNumIterations() const
LogisticRegression(const bool useScaling=true)
Vector< MinMax > getInputRanges() const
virtual bool predict_(VectorFloat &inputVector)
virtual bool resize(const unsigned int size)
LogisticRegression & operator=(const LogisticRegression &rhs)
bool copyBaseVariables(const Regressifier *regressifier)
This class implements the Logistic Regression algorithm. Logistic Regression is a simple but effectiv...
UINT getNumInputDimensions() const
unsigned int getSize() const
virtual bool train_(RegressionData &trainingData)
virtual ~LogisticRegression(void)
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 loadLegacyModelFromFile(std::fstream &file)
bool loadBaseSettingsFromFile(std::fstream &file)
VectorFloat w
The weights vector.
bool setMaxNumIterations(UINT maxNumIterations)
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
virtual bool deepCopyFrom(const Regressifier *regressifier)
virtual bool loadModelFromFile(std::fstream &file)
bool setMaxNumEpochs(const UINT maxNumEpochs)
virtual bool saveModelToFile(std::fstream &file) const
UINT getNumSamples() const