/* Copyright (c) 2008-2011 Octasic Inc. Written by Jean-Marc Valin */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "mlp_train.h" #include #include #include #include #include #include #include int stopped = 0; void handler(int sig) { stopped = 1; signal(sig, handler); } MLPTrain * mlp_init(int *topo, int nbLayers, float *inputs, float *outputs, int nbSamples) { int i, j, k; MLPTrain *net; int inDim, outDim; net = malloc(sizeof(*net)); net->topo = malloc(nbLayers*sizeof(net->topo[0])); for (i=0;itopo[i] = topo[i]; inDim = topo[0]; outDim = topo[nbLayers-1]; net->in_rate = malloc((inDim+1)*sizeof(net->in_rate[0])); net->weights = malloc((nbLayers-1)*sizeof(net->weights)); net->best_weights = malloc((nbLayers-1)*sizeof(net->weights)); for (i=0;iweights[i] = malloc((topo[i]+1)*topo[i+1]*sizeof(net->weights[0][0])); net->best_weights[i] = malloc((topo[i]+1)*topo[i+1]*sizeof(net->weights[0][0])); } double inMean[inDim]; for (j=0;jin_rate[1+j] = .5/(.0001+std); std = std-inMean[j]*inMean[j]; if (std<.001) std = .001; std = 1/sqrt(inDim*std); for (k=0;kweights[0][k*(topo[0]+1)+j+1] = randn(std); } net->in_rate[0] = 1; for (j=0;jweights[0][j*(topo[0]+1)+k+1]; net->weights[0][j*(topo[0]+1)] = -sum; } for (j=0;jweights[nbLayers-2][j*(topo[nbLayers-2]+1)] = mean; for (k=0;kweights[nbLayers-2][j*(topo[nbLayers-2]+1)+k+1] = randn(std); } return net; } #define MAX_NEURONS 100 #define MAX_OUT 10 double compute_gradient(MLPTrain *net, float *inputs, float *outputs, int nbSamples, double *W0_grad, double *W1_grad, double *error_rate) { int i,j; int s; int inDim, outDim, hiddenDim; int *topo; double *W0, *W1; double rms=0; int W0_size, W1_size; double hidden[MAX_NEURONS]; double netOut[MAX_NEURONS]; double error[MAX_NEURONS]; topo = net->topo; inDim = net->topo[0]; hiddenDim = net->topo[1]; outDim = net->topo[2]; W0_size = (topo[0]+1)*topo[1]; W1_size = (topo[1]+1)*topo[2]; W0 = net->weights[0]; W1 = net->weights[1]; memset(W0_grad, 0, W0_size*sizeof(double)); memset(W1_grad, 0, W1_size*sizeof(double)); for (i=0;i1; if (i==0) error[i] *= 3; rms += error[i]*error[i]; /*error[i] = error[i]/(1+fabs(error[i]));*/ } /* Back-propagate error */ for (i=0;inet->topo; W0_size = (topo[0]+1)*topo[1]; W1_size = (topo[1]+1)*topo[2]; double W0_grad[W0_size]; double W1_grad[W1_size]; arg->W0_grad = W0_grad; arg->W1_grad = W1_grad; while (1) { sem_wait(&sem_begin[arg->id]); if (arg->done) break; arg->rms = compute_gradient(arg->net, arg->inputs, arg->outputs, arg->nbSamples, arg->W0_grad, arg->W1_grad, arg->error_rate); sem_post(&sem_end[arg->id]); } fprintf(stderr, "done\n"); return NULL; } float mlp_train_backprop(MLPTrain *net, float *inputs, float *outputs, int nbSamples, int nbEpoch, float rate) { int i, j; int e; float best_rms = 1e10; int inDim, outDim, hiddenDim; int *topo; double *W0, *W1, *best_W0, *best_W1; double *W0_old, *W1_old; double *W0_old2, *W1_old2; double *W0_grad, *W1_grad; double *W0_oldgrad, *W1_oldgrad; double *W0_rate, *W1_rate; double *best_W0_rate, *best_W1_rate; int W0_size, W1_size; topo = net->topo; W0_size = (topo[0]+1)*topo[1]; W1_size = (topo[1]+1)*topo[2]; struct GradientArg args[NB_THREADS]; pthread_t thread[NB_THREADS]; int samplePerPart = nbSamples/NB_THREADS; int count_worse=0; int count_retries=0; topo = net->topo; inDim = net->topo[0]; hiddenDim = net->topo[1]; outDim = net->topo[2]; W0 = net->weights[0]; W1 = net->weights[1]; best_W0 = net->best_weights[0]; best_W1 = net->best_weights[1]; W0_old = malloc(W0_size*sizeof(double)); W1_old = malloc(W1_size*sizeof(double)); W0_old2 = malloc(W0_size*sizeof(double)); W1_old2 = malloc(W1_size*sizeof(double)); W0_grad = malloc(W0_size*sizeof(double)); W1_grad = malloc(W1_size*sizeof(double)); W0_oldgrad = malloc(W0_size*sizeof(double)); W1_oldgrad = malloc(W1_size*sizeof(double)); W0_rate = malloc(W0_size*sizeof(double)); W1_rate = malloc(W1_size*sizeof(double)); best_W0_rate = malloc(W0_size*sizeof(double)); best_W1_rate = malloc(W1_size*sizeof(double)); memcpy(W0_old, W0, W0_size*sizeof(double)); memcpy(W0_old2, W0, W0_size*sizeof(double)); memset(W0_grad, 0, W0_size*sizeof(double)); memset(W0_oldgrad, 0, W0_size*sizeof(double)); memcpy(W1_old, W1, W1_size*sizeof(double)); memcpy(W1_old2, W1, W1_size*sizeof(double)); memset(W1_grad, 0, W1_size*sizeof(double)); memset(W1_oldgrad, 0, W1_size*sizeof(double)); rate /= nbSamples; for (i=0;iin_rate[j]; for (i=0;i30) { count_retries++; count_worse=0; for (i=0;i10) break; for (i=0;i 0) W0_rate[i] *= 1.01; else if (W0_oldgrad[i]*W0_grad[i] < 0) W0_rate[i] *= .9; mean_rate += W0_rate[i]; if (W0_rate[i] < min_rate) min_rate = W0_rate[i]; if (W0_rate[i] < 1e-15) W0_rate[i] = 1e-15; /*if (W0_rate[i] > .01) W0_rate[i] = .01;*/ W0_oldgrad[i] = W0_grad[i]; W0_old2[i] = W0_old[i]; W0_old[i] = W0[i]; W0[i] += W0_grad[i]*W0_rate[i]; } for (i=0;i 0) W1_rate[i] *= 1.01; else if (W1_oldgrad[i]*W1_grad[i] < 0) W1_rate[i] *= .9; mean_rate += W1_rate[i]; if (W1_rate[i] < min_rate) min_rate = W1_rate[i]; if (W1_rate[i] < 1e-15) W1_rate[i] = 1e-15; W1_oldgrad[i] = W1_grad[i]; W1_old2[i] = W1_old[i]; W1_old[i] = W1[i]; W1[i] += W1_grad[i]*W1_rate[i]; } mean_rate /= (topo[0]+1)*topo[1] + (topo[1]+1)*topo[2]; fprintf (stderr, "%g %d", mean_rate, e); if (count_retries) fprintf(stderr, " %d", count_retries); fprintf(stderr, "\n"); if (stopped) break; } for (i=0;i \n"); return 1; } nbInputs = atoi(argv[1]); nbHidden = atoi(argv[2]); nbOutputs = atoi(argv[3]); nbSamples = atoi(argv[4]); nbEpoch = atoi(argv[5]); nbRealInputs = nbInputs; inputs = malloc(nbInputs*nbSamples*sizeof(*inputs)); outputs = malloc(nbOutputs*nbSamples*sizeof(*outputs)); seed = time(NULL); /*seed = 1452209040;*/ fprintf (stderr, "Seed is %u\n", seed); srand(seed); build_tansig_table(); signal(SIGTERM, handler); signal(SIGINT, handler); signal(SIGHUP, handler); for (i=0;i