Esempio n. 1
0
void init_network_weights(network_ptr network, double max_weight_max, RNG *generator)

/* This routine takes in a pointer to an allocated network, a maximum weight value,
   and a pointer to a random number generator struct (see rnd.c).  It walks the network
   and pre-initializes all weights and biases to values in the range of -max_weight_max
   to max_weight_max.  Be sure you pass in only network structs that have been 
   allocated via malloc_network().
   
   If this were a mature implementation, this routine would actually initialize
   the weights according to a heursitic that took into account the number of inputs
   going into each neuron AND presuming that input patters were preprocessed and
   pre-scaled into a known range of numerical values.  See the book and class
   notes for details.
   
*/

{ int count;
  for (count=0; count < (network->input_node_count * network->hidden_neuron_count); count++)
      network->first_layer_weights[count] = rng_uniform(generator, -max_weight_max, max_weight_max);
  for (count=0; count < (network->hidden_neuron_count * network->output_neuron_count); count++)
      network->second_layer_weights[count] = rng_uniform(generator, -max_weight_max, max_weight_max);
  for (count = 0; count < network->hidden_neuron_count; count++)
      network->hidden_layer[count].bias = rng_uniform(generator, -max_weight_max, max_weight_max);
  for (count = 0; count < network->output_neuron_count; count++)
      network->output_layer[count].bias = rng_uniform(generator, -max_weight_max, max_weight_max);
}
Esempio n. 2
0
File: main.cpp Progetto: brantr/lucy
Real *MakeRandomAmplitudes(int nr, Real amin, Real amax)
{
  Real *amp = (Real *) malloc(nr*sizeof(Real));
  for(int i=0;i<nr;i++)
    amp[i] = rng_uniform(amin, amax);

  return amp;
}
Esempio n. 3
0
void _rng_generate_normals(RNG *rng, double *gX1, double *gX2, double *gSX)
{ 
  double v1,v2,s,d;
  double rng_uniform();
  
  /* This is a private function that should never be called by
     a user */
  do
   { 
     v1 = rng_uniform(rng, -1.0,1.0);
     v2 = rng_uniform(rng, -1.0,1.0);
     s = v1 * v1 + v2 * v2;
   }
  while (s >= 1.0);
  *gSX = -2 * log(s);
  d = sqrt(*gSX/s);
  *gX1 = v1 * d;
  *gX2 = v2 * d;
}
Esempio n. 4
0
real_t rng_uniform_positive(rng_t* rng)
{
  if (rng->vtable.uniform_positive != NULL)
    return rng->vtable.uniform_positive(rng->context);
  else
  {
    while (true)
    {
      real_t val = rng_uniform(rng);
      if (val != 0.0)
        return val;
    }
  }
}
Esempio n. 5
0
void Hedcut::sample_initial_points(cv::Mat & img, int n, std::vector<cv::Point2d> & pts)
{
	//create n points that spread evenly that are in areas of black points...
	int count = 0;

	cv::RNG rng_uniform(time(NULL));
	cv::RNG rng_gaussian(time(NULL));
	cv::Mat visited(img.size(), CV_8U, cv::Scalar::all(0)); //all unvisited

	while (count < n)
	{
		//generate a random point
		int c = (int)floor(img.size().width*rng_uniform.uniform(0.f, 1.f));
		int r = (int)floor(img.size().height*rng_uniform.uniform(0.f, 1.f));

		//decide to keep basic on a probability (black has higher probability)
		float value = img.at<uchar>(r, c)*1.0/255; //black:0, white:1
		float gr = fabs(rng_gaussian.gaussian(0.8));
		if ( value < gr && visited.at<uchar>(r, c) ==0) //keep
		{
			count++;
			pts.push_back(cv::Point(r, c));
			visited.at<uchar>(r,c)=1;
		}
	}

	if (debug)
	{
		cv::Mat tmp = img.clone();
		for (auto& c : pts)
		{
			cv::circle(tmp, cv::Point(c.y, c.x), 2, CV_RGB(0, 0, 255), -1);
		}
		cv::imshow("samples", tmp);
		//cv::waitKey();
	}
}
Esempio n. 6
0
double RAN1_UniformRandom(double l, double u)  
{ return rng_uniform(&_ran1_rng, l, u);}
Esempio n. 7
0
double rng_uniform01(RNG *rng)
{
  return rng_uniform(rng, 0.0, 1.0);
}
Esempio n. 8
0
void ios_feas_pump(glp_tree *T)
{     glp_prob *P = T->mip;
      int n = P->n;
      glp_prob *lp = NULL;
      struct VAR *var = NULL;
      RNG *rand = NULL;
      GLPCOL *col;
      glp_smcp parm;
      int j, k, new_x, nfail, npass, nv, ret, stalling;
      double dist, tol;
      xassert(glp_get_status(P) == GLP_OPT);
      /* this heuristic is applied only once on the root level */
      if (!(T->curr->level == 0 && T->curr->solved == 1)) goto done;
      /* determine number of binary variables */
      nv = 0;
      for (j = 1; j <= n; j++)
      {  col = P->col[j];
         /* if x[j] is continuous, skip it */
         if (col->kind == GLP_CV) continue;
         /* if x[j] is fixed, skip it */
         if (col->type == GLP_FX) continue;
         /* x[j] is non-fixed integer */
         xassert(col->kind == GLP_IV);
         if (col->type == GLP_DB && col->lb == 0.0 && col->ub == 1.0)
         {  /* x[j] is binary */
            nv++;
         }
         else
         {  /* x[j] is general integer */
            if (T->parm->msg_lev >= GLP_MSG_ALL)
               xprintf("FPUMP heuristic cannot be applied due to genera"
                  "l integer variables\n");
            goto done;
         }
      }
      /* there must be at least one binary variable */
      if (nv == 0) goto done;
      if (T->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Applying FPUMP heuristic...\n");
      /* build the list of binary variables */
      var = xcalloc(1+nv, sizeof(struct VAR));
      k = 0;
      for (j = 1; j <= n; j++)
      {  col = P->col[j];
         if (col->kind == GLP_IV && col->type == GLP_DB)
            var[++k].j = j;
      }
      xassert(k == nv);
      /* create working problem object */
      lp = glp_create_prob();
more: /* copy the original problem object to keep it intact */
      glp_copy_prob(lp, P, GLP_OFF);
      /* we are interested to find an integer feasible solution, which
         is better than the best known one */
      if (P->mip_stat == GLP_FEAS)
      {  int *ind;
         double *val, bnd;
         /* add a row and make it identical to the objective row */
         glp_add_rows(lp, 1);
         ind = xcalloc(1+n, sizeof(int));
         val = xcalloc(1+n, sizeof(double));
         for (j = 1; j <= n; j++)
         {  ind[j] = j;
            val[j] = P->col[j]->coef;
         }
         glp_set_mat_row(lp, lp->m, n, ind, val);
         xfree(ind);
         xfree(val);
         /* introduce upper (minimization) or lower (maximization)
            bound to the original objective function; note that this
            additional constraint is not violated at the optimal point
            to LP relaxation */
#if 0 /* modified by xypron <*****@*****.**> */
         if (P->dir == GLP_MIN)
         {  bnd = P->mip_obj - 0.10 * (1.0 + fabs(P->mip_obj));
            if (bnd < P->obj_val) bnd = P->obj_val;
            glp_set_row_bnds(lp, lp->m, GLP_UP, 0.0, bnd - P->c0);
         }
         else if (P->dir == GLP_MAX)
         {  bnd = P->mip_obj + 0.10 * (1.0 + fabs(P->mip_obj));
            if (bnd > P->obj_val) bnd = P->obj_val;
            glp_set_row_bnds(lp, lp->m, GLP_LO, bnd - P->c0, 0.0);
         }
         else
            xassert(P != P);
#else
         bnd = 0.1 * P->obj_val + 0.9 * P->mip_obj;
         /* xprintf("bnd = %f\n", bnd); */
         if (P->dir == GLP_MIN)
            glp_set_row_bnds(lp, lp->m, GLP_UP, 0.0, bnd - P->c0);
         else if (P->dir == GLP_MAX)
            glp_set_row_bnds(lp, lp->m, GLP_LO, bnd - P->c0, 0.0);
         else
            xassert(P != P);
#endif
      }
      /* reset pass count */
      npass = 0;
      /* invalidate the rounded point */
      for (k = 1; k <= nv; k++)
         var[k].x = -1;
pass: /* next pass starts here */
      npass++;
      if (T->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Pass %d\n", npass);
      /* initialize minimal distance between the basic point and the
         rounded one obtained during this pass */
      dist = DBL_MAX;
      /* reset failure count (the number of succeeded iterations failed
         to improve the distance) */
      nfail = 0;
      /* if it is not the first pass, perturb the last rounded point
         rather than construct it from the basic solution */
      if (npass > 1)
      {  double rho, temp;
         if (rand == NULL)
            rand = rng_create_rand();
         for (k = 1; k <= nv; k++)
         {  j = var[k].j;
            col = lp->col[j];
            rho = rng_uniform(rand, -0.3, 0.7);
            if (rho < 0.0) rho = 0.0;
            temp = fabs((double)var[k].x - col->prim);
            if (temp + rho > 0.5) var[k].x = 1 - var[k].x;
         }
         goto skip;
      }
loop: /* innermost loop begins here */
      /* round basic solution (which is assumed primal feasible) */
      stalling = 1;
      for (k = 1; k <= nv; k++)
      {  col = lp->col[var[k].j];
         if (col->prim < 0.5)
         {  /* rounded value is 0 */
            new_x = 0;
         }
         else
         {  /* rounded value is 1 */
            new_x = 1;
         }
         if (var[k].x != new_x)
         {  stalling = 0;
            var[k].x = new_x;
         }
      }
      /* if the rounded point has not changed (stalling), choose and
         flip some its entries heuristically */
      if (stalling)
      {  /* compute d[j] = |x[j] - round(x[j])| */
         for (k = 1; k <= nv; k++)
         {  col = lp->col[var[k].j];
            var[k].d = fabs(col->prim - (double)var[k].x);
         }
         /* sort the list of binary variables by descending d[j] */
         qsort(&var[1], nv, sizeof(struct VAR), fcmp);
         /* choose and flip some rounded components */
         for (k = 1; k <= nv; k++)
         {  if (k >= 5 && var[k].d < 0.35 || k >= 10) break;
            var[k].x = 1 - var[k].x;
         }
      }
skip: /* check if the time limit has been exhausted */
      if (T->parm->tm_lim < INT_MAX &&
         (double)(T->parm->tm_lim - 1) <=
         1000.0 * xdifftime(xtime(), T->tm_beg)) goto done;
      /* build the objective, which is the distance between the current
         (basic) point and the rounded one */
      lp->dir = GLP_MIN;
      lp->c0 = 0.0;
      for (j = 1; j <= n; j++)
         lp->col[j]->coef = 0.0;
      for (k = 1; k <= nv; k++)
      {  j = var[k].j;
         if (var[k].x == 0)
            lp->col[j]->coef = +1.0;
         else
         {  lp->col[j]->coef = -1.0;
            lp->c0 += 1.0;
         }
      }
      /* minimize the distance with the simplex method */
      glp_init_smcp(&parm);
      if (T->parm->msg_lev <= GLP_MSG_ERR)
         parm.msg_lev = T->parm->msg_lev;
      else if (T->parm->msg_lev <= GLP_MSG_ALL)
      {  parm.msg_lev = GLP_MSG_ON;
         parm.out_dly = 10000;
      }
      ret = glp_simplex(lp, &parm);
      if (ret != 0)
      {  if (T->parm->msg_lev >= GLP_MSG_ERR)
            xprintf("Warning: glp_simplex returned %d\n", ret);
         goto done;
      }
      ret = glp_get_status(lp);
      if (ret != GLP_OPT)
      {  if (T->parm->msg_lev >= GLP_MSG_ERR)
            xprintf("Warning: glp_get_status returned %d\n", ret);
         goto done;
      }
      if (T->parm->msg_lev >= GLP_MSG_DBG)
         xprintf("delta = %g\n", lp->obj_val);
      /* check if the basic solution is integer feasible; note that it
         may be so even if the minimial distance is positive */
      tol = 0.3 * T->parm->tol_int;
      for (k = 1; k <= nv; k++)
      {  col = lp->col[var[k].j];
         if (tol < col->prim && col->prim < 1.0 - tol) break;
      }
      if (k > nv)
      {  /* okay; the basic solution seems to be integer feasible */
         double *x = xcalloc(1+n, sizeof(double));
         for (j = 1; j <= n; j++)
         {  x[j] = lp->col[j]->prim;
            if (P->col[j]->kind == GLP_IV) x[j] = floor(x[j] + 0.5);
         }
#if 1 /* modified by xypron <*****@*****.**> */
         /* reset direction and right-hand side of objective */
         lp->c0  = P->c0;
         lp->dir = P->dir;
         /* fix integer variables */
         for (k = 1; k <= nv; k++)
#if 0 /* 18/VI-2013; fixed by mao
       * this bug causes numerical instability, because column statuses
       * are not changed appropriately */
         {  lp->col[var[k].j]->lb   = x[var[k].j];
            lp->col[var[k].j]->ub   = x[var[k].j];
            lp->col[var[k].j]->type = GLP_FX;
         }
#else
            glp_set_col_bnds(lp, var[k].j, GLP_FX, x[var[k].j], 0.);
#endif
         /* copy original objective function */
         for (j = 1; j <= n; j++)
            lp->col[j]->coef = P->col[j]->coef;
         /* solve original LP and copy result */
         ret = glp_simplex(lp, &parm);
         if (ret != 0)
         {  if (T->parm->msg_lev >= GLP_MSG_ERR)
               xprintf("Warning: glp_simplex returned %d\n", ret);
            goto done;
         }
         ret = glp_get_status(lp);
         if (ret != GLP_OPT)
         {  if (T->parm->msg_lev >= GLP_MSG_ERR)
               xprintf("Warning: glp_get_status returned %d\n", ret);
            goto done;
         }
         for (j = 1; j <= n; j++)
            if (P->col[j]->kind != GLP_IV) x[j] = lp->col[j]->prim;
#endif
         ret = glp_ios_heur_sol(T, x);
         xfree(x);
         if (ret == 0)
         {  /* the integer solution is accepted */
            if (ios_is_hopeful(T, T->curr->bound))
            {  /* it is reasonable to apply the heuristic once again */
               goto more;
            }
            else
            {  /* the best known integer feasible solution just found
                  is close to optimal solution to LP relaxation */
               goto done;
            }
         }
      }
Esempio n. 9
0
/**
 * Split randomly.
 * Return true on success.
 * Return false when:
 * - disabled (i.e. when doing regular language processing).
 * - an error occurs (the behavior then is undefined).
 *   Such an error has not been observed yet.
 */
bool anysplit(Sentence sent, const char *word)
{
	Dictionary afdict = sent->dict->affix_table;
	anysplit_params *as;
	Afdict_class * stemsubscr;
	size_t stemsubscr_len;

	size_t l = strlen(word);
	p_list pl;
	size_t pos;
	int p;
	int sample_point;
	size_t nsplits;
	size_t rndtried = 0;
	size_t rndissued = 0;
	size_t i;
	unsigned int seed = 0;
	char *prefix_string = alloca(l+2+1); /* word + ".=" + NUL */
	char *suffix_string = alloca(l+1);   /* word + NUL */
	bool use_sampling = true;
	const char infix_mark = INFIX_MARK(afdict);


	if (NULL == afdict) return false;
	as = afdict->anysplit;

	if ((NULL == as) || (0 == as->nparts)) return false; /* Anysplit disabled */

	if (0 == l)
	{
		prt_error("Warning: anysplit(): word length 0\n");
		return false;
	}

	stemsubscr = AFCLASS(afdict, AFDICT_STEMSUBSCR);
	stemsubscr_len = (NULL == stemsubscr->string[0]) ? 0 :
		strlen(stemsubscr->string[0]);

	/* Don't split morphemes again. If INFIXMARK and/or SUBSCRMARK are
	 * not defined in the affix file, then morphemes may get split again unless
	 * restricted by REGPRE/REGMID/REGSUF. */
	if (word[0] == infix_mark) return true;
	if ((l > stemsubscr_len) &&
	    (0 == strcmp(word+l-stemsubscr_len, stemsubscr->string[0])))
		return true;

	// seed = time(NULL)+(unsigned int)(long)&seed;

#if DEBUG_ANYSPLIT
	gw = word;
#endif

	nsplits = split(l, as->nparts, &as->scl[l]);
	if (0 == nsplits)
	{
		prt_error("Warning: anysplit(): split() failed (shouldn't happen)\n");
		return false;
	}

	if (as->altsmax >= nsplits)
	{
		/* Issue everything */
		sample_point = -1;
		use_sampling = false;
	}

	lgdebug(+2, "Start%s sampling: word=%s, nsplits=%zu, maxsplits=%d, "
	        "as->altsmin=%zu, as->altsmax=%zu\n", use_sampling ? "" : " no",
	        word, nsplits, as->nparts, as->altsmin, as->altsmax);

	while (rndtried < nsplits && (!use_sampling || (rndissued < as->altsmin)))
	{
		if (use_sampling)
		{
			sample_point = rng_uniform(&seed, nsplits);

			if (sample_point < 0) /* Cannot happen with rand_r() */
			{
				prt_error("Error: rng: %s\n", strerror(errno));
				return false;
			}
		}
		else
		{
			sample_point++;
		}

		lgdebug(2, "Sample: %d ", sample_point);
		if (as->scl[l].p_tried[sample_point])
		{
			lgdebug(4, "(repeated)\n");
			continue;
		}
		lgdebug(4, "(new)");
		rndtried++;
		as->scl[l].p_tried[sample_point] = true;
		if (morpheme_match(sent, word, l, &as->scl[l].sp[sample_point*as->nparts]))
		{
			as->scl[l].p_selected[sample_point] = true;
			rndissued++;
		}
		else
		{
			lgdebug(2, "\n");
		}
	}

	lgdebug(2, "Results: word '%s' (length=%zu): %zu/%zu:\n", word, l, rndissued, nsplits);

	for (i = 0; i < nsplits; i++)
	{
		const char **suffixes = NULL;
		int num_suffixes = 0;

		if (!as->scl[l].p_selected[i]) continue;

		pl = &as->scl[l].sp[i*as->nparts];
		pos = 0;
		for (p = 0; p < as->nparts; p++)
		{
			if (pl[0] == (int)l)  /* This is the whole word */
			{
				strncpy(prefix_string, &word[pos], pl[p]-pos);
				prefix_string[pl[p]-pos] = '\0';
			}
			else
			if (0 == pos)   /* The first but not the only morpheme */
			{
				strncpy(prefix_string, &word[pos], pl[p]-pos);
				prefix_string[pl[p]-pos] = '\0';

				if (0 != stemsubscr->length)
				    strcat(prefix_string, stemsubscr->string[0]);
			}
			else           /* 2nd and on morphemes */
			{
				strncpy(suffix_string, &word[pos], pl[p]-pos);
				suffix_string[pl[p]-pos] = '\0';
				altappend(sent, &suffixes, suffix_string);
				num_suffixes++;
			}

			pos = pl[p];
			if (pos == l) break;
		}

		/* Here a leading INFIX_MARK is added to the suffixes if needed. */
		add_alternative(sent,
		   0,NULL, 1,(const char **)&prefix_string, num_suffixes,suffixes);
		free(suffixes);
	}

	return true;
}
Esempio n. 10
0
int main() 
{ int c;
  int random_index;
  double Y;
  
  network_ptr my_network;  /* This is a pointer to the neural net structure that will
                              store our perceptron */

  RNG *rng_one;            /* This holds the current state of a random number generator.
                              see rnd.c if you interested */

  
  /* Note that below I'm not training the outputs to extreme values (0.0 or 1.0) 
     and I'm not inputting extreme values (0.0 or 1.0) on the input units.
     Anyone want to hazard a guess as to why? */
     
  /* Define Input Vectors (XOR inputs) */
  //double X[4][2] = { {0.05, 0.05}, {0.05, 0.95}, {0.95, 0.05}, {0.95, 0.95} };  

  /* Define Corresponding Desired Outputs (XOR outputs) */
  //double D[4]    = { 0.05, 0.95, 0.95, 0.05 };

  double X[1];
  double D[1];
  double cc;

  /* Set up random number generator */
   rng_one = rng_create();

  /* Set up neural network */
   malloc_network(1, 500, 1, &my_network); /* Two input nodes, two hidden nodes, one output node */

   /* Randomize initial weights */
   init_network_weights(my_network,0.1,rng_one);

   /* Ok... let's let this guy learn the XOR function.  We'll use random selection 
      of input/output pairs and update weights with each trial (non-epoch).  Also,
      we'll hard code for 50,000 presentations.  You might want to change the stopping
      condition to something that makes more sense, like achieving a target gradient */
   int i=0;
   int counter=0;
   int counter2=0;
   double o1,o2;
   o1=0.0;
   o2=0.0;
   for (c=0; c<5000000; c++)
      { 
        o1=o2;
        X[0] = rng_uniform(rng_one, 0.0, 2.0 * 3.14159); 
		D[0] = sin(X[0]);
		compute_network_outputs(&X[0], my_network);        /* feed inputs forward  */
        compute_network_gradients(&D[0], my_network);      /* compute gradients       */
        apply_delta_rule(my_network, 0.01);                /* adjust weights       */
        o2 = my_network->output_layer[0].output;
        for(i=0;i<my_network->hidden_neuron_count;i++){                
             if(my_network->hidden_layer[i].gradient-0<0.000001 && my_network->hidden_layer[i].gradient-0>-0.000001){ 
                    counter++;
             }
        }
        if(o2-o1<0.1 && o2-o1>-0.1){            //output2-output1 must be in the range[-0.01,0.01], and 10 times
                    counter2++;              
        }else{
                    counter2=0;
        }
        if(counter==500&&counter2==3){          //all gradients of hiden layer are 0.0      
                 break;
        }
        counter=0;
      } 
     


   /* Now, let's see how we did by checking all the inputs against what the trained network
      produces */
	  
   printf("\nNetwork Outputs for Each Training Pattern\n");
   printf("-----------------------------------------\n");
   
   for (cc=0.0; cc<= 2.0 * 3.14159; cc+=0.01)
       { 
         X[0] = cc;
		 compute_network_outputs(&X[0],my_network); /* Feed input pattern forward */
         Y = my_network->output_layer[0].output;   /* Get output of output layer node 0 */
         printf("%lf %lf\n", X[0], Y); /* Print outputs generated by trained network */
       }


   printf("\n");
   printf("Perceptron Architecture \n");
   printf("-----------------------------------------\n");

   //print_network_parameters(my_network);
   print_network_outputs(my_network);
   free_network(&my_network);
   
    printf("Break at:%d\n",c);    
   system("pause");

}