int main (int argc, const char * argv[]) { //Parse the data filename from the argument list if( argc != 2 ){ cout << "Error: failed to parse data filename from command line. You should run this example with one argument pointing to the data filename!\n"; return EXIT_FAILURE; } const string filename = argv[1]; //Create a new Softmax instance Softmax softmax; //Load some training data to train the classifier ClassificationData trainingData; if( !trainingData.load( filename ) ){ cout << "Failed to load training data: " << filename << endl; return EXIT_FAILURE; } //Use 20% of the training dataset to create a test dataset ClassificationData testData = trainingData.split( 80 ); //Train the classifier if( !softmax.train( trainingData ) ){ cout << "Failed to train classifier!\n"; return EXIT_FAILURE; } //Save the Softmax model to a file if( !softmax.save("SoftmaxModel.grt") ){ cout << "Failed to save the classifier model!\n"; return EXIT_FAILURE; } //Load the Softmax model from a file if( !softmax.load("SoftmaxModel.grt") ){ cout << "Failed to load the classifier model!\n"; return EXIT_FAILURE; } //Use the test dataset to test the softmax model double accuracy = 0; for(UINT i=0; i<testData.getNumSamples(); i++){ //Get the i'th test sample UINT classLabel = testData[i].getClassLabel(); VectorFloat inputVector = testData[i].getSample(); //Perform a prediction using the classifier if( !softmax.predict( inputVector ) ){ cout << "Failed to perform prediction for test sample: " << i <<"\n"; return EXIT_FAILURE; } //Get the predicted class label UINT predictedClassLabel = softmax.getPredictedClassLabel(); VectorFloat classLikelihoods = softmax.getClassLikelihoods(); VectorFloat classDistances = softmax.getClassDistances(); //Update the accuracy if( classLabel == predictedClassLabel ) accuracy++; cout << "TestSample: " << i << " ClassLabel: " << classLabel << " PredictedClassLabel: " << predictedClassLabel << endl; } cout << "Test Accuracy: " << accuracy/double(testData.getNumSamples())*100.0 << "%" << endl; return EXIT_SUCCESS; }
int main (int argc, const char * argv[]) { //Create a new Softmax instance Softmax softmax; //Load some training data to train the classifier ClassificationData trainingData; if( !trainingData.loadDatasetFromFile("SoftmaxTrainingData.txt") ){ cout << "Failed to load training data!\n"; return EXIT_FAILURE; } //Use 20% of the training dataset to create a test dataset ClassificationData testData = trainingData.partition( 80 ); //Train the classifier if( !softmax.train( trainingData ) ){ cout << "Failed to train classifier!\n"; return EXIT_FAILURE; } //Save the Softmax model to a file if( !softmax.saveModelToFile("SoftmaxModel.txt") ){ cout << "Failed to save the classifier model!\n"; return EXIT_FAILURE; } //Load the Softmax model from a file if( !softmax.loadModelFromFile("SoftmaxModel.txt") ){ cout << "Failed to load the classifier model!\n"; return EXIT_FAILURE; } //Use the test dataset to test the softmax model double accuracy = 0; for(UINT i=0; i<testData.getNumSamples(); i++){ //Get the i'th test sample UINT classLabel = testData[i].getClassLabel(); vector< double > inputVector = testData[i].getSample(); //Perform a prediction using the classifier if( !softmax.predict( inputVector ) ){ cout << "Failed to perform prediction for test sample: " << i <<"\n"; return EXIT_FAILURE; } //Get the predicted class label UINT predictedClassLabel = softmax.getPredictedClassLabel(); vector< double > classLikelihoods = softmax.getClassLikelihoods(); vector< double > classDistances = softmax.getClassDistances(); //Update the accuracy if( classLabel == predictedClassLabel ) accuracy++; cout << "TestSample: " << i << " ClassLabel: " << classLabel << " PredictedClassLabel: " << predictedClassLabel << endl; } cout << "Test Accuracy: " << accuracy/double(testData.getNumSamples())*100.0 << "%" << endl; return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { Config config; Layer l(config); FileReader fr(config.train_file, config.batchSize); Softmax softmax; softmax.initSoftmax(config.outputVocabSize, config.hiddenSize, config.batchSize, &l); l.softmax = &softmax; type prevPerplexity = 10000000; if(config.loadWeights) { l.loadWeights("Layer_Weights.txt"); softmax.loadWeights("Softmax_Weights.txt"); if(config.verbose) { softmax.printMatrices(); l.printMatrices(); } } if(config.trainModel) { for(int i = 0; i < config.numPasses; i++) { std::chrono::time_point<std::chrono::system_clock> start, end, batchStart, batchEnd; start = std::chrono::system_clock::now(); std::cout << "PASS NUMBER: " << i << std::endl; fr.resetFile(); for(int j = 0; j < config.numTrainBatches; j++) { if( j % 10 == 0){ batchEnd = std::chrono::system_clock::now(); std::chrono::duration<type> elapsed_seconds = batchEnd-batchStart; printf("Time Taken %f \n", elapsed_seconds.count()); std::cout << "Batch number: " << j << std::endl; batchStart = std::chrono::system_clock::now(); } fr.readNextBatch(); l.initLSTMVector(fr); l.forwardPropagation(fr.inputMiniBatch); l.backwardPropagation(fr.outputMiniBatch); if(config.checkGradients){ l.checkGradients(config.ep); softmax.checkGradients(config.ep); } softmax.applyGradients(); l.applyGradients(); l.clearRunningErrors(); l.clearGradients(); softmax.clearGradients(); if( j % 100 == 0 && j != 0){ if( config.getPerplexity) { type perplexity = 0.0; int numWords = 0; FileReader p_fr(config.test_file, config.batchSize); for (int i = 0; i < config.numTestBatches; i++){ p_fr.readNextBatch(); l.initLSTMVector(p_fr); perplexity += l.getError(); l.clearRunningErrors(); l.clearGradients(); softmax.clearGradients(); numWords +=p_fr.numWordsInBatch; } perplexity = perplexity/std::log(2.0); //Change to base 2 log perplexity = std::pow(2,-1*perplexity/numWords); std::cout << "PERPLEXITY: " << perplexity << std::endl; } } } end = std::chrono::system_clock::now(); std::chrono::duration<type> elapsed_seconds = end-start; std::time_t end_time = std::chrono::system_clock::to_time_t(end); std::cout << "Finished Computing 1 Pass in " << elapsed_seconds.count() << "s\n"; l.dumpMatrices(); softmax.dumpMatrices(); } if(config.verbose) { l.printMatrices(); softmax.printMatrices(); } } return 0; }