示例#1
0
Expr*
admit_call_expr(Context& cxt, Usage& c, Call_expr& e)
{
  Call_expr& a = cast<Call_expr>(c.expression());

  // Build the list of parameter types from the declared types
  // of operands in the constraint.
  Type_list ts {&declared_type(a.function())};
  for (Expr& e0 : a.arguments())
    ts.push_back(declared_type(e0));

  // Build the list of arguments from e. Note that the first
  // argument is actually the function.
  Expr_list es {&e.function()};
  for (Expr& e0 : e.arguments())
    es.push_back(e0);

  // If conversion fails, this is not accessible.
  try {
    initialize_parameters(cxt, ts, es);
  } catch (Translation_error&) {
    return nullptr;
  }

  // Adjust the type and admit the expression.
  e.type_ = &a.type();
  return &e;
}
示例#2
0
/* RANDOM_SET_CLSF
   10nov94 wmt: initialize n_used_list
   05jan95 wmt: upgrade calculations to ac-x code
   17may95 wmt: Convert from srand/rand to srand48/lrand48

   This resets the classification 'clsf to size n-classes and uses
   choose-random-prototype to randomly initializes any added classes, or all
   classes with 'random-start.  Classes are initialized by choosing one class
   as a prototype and adding 1% from two others for variance.  The function
   modifies the classification and returns the number of classes and the
   marginal probability.
   */
int random_set_clsf( clsf_DS clsf, int n_classes, int delete_duplicates,
                    int  display_wts, unsigned int initial_cycles_p,
                    FILE *log_file_fp, FILE *stream)
{
  int n, i, n_data, index_0, *used_list, n_used_list = 0, num_atts = 0, n_others;
  int index_1, *used_cls_list, n_used_cls_list = 0, m;
  class_DS *classes, class;
  float proto_wt, wt_0, wt_1;

  n_classes =
    max(1, min((int) ceil((double) n_classes), clsf_DS_max_n_classes(clsf)));

  adjust_clsf_DS_classes(clsf, n_classes);

  n_data = clsf->database->n_data;
  for (i=0; i<clsf->database->n_atts; i++)
    if (eqstring( clsf->database->att_info[i]->type, "dummy") != TRUE)
      num_atts++;
  classes = clsf->classes;
  proto_wt = (float) n_data / n_classes;
  wt_0 = 0.95 * proto_wt;
  n_others = min( num_atts, (int) ceil( (double) (n_data / 2)));
  wt_1 = (0.05 * proto_wt) / n_others;
  used_list = (int *) malloc( n_classes * sizeof(int));
  used_cls_list = (int *) malloc( (n_others + 1) * sizeof(int));
  for (i=0; i<n_classes; i++)
    used_list[i] = 0;                                   /* primary cases for all classes */
  srand48( (long) (get_universal_time()));              /* re-init random number generator */
  for (n=0; n<n_classes; n++) {
    class = classes[n];
    index_0 = new_random( n_data, used_list, n_used_list);
    used_list[n_used_list] = index_0;
    n_used_list++;
    for (i=0; i<n_others; i++)
      used_cls_list[i] = 0;                               /* secondary cases for each class */
    used_cls_list[0] = index_0;
/*  printf("\nclass-index %d index_0 %d\n", n, index_0); */
    n_used_cls_list = 1;
    class->wts = fill( class->wts, 0.0, class->num_wts, n_data);
    class->wts[index_0] = wt_0;
    for (m=0; m<n_others; m++) {
      index_1 = new_random( n_data, used_cls_list, n_used_cls_list);
/*  printf("index_1 %d  ", index_1); */
      used_cls_list[n_used_cls_list] = index_1;
      n_used_cls_list++;
      class->wts[index_1] = wt_1;
    }
/*  printf("\n"); */
/*  for (i=0; i<class->num_wts; i++) */
/*    printf("%f  ", class->wts[i]); */
    class->w_j = max( proto_wt, clsf->min_class_wt);
  }
  free(used_list);
  free(used_cls_list);
  
  return(initialize_parameters(clsf, display_wts, delete_duplicates, initial_cycles_p,
                               log_file_fp, stream));
}
CMultitaskLogisticRegression::CMultitaskLogisticRegression(
     float64_t z, CDotFeatures* train_features, 
     CBinaryLabels* train_labels, CTaskRelation* task_relation) :
	CMultitaskLinearMachine(train_features,(CLabels*)train_labels,task_relation)
{
	initialize_parameters();
	register_parameters();
	set_z(z);
}
示例#4
0
StabilizeStepper::StabilizeStepper(std::shared_ptr<Space> space,
        std::shared_ptr<Model> model, double dx) :
    _space(space), _model(model), _dx(dx) {
    const std::size_t num_beads(_space->num_beads());
    _dt = 0;
    _mass_list = std::vector<double>(num_beads);

    initialize_parameters();

}
示例#5
0
bool Jitrino::CompileMethod(CompilationContext* cc) {
    CompilationInterface* compilationInterface = cc->getVMCompilationInterface();
    bool success = false;
    MethodDesc& methodDesc = *compilationInterface->getMethodToCompile();
    initialize_parameters(cc, methodDesc);
    
    if (methodDesc.getByteCodeSize() <= 0) {
        Log::out() << " ... Skipping because of 0 byte codes ..." << ::std::endl;
        assert(0);
    } else {
        success = compileMethod(cc);
    }
    return success;
}
示例#6
0
/* BLOCK_SET_CLSF
   15dec94 wmt: added params FILE *log_file_fp, FILE *stream
   05jan95 wmt: upgrade to ac-x code
   22mar95 wmt: ceil -> floor for n_classes & block_size; only full blocks

   Initalize a classification by dividing the database into sequential blocks,
   assigning each block to a class, and updating the class parameters.
   Blocks are are specified by giving n-classes or block-size (which overides
   n-classes when specified).  This is useful for checking the `correct'
   classification in artifical databases formed of blocks of data
   generated from some model such as those implimented through
   #'gen-formatted-data.  The function modifies the classification and
   returns the number of classes and the marginal probability.
   */
