Esempio n. 1
0
/* Train the GloVe model */
void *glove_thread(void *vid) {
    long long a, b ,l1, l2;
    long long id = *(long long*)vid;
    CREC cr;
    real diff, fdiff, temp1, temp2;
    FILE *fin;
    fin = fopen(input_file, "rb");
    fseeko(fin, (num_lines / num_threads * id) * (sizeof(CREC)), SEEK_SET); //Threads spaced roughly equally throughout file
    cost[id] = 0;
    
    for (a = 0; a < lines_per_thread[id]; a++) {
        fread(&cr, sizeof(CREC), 1, fin);
        if (feof(fin)) break;
        if (cr.word1 < 1 || cr.word2 < 1) { continue; }
        
        /* Get location of words in W & gradsq */
        l1 = (cr.word1 - 1LL) * (vector_size + 1); // cr word indices start at 1
        l2 = ((cr.word2 - 1LL) + vocab_size) * (vector_size + 1); // shift by vocab_size to get separate vectors for context words
        
        /* Calculate cost, save diff for gradients */
        diff = 0;
        for (b = 0; b < vector_size; b++) diff += W[b + l1] * W[b + l2]; // dot product of word and context word vector
        diff += W[vector_size + l1] + W[vector_size + l2] - log(cr.val); // add separate bias for each word
        fdiff = (cr.val > x_max) ? diff : pow(cr.val / x_max, alpha) * diff; // multiply weighting function (f) with diff

        // Check for NaN and inf() in the diffs.
        if (isnan(diff) || isnan(fdiff) || isinf(diff) || isinf(fdiff)) {
            fprintf(stderr,"Caught NaN in diff for kdiff for thread. Skipping update");
            continue;
        }

        cost[id] += 0.5 * fdiff * diff; // weighted squared error
        
        /* Adaptive gradient updates */
        fdiff *= eta; // for ease in calculating gradient
        for (b = 0; b < vector_size; b++) {
            // learning rate times gradient for word vectors
            temp1 = fdiff * W[b + l2];
            temp2 = fdiff * W[b + l1];
            // adaptive updates
            W[b + l1] -= check_nan(temp1 / sqrt(gradsq[b + l1]));
            W[b + l2] -= check_nan(temp2 / sqrt(gradsq[b + l2]));
            gradsq[b + l1] += temp1 * temp1;
            gradsq[b + l2] += temp2 * temp2;
        }
        // updates for bias terms
        W[vector_size + l1] -= check_nan(fdiff / sqrt(gradsq[vector_size + l1]));
        W[vector_size + l2] -= check_nan(fdiff / sqrt(gradsq[vector_size + l2]));
        fdiff *= fdiff;
        gradsq[vector_size + l1] += fdiff;
        gradsq[vector_size + l2] += fdiff;
        
    }
    
    fclose(fin);
    pthread_exit(NULL);
}
Esempio n. 2
0
int
main (int argc, char *argv[])
{
  MPFR_TEST_USE_RANDS ();
  tests_start_mpfr ();

  check_inexact ();
  check_hard ();
  check_nan ();
  check_lowr ();
  check_float (); /* checks single precision */
  check_double ();
  check_convergence ();
  check_64 ();

  check4("4.0","4.503599627370496e15", GMP_RNDZ, 62,
   "0.10000000000000000000000000000000000000000000000000000000000000E-49");
  check4("1.0","2.10263340267725788209e+187", GMP_RNDU, 65,
   "0.11010011111001101011111001100111110100000001101001111100111000000E-622");
  check4("2.44394909079968374564e-150", "2.10263340267725788209e+187",GMP_RNDU,
         65,
  "0.11010011111001101011111001100111110100000001101001111100111000000E-1119");

  consistency ();
  test_20070603 ();
  test_20070628 ();
  test_generic (2, 800, 50);

  tests_end_mpfr ();
  return 0;
}
Esempio n. 3
0
bool num_limits(void)
{   bool ok = true;

    ok &= check_epsilon();
    ok &= check_min();
    ok &= check_max();
    ok &= check_nan();

    return ok;
}
Esempio n. 4
0
 void operator()(const unsigned int npoints,
                 const point_type *const points,
                 value_type *const S) const
 {
   value_type S_[nnodes];
   for (unsigned int i=0; i<npoints; ++i) {
     operator()(points[i], S_);
     assert(check_nan(nnodes, S_));
     for (unsigned int j=0; j<nnodes; ++j)
       S[j*npoints + i] = S_[j];
   }
 }
