main_train.cpp

00001 
00002 /*
00003     This example reads the entry data, sets the SOM parameters, trains the net, prints the training quality and the elapsed time for training, and then saves the net's map.
00004 
00005         
00006         This program takes as parameters:
00007                 - the path to a file of type txt (e.g.: \data\IrisInputTrain.txt);
00008                 - the type of learning (e.g.: 'batch' or 'online');
00009                 - the two dimensions of the SOM (e.g.: 10 15);
00010                 - the number of iterations (e.g.: 1000)
00011                 - the name of the output file where the net will be saved
00012                 
00013         ./somcode <file path> <learning type> <dimension one> <dimension two> <numIte> <output file name>
00014 
00015         Example of how to execute this program: ./somcode ./data/IrisInputTrain.txt batch 10 15 1000 map10_15.txt       
00016 
00017         This is just an example. The values of the parameters must be adjusted to each situation.
00018         
00019 */
00020 
00021 
00022 
00023 #pragma warning(disable: 4786)
00024 
00025 #include <cstdlib>
00026 #include <iostream>
00027 #include <vector>
00028 #include <string>
00029 
00030 //inclusion of the libraries from somcode needed to compile this example correctly
00031 
00032 #include "RepositorySOMDataXML.h"
00033 #include "RepositoryMapcodeXML.h"
00034 #include "RepositorySOMDataFile.h"
00035 #include "RepositoryMapcodeFile.h"
00036 
00037 
00038 
00039 #include "Som.h"
00040 #include "SOMDataCadastre.h"
00041 #include "MapcodeCadastre.h"
00042 
00043 #include "GraphSegmentation.h"
00044 #include "CDbw.h"
00045 #include "DaviesBouldin.h"
00046 
00047 #include "BatchFactory.h"
00048 #include "TwoDFactory.h"
00049 #include "OnlineFactory.h"
00050 
00051 
00052 using std::cout;
00053 
00054 //creates the object constructors from abstract factory pattern
00055 
00056 static OnlineFactory fact_online("Online");
00057 static BatchFactory fact_batch("Batch");
00058 static TwoDFactory fact_twoD("Two");
00059 
00060 
00061 
00062 int main(int argc, char *argv[]) {  
00063 
00064   try {
00065     
00066           
00067           clock_t c0 = clock();
00068   
00069           //abstract class used as interface to the different ways to access data used for training.
00070           ISOMDataRepository *repD;  
00071 
00072           //concrete class used to isolate data from storing details.
00073           SOMDataCadastre *cadD;
00074 
00075           //attribution of the parameter which contains the name of the file that will be read.
00076           std::vector<std::string> params;
00077           params.push_back(argv[1]);    
00078 
00079           //creates an object of the concrete class that implements the way to access data through file systems
00080           repD = new RepositorySOMDataFile();
00081           repD->load(params,0);
00082 
00083            //instantiates an object of the concrete class SOMDataCadastre, which is used to isolate data from storing details
00084           cadD = new SOMDataCadastre(*repD);
00085 
00086           cout << "Starting SOMCode-1.0 ...\n\n";
00087  
00088           //reads the file through the concrete class SOMDataCadastre
00089           SOMData* data = cadD->load( std::vector<std::string>() );
00090 
00091           clock_t c1 = clock();
00092   
00093          
00094           clock_t c2 = clock();
00095 
00104           int dimL, dimC, numIte; 
00105           dimL = strtoint(argv[3]);
00106           dimC = strtoint(argv[4]);
00107           numIte = strtoint(argv[5]);
00108 
00109           //instantiates the object 'net', of type NetParams, which represents the net. 
00110           NetParams* net = new NetParams();
00111 
00112           //instantiates the object 'map', of type Mapcode, which represents the net's map
00113           Mapcode* map = new Mapcode();
00114 
00115 
00129           net->setData(data);
00130           net->setNumIterations( numIte );
00131           net->setInitType( LINEAR );
00132           net->setInitLearningRate( 0.5 );
00133           net->setInitNeighbor( 15 );
00134 
00135           if ( strcmp(argv[2],"online") == 0 ){
00136              net->setLearningType( ONLINE );
00137              net->setLearnType( "Online" );
00138           }
00139 
00140           if ( strcmp(argv[2],"batch") == 0 ) {
00141             net->setLearningType( BATCH );
00142             net->setLearnType( "Batch" );
00143           }
00144 
00145           net->setMapcode(map);
00146 
00147 
00148           /*
00149                Map's variables initialization
00150                
00151                setNumVar - sets the dimension of the entry vectors, i.e, the number of variables
00152                setDimensions - sets the size to a certain net dimension
00153                setLattice - sets the type of spatial arrangement
00154                setNeighborType - sets the type of neighborhood
00155                setTopolParamsText - sets the type of topology related to the number of dimensions
00156                setDimension - sets the number of dimensions
00157           */
00158 
00159           net->getMapcode()->setNumVar( data->getDimension() );
00160           net->getMapcode()->setDimensions( 0, dimL );
00161           net->getMapcode()->setDimensions( 1, dimC );
00162           net->getMapcode()->setLattice( "hexa" );      
00163           net->getMapcode()->setNeighborType( "gaussian" );
00164           net->getMapcode()->setTopolParamsText( Default_DimensionType );
00165           net->getMapcode()->setDimension(2);
00166   
00167           //instantiates the object mysom, of type SOM, which represents the SOM neural network
00168           SOM* mysom = new SOM(*net);
00169 
00170 
00171           /*
00172                Calls the SOM methods:
00173                - InitMapcode - initializes the map;
00174                - Learning - trains the net;
00175                - Quality - calculates the learning quality indexes;
00176                - Labeling -> labels the code vectors of the map;
00177           */
00178 
00179         
00180           mysom->InitMapcode();         
00181           mysom->Learning();            
00182           mysom->Quality();
00183           mysom->Labeling(); 
00184         
00185           clock_t c3 = clock();
00186         
00187             
00188 
00189           cout << "Quantization error = " << mysom->getQuantizationError() << std::endl;
00190           cout << "Topological error = " << mysom->getTopologicalError() << std::endl;
00191           cout << "Time elapsed Loading Data= " << (float) (c1 - c0)/CLOCKS_PER_SEC << std::endl;
00192           cout << "Time elapsed Computing Data = " << (float) (c3 - c2)/CLOCKS_PER_SEC << std::endl;
00193 
00194                 
00195           //saves the net's map in a file with the name passed as the last parameter 
00196           RepositoryMapcodeFile repMapcode;
00197           repMapcode.save(mysom->getMapcode(),argv[6]);
00198 
00199                  
00200     
00201          delete data;
00202          delete mysom;
00203 
00204   } catch(std::exception& ex) {
00205     cout << ex.what() << std::endl;
00206   }
00207       
00208   return EXIT_SUCCESS;
00209   
00210 };
00211 

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3