Пример #1
0
void FFM::initParams(void)
{ 
  int k_aligned = (int)ceil((double)parameters.num_factors/kALIGN)*kALIGN;
  parameters.num_factors = k_aligned;
    
  try
  {
    W = malloc_aligned_float((long long)num_features*num_fields*k_aligned*2);
  }
  catch(std::bad_alloc const &e)
  {
    throw;
  }

  init_coeff = 0.5/sqrt(parameters.num_factors);
  float *w = W;

  std::default_random_engine generator;
  std::uniform_real_distribution<float> distribution(0.0, 1.0);

  for(int j = 0; j < num_features; j++)
  {
    for(int f = 0; f < num_fields; f++)
    {
      for(int d = 0; d < parameters.num_factors; d++, w++)
	  *w = init_coeff*distribution(generator);
      for(int d = parameters.num_factors; d < k_aligned; d++, w++)
	  *w = 0;
      for(int d = k_aligned; d < 2*k_aligned; d++, w++)
	  *w = 1;
    }
  }
}
Пример #2
0
ffm_model* init_model(ffm_int n, ffm_int m, ffm_parameter param)
{
    ffm_int k_aligned = (ffm_int)ceil((ffm_double)param.k/kALIGN)*kALIGN;

    ffm_model *model = new ffm_model;
    model->n = n;
    model->k = k_aligned;
    model->m = m;
    model->W = nullptr;
    model->normalization = param.normalization;
    
    try
    {
        model->W = malloc_aligned_float((ffm_long)n*m*k_aligned*2);
    }
    catch(bad_alloc const &e)
    {
        ffm_destroy_model(&model);
        throw;
    }

    ffm_float coef = 0.5/sqrt(param.k);
    ffm_float *w = model->W;

    default_random_engine generator;
    uniform_real_distribution<ffm_float> distribution(0.0, 1.0);

    for(ffm_int j = 0; j < model->n; j++)
    {
        for(ffm_int f = 0; f < model->m; f++)
        {
            for(ffm_int d = 0; d < param.k; d++, w++)
                *w = coef*distribution(generator);
            for(ffm_int d = param.k; d < k_aligned; d++, w++)
                *w = 0;
            for(ffm_int d = k_aligned; d < 2*k_aligned; d++, w++)
                *w = 1;
        }
    }

    return model;
}