Esempio n. 5
0
 void operator()(const unsigned int nparticles,
                 const unsigned int *const pindices,
                 const particle_type *const particles,
                 value_type *const S) const
 {
   value_type S_[nnodes];
   for (unsigned int i=0; i<nparticles; ++i) {
     operator()(particles[pindices[i]].getPoint(), S_);
     assert(check_nan(nnodes, S_));
     for (unsigned int j=0; j<nnodes; ++j)
       S[j*nparticles + i] = S_[j];
   }
 }
Esempio n. 6
0
/* Using this function below to constrain the state estimate.  The
 * constraint is applied after the UKF correction step.
 *
 * Constraints applied:
 *   0 <= x <= frame (image) width
 *   0 <= y <= frame height
 *   0 <= speed <= 100 (px/frame)
 *   If x, y, heading, or speed are NaN, then
 *      a) if the corresponding estimate is valid, that number is used
 *      b) if the estimate is also NaN, x, y, and heading are set to
 *          0, speed is set to 0.1 
 */
void constrain_state(CvMat* x_k,
                     CvMat* X_p,
                     double tank_radius,
                     double water_depth)
{
    double x     = cvGetReal2D(x_k, BELUGA_STATE_X, 0);
    double y     = cvGetReal2D(x_k, BELUGA_STATE_Y, 0);
    double z     = cvGetReal2D(x_k, BELUGA_STATE_Z, 0);
    double zdot  = cvGetReal2D(x_k, BELUGA_STATE_ZDOT, 0);
    double speed = cvGetReal2D(x_k, BELUGA_STATE_SPEED, 0);
    double theta = cvGetReal2D(x_k, BELUGA_STATE_THETA, 0);
    double omega = cvGetReal2D(x_k, BELUGA_STATE_OMEGA, 0);    

    double x_p     = cvGetReal2D(X_p, BELUGA_STATE_X, 0);
	double y_p	   = cvGetReal2D(X_p, BELUGA_STATE_Y, 0);
	double z_p	   = cvGetReal2D(X_p, BELUGA_STATE_Z, 0);
	double zdot_p  = cvGetReal2D(X_p, BELUGA_STATE_ZDOT, 0);
	double speed_p = cvGetReal2D(X_p, BELUGA_STATE_SPEED, 0);
	double theta_p = cvGetReal2D(X_p, BELUGA_STATE_THETA, 0);
	double omega_p = cvGetReal2D(X_p, BELUGA_STATE_OMEGA, 0);	 

    x = check_nan(x, x_p, 0);
    y = check_nan(y, y_p, 0);
    z = check_nan(z, z_p, water_depth);
    zdot = check_nan(zdot, zdot_p, 0);
    speed = check_nan(speed, speed_p, 0.1);
    theta = check_nan(theta, theta_p, 0);
    omega = check_nan(omega, omega_p, 0);

	if(x*x + y*y > tank_radius)
	{
		double phi = atan2(y, x);
		x = 0.95*tank_radius*cos(phi);
		y = 0.95*tank_radius*sin(phi);
	}

    z = MT_CLAMP(z, 0, water_depth);
    zdot = MT_CLAMP(zdot, -BELUGA_CONSTRAINT_MAX_VERTICAL_SPEED,
                    BELUGA_CONSTRAINT_MAX_VERTICAL_SPEED);
    speed = MT_CLAMP(speed, 0, BELUGA_CONSTRAINT_MAX_SPEED);
    omega = MT_CLAMP(omega, -BELUGA_CONSTRAINT_MAX_TURN_RATE,
                     BELUGA_CONSTRAINT_MAX_TURN_RATE);
    
    cvSetReal2D(x_k, BELUGA_STATE_X, 0, x);		 
	cvSetReal2D(x_k, BELUGA_STATE_Y, 0, y);		 
	cvSetReal2D(x_k, BELUGA_STATE_Z, 0, z);		 
	cvSetReal2D(x_k, BELUGA_STATE_ZDOT, 0, zdot);		 
	cvSetReal2D(x_k, BELUGA_STATE_SPEED, 0, speed);	 
	cvSetReal2D(x_k, BELUGA_STATE_THETA, 0, theta);	 
	cvSetReal2D(x_k, BELUGA_STATE_OMEGA, 0, omega);	 
                
}
int
main (int argc, char *argv[])
{
  tests_start_mpfr ();

  check_nan ();
  check_inexact ();
  check(948002822, "1.22191250737771397120e+20", MPFR_RNDN,
        "7.758352715731357946e-12");
  check(1976245324, "1.25296395864546893357e+232", MPFR_RNDZ,
        "1.5772563211925444801e-223");
  check(740454110, "2.11496253355831863313e+183", MPFR_RNDZ,
        "3.5010270784996976041e-175");
  check(1690540942, "1.28278599852446657468e-276", MPFR_RNDU,
        "1.3178666932321966062e285");
  check(1476599377, "-2.14191393656148625995e+305", MPFR_RNDD,
        "-6.8938315017943889615e-297");

  /* inv is for 1/x */
  data_check ("data/inv", mpfr_inv, "mpfr_ui_div(1,x)");

  tests_end_mpfr ();
  return 0;
}
int
main (void)
{
  mpfr_prec_t p;
  int k;

  tests_start_mpfr ();

  for (p = MPFR_PREC_MIN; p <= 128; p++)
    {
      test_property1 (p, MPFR_RNDN, 0);
      test_property1 (p, MPFR_RNDU, 0);
      test_property1 (p, MPFR_RNDA, 0);
      test_property1 (p, MPFR_RNDN, 1);
      test_property1 (p, MPFR_RNDU, 1);
      test_property1 (p, MPFR_RNDA, 1);
      test_property2 (p, MPFR_RNDN);
    }

  check_diverse ("635030154261163106768013773815762607450069292760790610550915652722277604820131530404842415587328", 160, "796887792767063979679855997149887366668464780637");
  special ();
  check_nan ();

  for (p=2; p<200; p++)
    for (k=0; k<200; k++)
      check_inexact (p);
  check_float();

  check3 ("-0.0", MPFR_RNDN, "0.0");
  check4 ("6.37983013646045901440e+32", MPFR_RNDN, "5.9bc5036d09e0c@13");
  check4 ("1.0", MPFR_RNDN, "1");
  check4 ("1.0", MPFR_RNDZ, "1");
  check4 ("3.725290298461914062500000e-9", MPFR_RNDN, "4@-4");
  check4 ("3.725290298461914062500000e-9", MPFR_RNDZ, "4@-4");
  check4 ("1190456976439861.0", MPFR_RNDZ, "2.0e7957873529a@6");
  check4 ("1219027943874417664.0", MPFR_RNDZ, "4.1cf2af0e6a534@7");
  /* the following examples are bugs in Cygnus compiler/system, found by
     Fabrice Rouillier while porting mpfr to Windows */
  check4 ("9.89438396044940256501e-134", MPFR_RNDU, "8.7af7bf0ebbee@-56");
  check4 ("7.86528588050363751914e+31", MPFR_RNDZ, "1.f81fc40f32062@13");
  check4 ("0.99999999999999988897", MPFR_RNDN, "f.ffffffffffff8@-1");
  check4 ("1.00000000000000022204", MPFR_RNDN, "1");
  /* the following examples come from the paper "Number-theoretic Test
   Generation for Directed Rounding" from Michael Parks, Table 4 */

  check4 ("78652858805036375191418371571712.0", MPFR_RNDN,
          "1.f81fc40f32063@13");
  check4 ("38510074998589467860312736661504.0", MPFR_RNDN,
          "1.60c012a92fc65@13");
  check4 ("35318779685413012908190921129984.0", MPFR_RNDN,
          "1.51d17526c7161@13");
  check4 ("26729022595358440976973142425600.0", MPFR_RNDN,
          "1.25e19302f7e51@13");
  check4 ("22696567866564242819241453027328.0", MPFR_RNDN,
          "1.0ecea7dd2ec3d@13");
  check4 ("22696888073761729132924856434688.0", MPFR_RNDN,
          "1.0ecf250e8e921@13");
  check4 ("36055652513981905145251657416704.0", MPFR_RNDN,
          "1.5552f3eedcf33@13");
  check4 ("30189856268896404997497182748672.0", MPFR_RNDN,
          "1.3853ee10c9c99@13");
  check4 ("36075288240584711210898775080960.0", MPFR_RNDN,
          "1.556abe212b56f@13");
  check4 ("72154663483843080704304789585920.0", MPFR_RNDN,
          "1.e2d9a51977e6e@13");

  check4 ("78652858805036375191418371571712.0", MPFR_RNDZ,
          "1.f81fc40f32062@13");
  check4 ("38510074998589467860312736661504.0", MPFR_RNDZ,
          "1.60c012a92fc64@13");
  check4 ("35318779685413012908190921129984.0", MPFR_RNDZ, "1.51d17526c716@13");
  check4 ("26729022595358440976973142425600.0", MPFR_RNDZ, "1.25e19302f7e5@13");
  check4 ("22696567866564242819241453027328.0", MPFR_RNDZ,
          "1.0ecea7dd2ec3c@13");
  check4 ("22696888073761729132924856434688.0", MPFR_RNDZ, "1.0ecf250e8e92@13");
  check4 ("36055652513981905145251657416704.0", MPFR_RNDZ,
          "1.5552f3eedcf32@13");
  check4 ("30189856268896404997497182748672.0", MPFR_RNDZ,
          "1.3853ee10c9c98@13");
  check4 ("36075288240584711210898775080960.0", MPFR_RNDZ,
          "1.556abe212b56e@13");
  check4 ("72154663483843080704304789585920.0", MPFR_RNDZ,
          "1.e2d9a51977e6d@13");

  check4 ("78652858805036375191418371571712.0", MPFR_RNDU,
          "1.f81fc40f32063@13");
  check4 ("38510074998589467860312736661504.0", MPFR_RNDU,
          "1.60c012a92fc65@13");
  check4 ("35318779685413012908190921129984.0", MPFR_RNDU,
          "1.51d17526c7161@13");
  check4 ("26729022595358440976973142425600.0", MPFR_RNDU,
          "1.25e19302f7e51@13");
  check4 ("22696567866564242819241453027328.0", MPFR_RNDU,
          "1.0ecea7dd2ec3d@13");
  check4 ("22696888073761729132924856434688.0", MPFR_RNDU,
          "1.0ecf250e8e921@13");
  check4 ("36055652513981905145251657416704.0", MPFR_RNDU,
          "1.5552f3eedcf33@13");
  check4 ("30189856268896404997497182748672.0", MPFR_RNDU,
          "1.3853ee10c9c99@13");
  check4 ("36075288240584711210898775080960.0", MPFR_RNDU,
          "1.556abe212b56f@13");
  check4 ("72154663483843080704304789585920.0", MPFR_RNDU,
          "1.e2d9a51977e6e@13");

  check4 ("78652858805036375191418371571712.0", MPFR_RNDD,
          "1.f81fc40f32062@13");
  check4 ("38510074998589467860312736661504.0", MPFR_RNDD,
          "1.60c012a92fc64@13");
  check4 ("35318779685413012908190921129984.0", MPFR_RNDD, "1.51d17526c716@13");
  check4 ("26729022595358440976973142425600.0", MPFR_RNDD, "1.25e19302f7e5@13");
  check4 ("22696567866564242819241453027328.0", MPFR_RNDD,
          "1.0ecea7dd2ec3c@13");
  check4 ("22696888073761729132924856434688.0", MPFR_RNDD, "1.0ecf250e8e92@13");
  check4 ("36055652513981905145251657416704.0", MPFR_RNDD,
          "1.5552f3eedcf32@13");
  check4 ("30189856268896404997497182748672.0", MPFR_RNDD,
          "1.3853ee10c9c98@13");
  check4 ("36075288240584711210898775080960.0", MPFR_RNDD,
          "1.556abe212b56e@13");
  check4 ("72154663483843080704304789585920.0", MPFR_RNDD,
          "1.e2d9a51977e6d@13");

  /* check that rounding away is just rounding toward plus infinity */
  check4 ("72154663483843080704304789585920.0", MPFR_RNDA,
          "1.e2d9a51977e6e@13");

  test_generic (2, 300, 15);
  data_check ("data/sqrt", mpfr_sqrt, "mpfr_sqrt");
  bad_cases (mpfr_sqrt, mpfr_sqr, "mpfr_sqrt", 8, -256, 255, 4, 128, 800, 50);

  tests_end_mpfr ();
  return 0;
}