testLDA.cc

  1 const char *help = "\
  2 testLDA (c) Sebastien Marcel 2003\n\
  3 \n\
  4 This program projects data into LDA sub-space\n";
  5 
  6 #include "FileBinDataSet.h"
  7 #include "LDATrainer.h"
  8 #include "FileListCmdOption.h"
  9 #include "CmdLine.h"
 10 
 11 using namespace Torch;
 12 			
 13 int main(int argc, char **argv)
 14 {
 15   	int n_inputs;
 16   	char *model_file;
 17 	real variance;
 18 	bool verbose;
 19 	
 20   	Allocator *allocator = new Allocator;
 21   	DiskXFile::setLittleEndianMode();
 22 
 23 
 24 	
 25   	//=================== The command-line ==========================
 26 	FileListCmdOption filelist("file name", "the list files or one data file");
 27         filelist.isArgument(true);
 28 
 29   	// Construct the command line
 30   	CmdLine cmd;
 31 	//cmd.setBOption("write log", false);
 32 	
 33   	// Put the help line at the beginning
 34   	cmd.info(help);
 35 
 36   	cmd.addText("\nArguments:");
 37   	cmd.addSCmdArg("model_file", &model_file, "model file");
 38   	cmd.addCmdOption(&filelist);
 39   	cmd.addICmdArg("n_inputs", &n_inputs, "input dimension of the data");
 40   	cmd.addText("\nOptions:");
 41   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 42   	cmd.addRCmdOption("-variance", &variance, -1.0, "variance");
 43 
 44   	// Read the command line
 45   	cmd.read(argc, argv);
 46 
 47 
 48 	
 49 	//
 50 	if(verbose)
 51 	{
 52 		print(" + n_filenames = %d\n", filelist.n_files);
 53 		for(int i = 0 ; i < filelist.n_files ; i++)
 54 			print("   filename[%d] = %s\n", i, filelist.file_names[i]);
 55 	}
 56 
 57 
 58   	//
 59 	// The LDA Machine
 60 	LDAMachine *lda_machine = NULL;
 61 	lda_machine = new(allocator) LDAMachine(n_inputs);
 62 
 63 
 64 	//
 65 	print("Loading LDA model: %s ...\n", model_file);
 66 
 67 	DiskXFile *file = NULL;
 68 	file = new DiskXFile(model_file, "r");
 69 	lda_machine->loadXFile(file);
 70 	delete file;
 71 	   
 72 	//
 73 	lda_machine->setIOption("verbose_level", 2);
 74 	lda_machine->setROption("variance", variance);
 75 	lda_machine->init();
 76 
 77 	
 78 	//
 79 	// Load all the data in the same dataset
 80     	FileBinDataSet *bindata = new(allocator) FileBinDataSet(filelist.file_names, filelist.n_files, n_inputs);
 81 	bindata->info(false);	
 82 
 83 
 84 	//
 85 	// Projects data into LDA sub-space
 86 	real *realinput = NULL;
 87         Sequence *seq;
 88 
 89         realinput = new real [n_inputs];
 90         seq = new Sequence(&realinput, 1, n_inputs);
 91 	
 92 	for(int i=0; i< bindata->n_examples; i++)
 93 	{
 94 		if(verbose) 
 95 	   		print("[%d]:\n", i);
 96 
 97 		//
 98 		bindata->setExample(i);
 99 		
100 		if(verbose) 
101 			print(" Input =   [%2.3f %2.3f %2.3f ...]\n", bindata->inputs->frames[0][0], bindata->inputs->frames[0][1], bindata->inputs->frames[0][2]);
102 
103 		//
104 		bindata->inputs->copyTo(realinput);
105 
106 		if(verbose) 
107 			print(" Seq =     [%2.3f %2.3f %2.3f ...]\n", realinput[0], realinput[1], realinput[2]);
108 
109 		//
110 		lda_machine->forward(seq);
111 			
112 		if(verbose) 
113 			print(" Output =   [%2.3f %2.3f %2.3f ...]\n", lda_machine->outputs->frames[0][0], lda_machine->outputs->frames[0][1], lda_machine->outputs->frames[0][2]);
114 	}
115 
116 	//
117 	delete [] realinput;
118 	delete seq;
119   	delete allocator;
120 
121   	return(0);
122 }