/* backpropmlp4layers.cpp - train the waits of an mlp with four hidden layers using backprop algorithm Copyright (C) 2010 Rui Rodrigues This software is released under the terms of the GNU General Public License (http://www.gnu.org/copyleft/gpl.html). */ #include #include #include using namespace std; #include #include #include #include "gradgsl.h" #include #include #include "netdimsandfilenames.h" void checkfstream(ofstream& file_io,const char* filename); //in ../geral/checkfstream.cpp void checkfstream(ifstream& file_io,const char* filename); //in ../geral/checkfstream.cpp void checkfstream(FILE* pointer,const char* filename); void read_datafile(ifstream&in,netdimsandfilenames& A); //in netdimsandfilenames.cpp void readgslvectormatrix(FILE* readfromfile, gsl_vector* v,vector&sizes); //in iosgslvectormatrix.cpp void readgslmatriz(const char* filename,gsl_matrix*&m); //in iosgslvectormatrix.cpp void writegslmatriz(const char* filename,gsl_matrix*m); //save a vector that contains several gslmatrices void savegslvectormatrix(FILE* writetofile, gsl_vector* v,vector&sizes); //in iosgslvectormatrix.cpp int givesize_gslvector_infile(FILE* readfromfile); //in iosgslvectormatrix.cpp void useblacklist(gsl_matrix * &inputdata,const char*blacklistfile); //in blacklist.cpp //the following will be use to choose to load initial weights for rbmlogistic or backproprbm weights const int num_matrices_autoencoder=4; // ------------------------------------------------------------------- const string startperceptron="startperceptron"; const string startmlp1="startmlp1"; const string cont="cont"; const string blacklist_use="useblacklist"; // ---------------------------------------------------------------------------------------- //-----------------------CONFIGURE----------------------------------------------------- const int batchsize=500; const int numepochs=300; //----------------------------------------------------------------------------------- int main(int argc, char ** argv){ try{ if(argc<5){ cout<<" must be called with argument startperceptron startmlp1 or cont and after signal1 then signal2 and folder name. Optionaly there is an extra argument: useblacklist !"<size1!=outputdata->size1){ cout<<"number of patches for input and outputdata is not the same!"<size2!=ninputs){ cout<<"inputdata is not compatible with ninputs!"<size2!=noutputs){ cout<<"outputdata is not compatible with noutputs!"<size1; size_t numbatches=npatches/batchsize; //load weights gsl_vector * minimumweights=gsl_vector_alloc (td.tamtotal); int nrows[]={ninputs+1,nhidden0+1,nhidden1+1,nhidden2+1,nhidden3+1}; int ncols[]={nhidden0,nhidden1,nhidden2,nhidden3,noutputs}; if(a==1){ //perceptron weights file string s="../"; s.append(cc); s.append("/perceptron_"); string signal1=argv[2]; string signal2=argv[3]; s.append(signal1); s.append("_"); s.append(signal2); s.append(".txt"); //which part of weights come from which file int limits0weights=nrows[0]*ncols[0]+nrows[1]*ncols[1]; int limits1weights=nrows[2]*ncols[2]; int limits2weights=nrows[3]*ncols[3]+nrows[4]*ncols[4]; //first file gsl_vector_view aux0=gsl_vector_subvector (minimumweights, 0,limits0weights); FILE * readweights0 = fopen(A1.backpropautoencodercoefficientsfile.c_str(), "r"); checkfstream(readweights0, A1.backpropautoencodercoefficientsfile.c_str()); //read and trash first 9 ints int trash1; for(int i=0;i<9;++i) if(fscanf(readweights0,"%u",&trash1)!=1) return -1; int z=gsl_vector_fscanf (readweights0,&aux0.vector); if(z){ cout<<"problem reading weights from file "< sizes; FILE * readweights = fopen(f.c_str(), "r"); checkfstream(readweights,f.c_str()); readgslvectormatrix(readweights, minimumweights,sizes); } else if(a==3){ //load weights from autoencoders and mlp1 //mlp1 filename string mlp1="../"; mlp1.append(cc); mlp1.append("/mlp1layers_"); mlp1.append(signal1); mlp1.append("_"); mlp1.append(signal2); mlp1.append(".txt"); //which part of weights come from which file int limits0weights=nrows[0]*ncols[0]; int limits1weights=nrows[1]*ncols[1]+nrows[2]*ncols[2]; int limits2weights=nrows[3]*ncols[3]+nrows[4]*ncols[4]; //first file //debug cout<<"loading weights from "<<0<<" to "<gradient, 1e-3); } while (status == GSL_CONTINUE && iter < maxiterations); printf("epoch %4i batch %4i error rate %e \r", epoch, batch,(s->f)*(s->f)/batchsize); fflush(stdout); gsl_vector_memcpy (minimumweights,s->x); gsl_multimin_fdfminimizer_free (s); } cout< matdimensions(matricesdimensions,matricesdimensions+11); FILE * writeweights = fopen(f.c_str(), "w"); savegslvectormatrix(writeweights, minimumweights,matdimensions); fclose(writeweights); //-------------------------computer error rate after training------------ { parametersfwdgsl parameters(numhidlayers,dimensions,npatches); gsl_matrix_memcpy(&(parameters.reallayerdata[0].matrix),inputdata); double error_rate=just_compute_error_gsl_vislinear(minimumweights,¶meters,outputdata); cout<<"error rate after training is "<