void block_set_clsf( clsf_DS clsf, int n_classes, int block_size, 
                    int delete_duplicates, int display_wts,
                    unsigned int initial_cycles_p, FILE *log_file_fp, FILE *stream)
{
  int i, n_class, n_data, base, limit, num_wts, count;
  float *wts;
  class_DS class, *classes;

  n_data = clsf->database->n_data;
  base = 0;
  limit = 0;
  if (block_size != 0) {
    block_size = max(1, min(block_size, n_data));
    n_classes = (int) floor((double) ((float) n_data / (float) block_size));
  }
  else if (n_classes != 0) {
    n_classes = max(1, min(n_classes, clsf_DS_max_n_classes( clsf)));
    block_size = (int) floor((double) ((float) n_data / (float) n_classes));
  }
  else {
    n_classes = clsf->n_classes;
    block_size = (int) floor((double) ((float) n_data / (float) n_classes));
  }

  adjust_clsf_DS_classes(clsf, n_classes);

  classes = clsf->classes;
  for (n_class = 0; n_class < n_classes; n_class++) {
    class = classes[n_class];
    num_wts = class->num_wts;
    wts = class->wts;
    count = 0;
    limit = min( limit + block_size, n_data);
    for (i=0; i<num_wts; i++)
      if ((i < base) || (i >= limit))
        wts[i] = 0.0;
      else {
        wts[i] = 1.0;
        count++;
      }
    class->w_j = max( clsf->min_class_wt, (float) count);
    base += count;
  }

  initialize_parameters(clsf, display_wts, delete_duplicates, initial_cycles_p,
                        log_file_fp, stream);
}
示例#7
0
文件: spf.cpp 项目: njuhugn/spf
SPF::SPF(model_settings* model_set, Data* dataset) {
    settings = model_set;
    data = dataset;

    // user influence
    printf("\tinitializing user influence (tau)\n");
    tau = sp_fmat(data->user_count(), data->user_count());
    logtau = sp_fmat(data->user_count(), data->user_count());
    a_tau = sp_fmat(data->user_count(), data->user_count());
    b_tau = sp_fmat(data->user_count(), data->user_count());

    // user preferences
    printf("\tinitializing user preferences (theta)\n");
    theta = fmat(settings->k, data->user_count());
    logtheta = fmat(settings->k, data->user_count());
    a_theta = fmat(settings->k, data->user_count());
    b_theta = fmat(settings->k, data->user_count());

    // item attributes
    printf("\tinitializing item attributes (beta)\n");
    printf("\t%d users and %d items\n", data->user_count(), data->item_count());
    beta = fmat(settings->k, data->item_count());
    logbeta = fmat(settings->k, data->item_count());
    a_beta = fmat(settings->k, data->item_count());
    a_beta_user = fmat(settings->k, data->item_count());
    b_beta = fmat(settings->k, data->item_count());

    delta = fvec(data->item_count());
    a_delta = fvec(data->item_count());
    b_delta = settings->b_delta + data->user_count();
    a_delta_user = fvec(data->item_count());

    // keep track of old a_beta and a_delta for SVI
    a_beta_old  = fmat(settings->k, data->item_count());
    a_beta_old.fill(settings->a_beta);
    a_delta_old = fvec(data->item_count());
    a_delta_old.fill(settings->a_delta);

    printf("\tsetting random seed\n");
    rand_gen = gsl_rng_alloc(gsl_rng_taus);
    gsl_rng_set(rand_gen, (long) settings->seed); // init the seed

    initialize_parameters();

    scale = settings->svi ? data->user_count() / settings->sample_size : 1;
}
示例#8
0
/* Train model */
int train_glove() {
    long long a, file_size;
    int b;
    FILE *fin;
    real total_cost = 0;
    fprintf(stderr, "TRAINING MODEL\n");
    
    fin = fopen(input_file, "rb");
    if(fin == NULL) {fprintf(stderr,"Unable to open cooccurrence file %s.\n",input_file); return 1;}
    fseeko(fin, 0, SEEK_END);
    file_size = ftello(fin);
    num_lines = file_size/(sizeof(CREC)); // Assuming the file isn't corrupt and consists only of CREC's
    fclose(fin);
    fprintf(stderr,"Read %lld lines.\n", num_lines);
    if(verbose > 1) fprintf(stderr,"Initializing parameters...");
    initialize_parameters();
    if(verbose > 1) fprintf(stderr,"done.\n");
    if(verbose > 0) fprintf(stderr,"vector size: %d\n", vector_size);
    if(verbose > 0) fprintf(stderr,"vocab size: %lld\n", vocab_size);
    if(verbose > 0) fprintf(stderr,"x_max: %lf\n", x_max);
    if(verbose > 0) fprintf(stderr,"alpha: %lf\n", alpha);
    pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
    lines_per_thread = (long long *) malloc(num_threads * sizeof(long long));
    
    // Lock-free asynchronous SGD
    for(b = 0; b < num_iter; b++) {
        //fprintf(stderr,"iter: %d\n", b);
        total_cost = 0;
        for (a = 0; a < num_threads - 1; a++) lines_per_thread[a] = num_lines / num_threads;
        lines_per_thread[a] = num_lines / num_threads + num_lines % num_threads;
        for (a = 0; a < num_threads; a++) pthread_create(&pt[a], NULL, glove_thread, (void *)a);
        //fprintf(stderr,"iter: %d started threads \n", b);
        for (a = 0; a < num_threads; a++) pthread_join(pt[a], NULL);
        //fprintf(stderr,"iter: %d done threads \n", b);
        for (a = 0; a < num_threads; a++) total_cost += cost[a];

        //for (a = 0; a < num_threads; a++) fprintf(stderr,"T: %d, cost: %lf\n",a,cost[a]);
        
        fprintf(stderr,"iter: %03d, cost: %lf\n", b+1, total_cost/num_lines);
    }

    //fprintf(stderr,"returning to save params");
    return save_params();
}
示例#9
0
void RBSCMConstruction::process_parameters_file(const std::string& parameters_filename)
{
  // First read in data from parameters_filename
  GetPot infile(parameters_filename);
  const unsigned int n_training_samples = infile("n_training_samples",1);
  const bool deterministic_training     = infile("deterministic_training",false);

  // Read in training_parameters_random_seed value.  This is used to
  // seed the RNG when picking the training parameters.  By default the
  // value is -1, which means use std::time to seed the RNG.
  unsigned int training_parameters_random_seed_in = static_cast<unsigned int>(-1);
  training_parameters_random_seed_in = infile("training_parameters_random_seed",
					   training_parameters_random_seed_in);
  set_training_random_seed(training_parameters_random_seed_in);

  // SCM Greedy termination tolerance
  const Real SCM_training_tolerance_in = infile("SCM_training_tolerance", SCM_training_tolerance);
  set_SCM_training_tolerance(SCM_training_tolerance_in);

  // Initialize the parameter ranges and the parameters themselves
  initialize_parameters(parameters_filename);

  std::map<std::string,bool> log_scaling;
  const RBParameters& mu = get_parameters();
  RBParameters::const_iterator it     = mu.begin();
  RBParameters::const_iterator it_end = mu.end();
  unsigned int i=0;
  for( ; it != it_end; ++it)
  {
    // Read vector-based log scaling values.  Note the intermediate conversion to
    // int... this implies log_scaling = '1 1 1...' in the input file.
//    log_scaling[i] = static_cast<bool>(infile("log_scaling", static_cast<int>(log_scaling[i]), i));

    std::string param_name = it->first;
    log_scaling[param_name] = static_cast<bool>(infile("log_scaling", 0, i));
    i++;
  }

  initialize_training_parameters(this->get_parameters_min(),
                                 this->get_parameters_max(),
                                 n_training_samples,
                                 log_scaling,
                                 deterministic_training);   // use deterministic parameters
}
 bool OnInit()
 {
     char **my_argv = new char*[argc];
     for (int i=0;i<argc;++i)
     {
         wxString mystr(argv[i]);
         my_argv[i] = new char[mystr.size()+1];
         strcpy(my_argv[i], mystr.To8BitData());
     }
     param *p = param::instance();
     initialize_parameters(p);
     if(!p->parse(argc,my_argv)) return false;
     m_confg_visitor = new simulated_annealing::wx::configuration_visitor((wxFrame *)NULL, wxID_ANY, _("librjmcmc: rectangular building footprint extraction"), wxDefaultPosition, wxSize(600,600) );
     m_confg_visitor->SetIcon(wxICON(IGN));
     m_param_visitor = new simulated_annealing::wx::parameters_visitor(m_confg_visitor);
     m_chart_visitor = new simulated_annealing::wx::chart_visitor(m_confg_visitor);
     m_visitor = new visitor(&m_log_visitor,m_confg_visitor,m_param_visitor,m_chart_visitor);
     m_confg_visitor->controler(this);
     return true;
 }
