bool Softmax::train_(ClassificationData &trainingData){ //Clear any previous model clear(); const unsigned int M = trainingData.getNumSamples(); const unsigned int N = trainingData.getNumDimensions(); const unsigned int K = trainingData.getNumClasses(); if( M == 0 ){ errorLog << "train_(ClassificationData &labelledTrainingData) - Training data has zero samples!" << std::endl; return false; } numInputDimensions = N; numClasses = K; models.resize(K); classLabels.resize(K); ranges = trainingData.getRanges(); //Scale the training data if needed if( useScaling ){ //Scale the training data between 0 and 1 trainingData.scale(0, 1); } //Train a regression model for each class in the training data for(UINT k=0; k<numClasses; k++){ //Set the class label classLabels[k] = trainingData.getClassTracker()[k].classLabel; //Train the model if( !trainSoftmaxModel(classLabels[k],models[k],trainingData) ){ errorLog << "train(ClassificationData labelledTrainingData) - Failed to train model for class: " << classLabels[k] << std::endl; return false; } } //Flag that the algorithm has been trained trained = true; return trained; }
bool Softmax::train_(ClassificationData &trainingData){ //Clear any previous model clear(); const unsigned int M = trainingData.getNumSamples(); const unsigned int N = trainingData.getNumDimensions(); const unsigned int K = trainingData.getNumClasses(); if( M == 0 ){ errorLog << __GRT_LOG__ << " Training data has zero samples!" << std::endl; return false; } numInputDimensions = N; numOutputDimensions = K; numClasses = K; models.resize(K); classLabels.resize(K); ranges = trainingData.getRanges(); ClassificationData validationData; //Scale the training data if needed if( useScaling ){ //Scale the training data between 0 and 1 trainingData.scale(0, 1); } if( useValidationSet ){ validationData = trainingData.split( 100-validationSetSize ); } //Train a regression model for each class in the training data for(UINT k=0; k<numClasses; k++){ //Set the class label classLabels[k] = trainingData.getClassTracker()[k].classLabel; //Train the model if( !trainSoftmaxModel(classLabels[k],models[k],trainingData) ){ errorLog << __GRT_LOG__ << " Failed to train model for class: " << classLabels[k] << std::endl; return false; } } //Flag that the models have been trained trained = true; converged = true; //Compute the final training stats trainingSetAccuracy = 0; validationSetAccuracy = 0; //If scaling was on, then the data will already be scaled, so turn it off temporially so we can test the model accuracy bool scalingState = useScaling; useScaling = false; if( !computeAccuracy( trainingData, trainingSetAccuracy ) ){ trained = false; converged = false; errorLog << __GRT_LOG__ << " Failed to compute training set accuracy! Failed to fully train model!" << std::endl; return false; } if( useValidationSet ){ if( !computeAccuracy( validationData, validationSetAccuracy ) ){ trained = false; converged = false; errorLog << __GRT_LOG__ << " Failed to compute validation set accuracy! Failed to fully train model!" << std::endl; return false; } } trainingLog << "Training set accuracy: " << trainingSetAccuracy << std::endl; if( useValidationSet ){ trainingLog << "Validation set accuracy: " << validationSetAccuracy << std::endl; } //Reset the scaling state for future prediction useScaling = scalingState; return trained; }