/* backpropautoencodergsl.cpp - train the waits of an autoencoder 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 #include #include #include using namespace std; #include #include #include "gradgsl.h" #include "netdimsandfilenames.h" double initializeseed(); //in random_matrix.cpp 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 read_datafile(ifstream&in,netdimsandfilenames& A); //in netdimsandfilenames.cpp void readgslvectormatrix(FILE* readfromfile, gsl_vector* v,vector&sizes); //in iosgslvectormatrix.cpp //save a vector that contains several gslmatrices void savegslvectormatrix(FILE* writetofile, gsl_vector* v,vector&sizes); //in iosgslvectormatrix.cpp void readgslmatriz(const char* filename,gsl_matrix*&m); //in iogslvectormatrix.cpp void writegslmatriz(const char* filename,gsl_matrix*m); //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 initial="initial";//from 'autoencoder_allcoefficientsgsl' const string backprop="backprop";//backproprbm struct weightsfile{ string filename; int num_matrices; int * whichmatrices; string matrixtype;//"initial" or "backprop" //informs if we take the weights from the file produced by 'prepareautoencoderforbackprop' or //by backproprbm }; void fill_vector_weights(gsl_vector* weights,int weights_size,vector& weightsfilev, int*nrows, int * ncols); //down in this file // ------------------------------------------------------------------- const string start="start"; const string cont="cont"; const string blacklist_use="useblacklist"; // ---------------------------------------------------------------------------------------- //-----------------------CONFIGURE----------------------------------------------------- const int batchsize=504; const int numepochs=50; //----------------------------------------------------------------------------------- int main(int argc, char ** argv){ try{ if(argc<4){ cout<<" must be called with argument start, startlogistic cont and after signal name and folder name . Optionaly thereis an extra argument: useblacklist!"<size2!=ninputs){ cout<<"inputdata is not compatible with ninputs!"<size1; int numbatches=(int) npatches/batchsize; //load weights gsl_vector * minimumweights=gsl_vector_alloc (td.tamtotal); if(a==1){ vector sizes; FILE * readweights = fopen(A.autoencoderallcoeficientsgsl.c_str(), "r"); readgslvectormatrix(readweights, minimumweights,sizes); } else if(a==2){ vector sizes; FILE * readweights = fopen(A.backpropautoencodercoefficientsfile.c_str(), "r"); readgslvectormatrix(readweights, minimumweights,sizes); } else if(a==3){ //const int num_matrices_autoencoder=4; //in the begining of this file int nrows[]={ninputs+1,A.nhidden0+1,A.nhidden1+1,A.nhidden0+1}; int ncols[]={A.nhidden0,A.nhidden1,A.nhidden0,ninputs}; //which weights matrix load from each file weightsfile wf1,wf2; //wf1 wf1.filename= A.autoencoderallcoeficientsgsl; wf1.num_matrices=2; int matrices1[]={0,3};//these numbers refer to the matrices in the //new autoencoder and in that one whose coefficients are in 'A.autoencoderallcoeficients' wf1.whichmatrices=matrices1; wf1.matrixtype="initial"; //wf2 wf2.filename=A.backprop_rbmlogisticweightsfile; wf2.num_matrices=2; int matrices2[]={1,2}; wf2.whichmatrices=matrices2; wf2.matrixtype="backprop"; vector weightsfvector(2); weightsfvector[0]=&wf1; weightsfvector[1]=&wf2; int weights_size=0; for(int i=0;igradient, 1e-3); //printf ("%5d %10.5f\n", iter,s->f); } while (status == GSL_CONTINUE && iter < maxiterations); printf("epoch %4i batch %4i error rate by patch %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+9); FILE * writeweights = fopen(A.backpropautoencodercoefficientsfile.c_str(), "w"); savegslvectormatrix(writeweights, minimumweights,matdimensions); fclose(writeweights); //-------------------computer error rate after training------------ { //it could/should be like we did before training parametersgradgsl parameters(numhidlayers,dimensions,npatches); parameters.batchoutputdata=data; gsl_matrix * inputdata=gsl_matrix_calloc(npatches,ninputs+1); v=gsl_matrix_column (inputdata,ninputs); gsl_vector_add_constant (&v.vector, 1.0); gsl_matrix_view realdata0=gsl_matrix_submatrix(inputdata,0,0,npatches,ninputs); gsl_matrix_memcpy(&realdata0.matrix,data); parameters.layerdata[0]=inputdata; double error_rate=compute_error_gsl_vislinear(minimumweights,¶meters); cout<<"error rate after training is "<& weightsfilev, int*nrows, int * ncols){ int numfiles=weightsfilev.size(); for(int i=0;imatrixtype.compare(initial)==0){ //load coefficients from this file into vector gslvecaux FILE * readweights = fopen(weightsfilev[i]->filename.c_str(), "r"); int tam=givesize_gslvector_infile(readweights); rewind (readweights); gsl_vector*gslvecaux=gsl_vector_calloc(tam); vector sizes(num_matrices_autoencoder); readgslvectormatrix(readweights, gslvecaux, sizes); //insert those we wnt into vector weights for(int j=0;jnum_matrices;++j){ int matrixorder=weightsfilev[i]->whichmatrices[j]; int begin=0; for(int k=0;k>a; } } for(int n=0;nmatrixtype.compare(backprop)==0){ //load coefficients into vector gslvecaux FILE * readweights = fopen(weightsfilev[i]->filename.c_str(), "r"); int tam=givesize_gslvector_infile(readweights); rewind (readweights); gsl_vector*gslvecaux=gsl_vector_calloc(tam); vector sizes(weightsfilev[i]->num_matrices); readgslvectormatrix(readweights, gslvecaux, sizes); //insert those coefficients in the appropriate place in vector weights for(int j=0;jnum_matrices;++j){ int matrixorder=weightsfilev[i]->whichmatrices[j]; int beginweights=0; for(int k=0;kwhichmatrices[z]; beginaux+=nrows[order]*ncols[order]; } for(int n=0;n