示例#11
0
void RBParametrized::initialize_parameters (const std::string& parameters_filename)
{
  GetPot infile(parameters_filename);

  const unsigned int n_parameters = infile("n_parameters",1);
  RBParameters mu_min_in;
  RBParameters mu_max_in;
  RBParameters initial_mu_in;
  for(unsigned int i=0; i<n_parameters; i++)
  {
    // Read in the parameter names
    std::string param_name = infile("parameter_names", "NONE", i);

    for(unsigned int j=0; j<3; j++)
    {
      if(j==0)
      {
        Real min_val = infile(param_name, 0., j);
        mu_min_in.set_value(param_name, min_val);
      }
      else if(j==1)
      {
        Real max_val = infile(param_name, 0., j);
        mu_max_in.set_value(param_name, max_val);
      }
      else
      {
        Real init_val = infile(param_name, 0., j);
        initial_mu_in.set_value(param_name, init_val);
      }
    }
  }

  // Initialize the parameter ranges and set the parameters to mu_min_vector
  initialize_parameters(mu_min_in, mu_max_in, initial_mu_in);
}
示例#12
0
void RBParametrized::read_parameter_ranges_from_file(const std::string& file_name,
                                                     const bool read_binary_data)
{
  // The reading mode: DECODE for binary, READ for ASCII
  XdrMODE mode = read_binary_data ? DECODE : READ;

  // Read in the parameter ranges
  Xdr parameter_ranges_in(file_name, mode);
  unsigned int n_params;
  parameter_ranges_in >> n_params;
  RBParameters param_min;
  for(unsigned int i=0; i<n_params; i++)
  {
    std::string param_name;
    Real param_value;

    parameter_ranges_in >> param_name;
    parameter_ranges_in >> param_value;

    param_min.set_value(param_name, param_value);
  }
  RBParameters param_max;
  for(unsigned int i=0; i<n_params; i++)
  {
    std::string param_name;
    Real param_value;

    parameter_ranges_in >> param_name;
    parameter_ranges_in >> param_value;

    param_max.set_value(param_name, param_value);
  }
  parameter_ranges_in.close();

  initialize_parameters(param_min, param_max, param_min);
}
示例#13
0
void RBParametrized::initialize_parameters(const RBParametrized& rb_parametrized)
{
  initialize_parameters(rb_parametrized.get_parameters_min(),
                        rb_parametrized.get_parameters_max(),
                        rb_parametrized.get_parameters());
}
示例#14
0
void RBSCMConstruction::process_parameters_file(const std::string& parameters_filename)
{
  // First read in data from parameters_filename
  GetPot infile(parameters_filename);
  const unsigned int n_training_samples = infile("n_training_samples",1);
  const bool deterministic_training     = infile("deterministic_training",false);

  // Read in training_parameters_random_seed value.  This is used to
  // seed the RNG when picking the training parameters.  By default the
  // value is -1, which means use std::time to seed the RNG.
  unsigned int training_parameters_random_seed_in = static_cast<int>(-1);
  training_parameters_random_seed_in = infile("training_parameters_random_seed",
                                              training_parameters_random_seed_in);
  set_training_random_seed(training_parameters_random_seed_in);

  // SCM Greedy termination tolerance
  const Real SCM_training_tolerance_in = infile("SCM_training_tolerance", SCM_training_tolerance);
  set_SCM_training_tolerance(SCM_training_tolerance_in);

  // Initialize the parameter ranges and the parameters themselves
  unsigned int n_continuous_parameters = infile.vector_variable_size("parameter_names");
  RBParameters mu_min_in;
  RBParameters mu_max_in;
  for(unsigned int i=0; i<n_continuous_parameters; i++)
    {
      // Read in the parameter names
      std::string param_name = infile("parameter_names", "NONE", i);

      {
        Real min_val = infile(param_name, 0., 0);
        mu_min_in.set_value(param_name, min_val);
      }

      {
        Real max_val = infile(param_name, 0., 1);
        mu_max_in.set_value(param_name, max_val);
      }
    }

  std::map< std::string, std::vector<Real> > discrete_parameter_values_in;

  unsigned int n_discrete_parameters = infile.vector_variable_size("discrete_parameter_names");
  for(unsigned int i=0; i<n_discrete_parameters; i++)
    {
      std::string param_name = infile("discrete_parameter_names", "NONE", i);

      unsigned int n_vals_for_param = infile.vector_variable_size(param_name);
      std::vector<Real> vals_for_param(n_vals_for_param);
      for(unsigned int j=0; j<vals_for_param.size(); j++)
        {
          vals_for_param[j] = infile(param_name, 0., j);
        }

      discrete_parameter_values_in[param_name] = vals_for_param;
    }

  initialize_parameters(mu_min_in, mu_max_in, discrete_parameter_values_in);

  std::map<std::string,bool> log_scaling;
  const RBParameters& mu = get_parameters();
  RBParameters::const_iterator it     = mu.begin();
  RBParameters::const_iterator it_end = mu.end();
  unsigned int i=0;
  for( ; it != it_end; ++it)
    {
      std::string param_name = it->first;
      log_scaling[param_name] = static_cast<bool>(infile("log_scaling", 0, i));
      i++;
    }

  initialize_training_parameters(this->get_parameters_min(),
                                 this->get_parameters_max(),
                                 n_training_samples,
                                 log_scaling,
                                 deterministic_training);   // use deterministic parameters
}
CMultitaskLogisticRegression::CMultitaskLogisticRegression() :
	CMultitaskLinearMachine()
{
	initialize_parameters();
	register_parameters();
}
void 
TaggerMergingData::read_tagsclusters(istream& ftagsclus) {

  int ntags;
  set<int> empty_set;

  ftagsclus>>COARSE_N;
  coarse2fine.resize(COARSE_N, empty_set);
  cerr<<"Reading fine tags merging structure ...\n";
  int finetag;
  for(int i=0; i<COARSE_N; i++) {
    ftagsclus>>ntags;
    while(ntags>0) {
      ftagsclus>>finetag;
      fine2coarse[finetag]=i;
      coarse2fine[i].insert(finetag);
      ntags--;
    }
  }

  ftagsclus>>COARSE_M;
  cerr<<"Reading ambiguity classes merging structure ...\n";
  int nambs;
  int fineamb;
  coarseamb2fineamb.resize(COARSE_M, empty_set);
  for(int i=0; i<COARSE_M; i++) {
    ftagsclus>>nambs;
    while(nambs>0) {
      ftagsclus>>fineamb;
      fineamb2coarseamb[fineamb]=i;
      coarseamb2fineamb[i].insert(fineamb);
      nambs--;
    }
  }

  cerr<<"Reading ambiguity classes of coarse tags ...\n";
  set<int> coarse_amb_class;
  int coarsetag;
  for(int i=0; i<COARSE_M; i++) {
    ftagsclus>>ntags;
    coarse_amb_class.clear();
    while(ntags>0) {
      ftagsclus>>coarsetag;
      coarse_amb_class.insert(coarsetag);
      ntags--;
    }
    if(coarse_amb_class.size()>0)
      coarse_output.add(coarse_amb_class);
  }

  cerr<<TaggerData::getN()<<" fine tags, "<<getN()<<" coarse tags.\n";
  cerr<<TaggerData::getM()<<" amb. classes of fine tags, "<<getM()<<" amb. classes of coarse tags.\n";

  initialize_parameters();

  /*  
      vector<set<int> >::iterator itv;
      set<int>::iterator its;
      cout<<COARSE_N<<"\n";
      for(int i=0; i<COARSE_N; i++) {
      cout<<coarse2fine[i].size()<<" ";
      for(its=coarse2fine[i].begin(); its!=coarse2fine[i].end(); its++)
      cout<<*its<<" ";
      cout<<"\n";
      }

      cout<<COARSE_M<<"\n";
      for(int k=0; k<COARSE_M; k++) {
      cout<<coarseamb2fineamb[k].size()<<" ";
      for(its=coarseamb2fineamb[k].begin(); its!=coarseamb2fineamb[k].end(); its++)
      cout<<*its<<" ";
      cout<<"\n";
      }

      for(int k=0; k<coarse_output.size(); k++) {
      set<int> amb_class=coarse_output[k];
      cout<<amb_class.size()<<" ";
      for(its=amb_class.begin(); its!=amb_class.end(); its++)
      cout<<*its<<" ";
      cout<<"\n";
      }
  */
}
示例#17
0
void Module_DMAP::execute(const Options & options) {
	clock_t started = clock();
	processed = 0;

	initialize_parameters(options);

	my_rank = MPI::COMM_WORLD.Get_rank();
	nprocs = MPI::COMM_WORLD.Get_size();
	proc_name = new char[MPI_MAX_PROCESSOR_NAME];
	int resultlen;
	MPI::Get_processor_name(proc_name, resultlen);

	// Check for the correct number of files
	if (my_rank == 0) {
		stringstream filename_numberfile;
		filename_numberfile << options.reference_file << "_n.dht";
		ifstream nf(filename_numberfile.str().c_str());
		if (nf.fail()) {
			ERROR_CHANNEL << "File " << filename_numberfile.str() << " not found! Check the reference name!" << endl;
			exit(5);
		}
		string s;
		getline(nf,s);
		int n_files = -1;
		n_files = atoi(s.c_str());
		DEFAULT_CHANNEL << "Number of rNA files in the set: " << s << endl;
		if (n_files != nprocs) {
			ERROR_CHANNEL << "Wrong number of files: expected " << nprocs << " files but the data structure was built for " << n_files << " processes!" << endl;
			exit(5);
		} else {
			DEFAULT_CHANNEL << '[' << my_rank << "] Found " << n_files << '/' << nprocs << " files" << endl;
		}
	}

	if (my_rank == 0)
		DEFAULT_CHANNEL << '[' << my_rank << "] Reading process started on machine " << proc_name << endl;
	else if (my_rank == nprocs-1)
		DEFAULT_CHANNEL << '[' << my_rank << "] Writing process started on machine " << proc_name << endl;
	else
		DEFAULT_CHANNEL << '[' << my_rank << "] Slave process " << my_rank << "/" << (nprocs-1) << " started on machine " << proc_name << endl;

	execute_generic_worker(options);

	if (my_rank == 0)
		DEFAULT_CHANNEL << '[' << my_rank << "] Reading process waiting for finalize on " << proc_name << endl;
	else if (my_rank == nprocs-1)
		DEFAULT_CHANNEL << '[' << my_rank << "] Writing process waiting for finalize on " << proc_name << endl;
	else
		DEFAULT_CHANNEL << '[' << my_rank << "] Slave process " << my_rank << "/" << (nprocs-1) << " waiting for finalize on " << proc_name << endl;

	MPI::Finalize();

	if (my_rank == 0) {
		clock_t finished = clock();
		double dif = (finished - started) / (double)CLOCKS_PER_SEC;

		DEFAULT_CHANNEL << "Time spent: " << dif << "s (";
		print_formatted_time(DEFAULT_CHANNEL, dif);
		DEFAULT_CHANNEL << ')' << endl;
		//DEFAULT_CHANNEL << "Queries per second: " << (processed/dif)	<<endl;
	}

}
示例#18
0
文件: hmm.cpp 项目: sjneph/hmm
Input::Input(int argc, char** argv) : _niters(1), _nstates(1), _nsymbols(0),
                                      _verbose(false), _read_params(false),
                                      _seed(std::time(NULL)) {
  for ( int i = 1; i < argc; ++i ) {
    if ( std::string(argv[i]) == "--help" )
      throw(Help());
    else if ( std::string(argv[i]) == "--version" )
      throw(Version());
  } // for

  if ( argc == 1 )
    throw(NoInput());
  if ( argc < 4 )
    throw("Wrong number of args: see --help");

  const std::string ints = "0123456789";
  int nextc = 1;
  const std::string todo = argv[nextc++];
  std::string next = argv[nextc++];
  if ( todo == "train" || todo == "train-and-decode" ) {
    if ( (argc < 5) || (argc > 7) )
      throw("Wrong number of args for '" + todo + ".  See --help");
    _operation = Ops::TRAIN;
    if ( todo == "train-and-decode" )
      _operation = Ops::TRAIN_AND_DECODE;
    int count = argc;
    while ( next.find_first_of("--seed") == 0 || next.find_first_of("--verbose") == 0 ) {
      if ( --count == 0 )
        break;
      if ( next == "--verbose" ) {
        _verbose = true;
      } else {
        auto v = split(next, "=");
        if ( v.size() != 2 )
          throw("Bad number.  Expect a +integer for " + next + ".  See --help");
        if ( v[1].find_first_not_of(ints) != std::string::npos )
          throw("Bad number. Expect a +integer for " + next + ".  See --help");
        _seed = std::atoi(v[1].c_str());
        std::srand(_seed);
      }
      if ( nextc != argc )
        next = argv[nextc++];
      else
        break;
    } // while
    if ( count != 5 )
      throw("Wrong number (or order) of arguments for " + todo + ".  See --help");
    if ( next.find_first_not_of(ints) != std::string::npos )
      throw("Bad argument: expect a +integer for <number-states>.  See --help");
    _nstates = std::atoi(next.c_str());
    next = argv[nextc++];
    if ( next.find_first_not_of(ints) != std::string::npos )
      throw("Bad argument - expect a +integer for <number-iterations>.  See --help");
    _niters = std::atoi(next.c_str());
  } else if ( todo == "probability" ) {
    if ( argc != 4 )
      throw("Wrong number of args for '" + todo + ".  See --help");
    _operation = Ops::PROB;
    _params = next;
    std::ifstream f(_params.c_str());
    if (!f)
      throw("Input file not found: " + _params);
    read_parameters();
  } else if ( todo == "decode" ) {
    if ( argc != 4 )
      throw("Wrong number of args for '" + todo + ".  See --help");
    _operation = Ops::DECODE;
    _params = next;
    std::ifstream f(_params.c_str());
    if (!f)
      throw("Input file not found: " + _params);
    read_parameters();
  } else {
    throw("Unknown operation: '" + todo + "'.  See --help.");
  }

  if ( _niters <= 0 || _niters > _MAXITER )
    throw("Bad number of '" + todo + "' iterations");
  else if ( _nstates <= 0 || _nstates > _MAXSTATES )
    throw("Bad number of '" + todo + "' states");

  _src = argv[nextc++];
  std::ifstream f(_src.c_str());
  if (!f)
    throw("Input file not found: " + _src);

  read_data(); // read observations; get _nsymbols from the data

  if ( todo == "train" || todo == "train-and-decode" ) {
    initialize_parameters(); // only after read_data() due to _nsymbols
    if ( _nsymbols == 0 )
      throw("Didn't find any data");
  }
}
示例#19
0
/* Train model */
int train_glove() {
    long long a, file_size;
    int save_params_return_code;
    int b;
    FILE *fin;
    real total_cost = 0;

    fprintf(stderr, "TRAINING MODEL\n");
    
    fin = fopen(input_file, "rb");
    if (fin == NULL) {fprintf(stderr,"Unable to open cooccurrence file %s.\n",input_file); return 1;}
    fseeko(fin, 0, SEEK_END);
    file_size = ftello(fin);
    num_lines = file_size/(sizeof(CREC)); // Assuming the file isn't corrupt and consists only of CREC's
    fclose(fin);
    
    fprintf(stderr,"Read %lld lines.\n", num_lines);
    if (verbose > 1) fprintf(stderr,"Initializing parameters...");
    initialize_parameters();
    if (verbose > 1) fprintf(stderr,"done.\n");
    if (verbose > 0) fprintf(stderr,"vector size: %d\n", vector_size);
    //!@#$%^&*
    if (verbose > 0) fprintf(stderr,"words size: %lld\n", words_size);
    if (verbose > 0) fprintf(stderr,"contexts size: %lld\n", contexts_size);
    if (verbose > 0) fprintf(stderr,"x_max: %lf\n", x_max);
    if (verbose > 0) fprintf(stderr,"alpha: %lf\n", alpha);
    pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
    lines_per_thread = (long long *) malloc(num_threads * sizeof(long long));
    
    time_t rawtime;
    struct tm *info;
    char time_buffer[80];
    // Lock-free asynchronous SGD
    for (b = 0; b < num_iter; b++) {
        total_cost = 0;
        for (a = 0; a < num_threads - 1; a++) lines_per_thread[a] = num_lines / num_threads;
        lines_per_thread[a] = num_lines / num_threads + num_lines % num_threads;
        long long *thread_ids = (long long*)malloc(sizeof(long long) * num_threads);
        for (a = 0; a < num_threads; a++) thread_ids[a] = a;
        for (a = 0; a < num_threads; a++) pthread_create(&pt[a], NULL, glove_thread, (void *)&thread_ids[a]);
        for (a = 0; a < num_threads; a++) pthread_join(pt[a], NULL);
        for (a = 0; a < num_threads; a++) total_cost += cost[a];
        free(thread_ids);

        time(&rawtime);
        info = localtime(&rawtime);
        strftime(time_buffer,80,"%x - %I:%M.%S%p", info);
        fprintf(stderr, "%s, iter: %03d, cost: %lf\n", time_buffer,  b+1, total_cost/num_lines);

        if (checkpoint_every > 0 && (b + 1) % checkpoint_every == 0) {
            fprintf(stderr,"    saving itermediate parameters for iter %03d...", b+1);
            save_params_return_code = save_params(b+1);
            if (save_params_return_code != 0)
                return save_params_return_code;
            fprintf(stderr,"done.\n");
        }

    }
    free(pt);
    free(lines_per_thread);
    return save_params(0);

}
示例#20
0
void inspector(std::vector<Building>& bldgs,Lot* lot,std::ofstream& fs)
{
    std::vector<Cuboid> boxes;

    for(size_t i=0;i<bldgs.size();++i)
    {
        OGRPoint pt;
        bldgs[i].footprint()->Centroid(&pt);
        Point_2 c(pt.getX(),pt.getY());
        boxes.push_back(Cuboid(c,bldgs[i].width(),bldgs[i].length(),bldgs[i].theta(),bldgs[i].height()));
    }

    double area=0,areaF=0,hF = lot->ruleGeom()->hFloor();
    Param p;
    initialize_parameters(&p);

    for(size_t i=0;i<boxes.size();++i )
    {
        std::cout<<"\nbldg "<<i<<"\n";
        fs<<"\nbldg "<<i<<"\n";

        area+=boxes[i].area();
        areaF+=boxes[i].area()*(int)(boxes[i].h()/hF);

        //width
        std::cout<<"  ruleWidth: "<<lot->ruleGeom()->strW()<<"\n";
        std::cout<<"    --width = "<<boxes[i].width()<<"\n";
        fs<<"  ruleWidth: "<<lot->ruleGeom()->strW()<<"\n";
        fs<<"    --width = "<<boxes[i].width()<<"\n";

        //length
        std::cout<<"  ruleLength: "<<lot->ruleGeom()->strL()<<"\n";
        std::cout<<"    --length = "<<boxes[i].length()<<"\n";
        fs<<"  ruleLength: "<<lot->ruleGeom()->strL()<<"\n";
        fs<<"    --length = "<<boxes[i].length()<<"\n";

        //height
        std::cout<<"  ruleHeight: "<<lot->ruleGeom()->strH()<<"\n";
        std::cout<<"    --h = "<<boxes[i].h()<<"\n";
        fs<<"  ruleHeight: "<<lot->ruleGeom()->strH()<<"\n";
        fs<<"    --h = "<<boxes[i].h()<<"\n";

        //distance to borders
        std::map<std::string,double> dist,hasWindow;
        std::map< std::string,double >::iterator itD;
        std::map<Var,double> var_value;

        lot->dist2borders(boxes[i],dist,hasWindow);
        for(itD=dist.begin();itD!=dist.end();++itD)
            var_value.insert(std::make_pair(Var("d"+itD->first),itD->second));

        for(itD=hasWindow.begin();itD!=hasWindow.end();++itD)
            var_value.insert(std::make_pair(Var("hasWindow"+itD->first),itD->second));

        var_value.insert(std::make_pair(Var("h"),boxes[i].h()));

        double eFront=0,eSide=0,eBack=0;
        if(lot->hasRule(RuleType::DistFront))
        {
            std::cout<<"  ruleFront: "<<lot->ruleEnergy(RuleType::DistFront)->ruleString()<<"\n";
            fs<<"  ruleFront: "<<lot->ruleEnergy(RuleType::DistFront)->ruleString()<<"\n";

            for(itD=dist.begin();itD!=dist.end();++itD)
            {
                if(itD->first.find("Front")==std::string::npos)
                    continue;
                std::cout<<"    --"<<"d"+itD->first<<" = "<<itD->second<<" hasWindow:"<<hasWindow[itD->first]<<"\n";
                fs<<"    --"<<"d"+itD->first<<" = "<<itD->second<<" hasWindow:"<<hasWindow[itD->first]<<"\n";
            }

            eFront = p.template get<double>("wdborder")*p.template get<double>("erej")*(lot->ruleEnergy(RuleType::DistFront))->energy(var_value);
            std::cout<<"  eFront = "<<eFront<< "\n";
            fs<<"  eFront = "<<eFront<<"\n";
        }

        if(lot->hasRule(RuleType::DistSide))
        {
            std::cout<<"  ruleSide: "<<lot->ruleEnergy(RuleType::DistSide)->ruleString()<<"\n";
            fs<<"  ruleSide: "<<lot->ruleEnergy(RuleType::DistSide)->ruleString()<<"\n";

            for(itD=dist.begin();itD!=dist.end();++itD)
            {
                if(itD->first.find("Side")==std::string::npos)
                    continue;

                std::cout<<"    --"<<"d"+itD->first<<" = "<<itD->second<<" hasWindow:"<<hasWindow[itD->first]<<"\n";
                fs<<"    --"<<"d"+itD->first<<" = "<<itD->second<<" hasWindow:"<<hasWindow[itD->first]<<"\n";
            }


            eSide = p.template get<double>("wdborder")*p.template get<double>("erej")*(lot->ruleEnergy(RuleType::DistSide))->energy(var_value);
            std::cout<<"  eSide = "<<eSide<<"\n";
            fs<<"  eSide = "<<eSide<<"\n";

        }

        if(lot->hasRule(RuleType::DistBack))
        {
            std::cout<<"  ruleBack: "<<lot->ruleEnergy(RuleType::DistBack)->ruleString()<<"\n";
            fs<<"  ruleBack: "<<lot->ruleEnergy(RuleType::DistBack)->ruleString()<<"\n";

            for(itD=dist.begin();itD!=dist.end();++itD)
            {
                if(itD->first.find("Back")==std::string::npos)
                    continue;
                std::cout<<"    --"<<"d"+itD->first<<" = "<<itD->second<<" hasWindow:"<<hasWindow[itD->first]<<"\n";
                fs<<"    --"<<"d"+itD->first<<" = "<<itD->second<<" hasWindow:"<<hasWindow[itD->first]<<"\n";
            }


            eBack = p.template get<double>("wdborder")*p.template get<double>("erej")*(lot->ruleEnergy(RuleType::DistBack))->energy(var_value);
            std::cout<<"  eBack = "<<eBack<<"\n";
            fs<<"  eBack = "<<eBack<<"\n";

        }

    }

    if(lot->hasRule(RuleType::DistPair))
    {
        std::cout<<"\n ruleDPair: "<<lot->ruleEnergy(RuleType::DistPair)->ruleString()<<"\n";
        fs<<"\n ruleDPair: "<<lot->ruleEnergy(RuleType::DistPair)->ruleString()<<"\n";

        int isValidDistBin = 1;
        if(!lot->ruleEnergy(RuleType::DistPair)->isConditional())
        {

            double dBin,hHigh;
            for(size_t i=0;i<boxes.size()-1;++i)
            {
                for(size_t j=i+1;j<boxes.size();++j)
                {
                    dBin = boxes[i].distance2cuboid(boxes[j]);
                    hHigh = std::max(boxes[i].h(),boxes[j].h());

                    if(geometry::do_intersect(boxes[i],boxes[j]))
                        dBin = -dBin;

                    std::map<Var,double> varValue;
                    varValue.insert(std::make_pair(Var("dPair"),dBin));
                    varValue.insert(std::make_pair(Var("hHigh"),hHigh));

                    if(lot->ruleEnergy(RuleType::DistPair)->isValid(varValue))
                    {
                        std::cout<<"  --dPair"<<i<<"_"<<j<<"="<<dBin<<"\n";
                        fs<<"  --dPair"<<i<<"_"<<j<<"="<<dBin<<"\n";
                        continue;
                    }


                    isValidDistBin = 0;
                    std::cout<<"  --violation: dPair"<<i<<"_"<<j<<"="<<dBin<<"\n";
                    fs<<"  --violation: dPair"<<i<<"_"<<j<<"="<<dBin<<"\n";

                }
            }

        }


        else
        {
            double dBin,hHigh,hLow;
            int hasWindowPair,hasWindowHigh,hasWindowLow;
            for(size_t i=0;i<boxes.size()-1;++i)
            {
                for(size_t j=i+1;j<boxes.size();++j)
                {
                    dBin = boxes[i].distance2cuboid(boxes[j],lot->lengthHasWindow(),hasWindowPair,hasWindowHigh,hasWindowLow);
                    hHigh = std::max((double)boxes[i].h(),(double)boxes[j].h());
                    hLow = std::min((double)boxes[i].h(),(double)boxes[j].h());

                    if(geometry::do_intersect(boxes[i],boxes[j]))
                        dBin = -dBin;

                    std::map<Var,double> varValue;
                    varValue.insert(std::make_pair(Var("hasWindowPair"),hasWindowPair));
                    varValue.insert(std::make_pair(Var("hasWindowHigh"),hasWindowHigh));
                    varValue.insert(std::make_pair(Var("hasWindowLow"),hasWindowLow));
                    varValue.insert(std::make_pair(Var("dPair"),dBin));
                    varValue.insert(std::make_pair(Var("hHigh"),hHigh));
                    varValue.insert(std::make_pair(Var("hLow"),hLow));

                    if(lot->ruleEnergy(RuleType::DistPair)->isValid(varValue))
                    {
                        std::cout<<"  --dPair"<<i<<"_"<<j<<"="<<dBin<<" hasWindowHigh:"<<hasWindowHigh<<" hasWindowLow:"<<hasWindowLow<<"\n";
                        fs<<"  --dPair"<<i<<"_"<<j<<"="<<dBin<<" hasWindowHigh:"<<hasWindowHigh<<" hasWindowLow:"<<hasWindowLow<<"\n";
                        continue;
                    }


                    isValidDistBin = 0;
                    std::cout<<"  --violation: dPair"<<i<<"_"<<j<<"="<<dBin<<" hasWindowHigh:"<<hasWindowHigh<<" hasWindowLow:"<<hasWindowLow<<"\n";
                    fs<<"  --violation: dPair"<<i<<"_"<<j<<"="<<dBin<<" hasWindowHigh:"<<hasWindowHigh<<" hasWindowLow:"<<hasWindowLow<<"\n";


                }
            }


        }

        if(isValidDistBin)
        {
            std::cout<<"  no violation\n";
            fs<<"  no Violation\n";
        }

    }

    if(lot->hasRule(RuleType::HeightDiff))
    {
        std::cout<<"\n ruleHDiff: "<<lot->ruleEnergy(RuleType::HeightDiff)->ruleString()<<"\n";
        fs<<"\n ruleHDiff: "<<lot->ruleEnergy(RuleType::HeightDiff)->ruleString()<<"\n";

        int isValidHDiff = 1;

        double dBin,hDiff,hHigh;
        for(size_t i=0;i<boxes.size()-1;++i)
        {
            for(size_t j=i+1;j<boxes.size();++j)
            {
                dBin = boxes[i].distance2cuboid(boxes[j]);
                hHigh = std::max((double)boxes[i].h(),(double)boxes[j].h());

                if(geometry::do_intersect(boxes[i],boxes[j]))
                    dBin = -dBin;

                if(dBin>=hHigh)
                    continue;

                hDiff = std::abs(boxes[i].h()-boxes[j].h());
                std::map<Var,double> varValue;
                varValue.insert(std::make_pair(Var("hDiff"),hDiff));


                if(lot->ruleEnergy(RuleType::HeightDiff)->isValid(varValue))
                    continue;

                isValidHDiff = 0;
                std::cout<<"  --violation: hDiff="<<hDiff<<"\n";
                fs<<"  --violation: hDiff="<<hDiff<<"\n";
            }
        }

        if(isValidHDiff)
        {
            std::cout<<"  no violation\n";
            fs<<"  no Violation\n";
        }
    }

    double lcr = area/lot->area(), far = areaF/lot->area();
    std::cout<<"\n ------Global rules-----\n";
    fs<<"\n ------Global rules-----\n";

    std::cout<<"ruleLCR: "<<lot->ruleEnergy(RuleType::LCR)->ruleString()<<"\n";
    std::cout<<"  --LCR = "<<lcr<<"\n";
    fs<<"ruleLCR: "<<lot->ruleEnergy(RuleType::LCR)->ruleString()<<"\n";
    fs<<"  --LCR = "<<lcr<<"\n";

    std::cout<<"ruleFAR: "<<lot->ruleEnergy(RuleType::FAR)->ruleString()<<"\n";
    std::cout<<"  --FAR = "<<far<<"\n";
    fs<<"ruleFAR: "<<lot->ruleEnergy(RuleType::FAR)->ruleString()<<"\n";
    fs<<"  --FAR = "<<far<<"\n";
}