예제 #1
0
파일: LoadModel.cpp 프로젝트: openube/ELL
 void SaveModel(const model::Model& model, const std::string& filename)
 {
     if (!utilities::IsFileWritable(filename))
     {
         throw utilities::SystemException(utilities::SystemExceptionErrors::fileNotWritable);
     }
     auto filestream = utilities::OpenOfstream(filename);
     SaveModel(model, filestream);
 }
예제 #2
0
파일: imod.cpp 프로젝트: imod-mirror/IMOD
/* DNM 2/7/02: keep it from sending up another window if one is already up */
void imod_quit(void)
{
  int done, err;

  if (imodDebug('T') || (windowKeys && strchr(windowKeys, '2')) ||
      !imod_model_changed(Model)) {
    imod_cleanup_autosave();
    imod_exit(0);
    return;
  }

  done = dia_choice("Save model before quitting?", "Yes", "No", "Cancel");

  switch(done){
  case 1:
          
    if ((err = SaveModel(Model))){
      if (err == IMOD_IO_SAVE_CANCEL)
        break;
      wprint("%s\n", imodIOGetErrorString());
      wprint("Model not saved; quit aborted.\n");
      break;
    }else{
      imod_cleanup_autosave();
      imod_exit(0);
      return;
    }
    break;

  case 2:
    imod_cleanup_autosave();
    /* DNM: It used to make one last autosave and quit, but if the user
       says NO, then just clean up and quit! */
    imod_exit(0);
    return;
    break;

  case 3:
    break;

  default:
    break;
  }
  return;
}
예제 #3
0
파일: HInit.c 프로젝트: songzhengxuan/kata
int main(int argc, char *argv[])
{
   char *datafn, *s;
   int nSeg;
   void Initialise(void);
   void LoadFile(char *fn);
   void EstimateModel(void);
   void SaveModel(char *outfn);
   
   if(InitShell(argc,argv,hinit_version,hinit_vc_id)<SUCCESS)
      HError(2100,"HInit: InitShell failed");
   InitMem();   InitLabel();
   InitMath();  InitSigP();
   InitWave();  InitAudio();
   InitVQ();    InitModel();
   if(InitParm()<SUCCESS)  
      HError(2100,"HInit: InitParm failed");
   InitTrain(); InitUtil();

   if (!InfoPrinted() && NumArgs() == 0)
      ReportUsage();
   if (NumArgs() == 0) Exit(0);
   SetConfParms();

   CreateHMMSet(&hset,&gstack,FALSE);
   while (NextArg() == SWITCHARG) {
      s = GetSwtArg();
      if (strlen(s)!=1) 
         HError(2119,"HInit: Bad switch %s; must be single letter",s);
      switch(s[0]){
      case 'e':
         epsilon = GetChkedFlt(0.0,1.0,s); break;
      case 'i':
         maxIter = GetChkedInt(0,100,s); break;
      case 'l':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: Segment label expected");
         segLab = GetStrArg();
         break;
      case 'm':
         minSeg = GetChkedInt(1,1000,s); break;
      case 'n':
         newModel = FALSE; break;
      case 'o':
         outfn = GetStrArg();
         break;
      case 'u':
         SetuFlags(); break;
      case 'v':
         minVar = GetChkedFlt(0.0,10.0,s); break;
      case 'w':
         mixWeightFloor = MINMIX * GetChkedFlt(0.0,10000.0,s); 
         break;
      case 'B':
         saveBinary = TRUE;
         break;
      case 'F':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: Data File format expected");
         if((dff = Str2Format(GetStrArg())) == ALIEN)
            HError(-2189,"HInit: Warning ALIEN Data file format set");
         break;
      case 'G':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: Label File format expected");
         if((lff = Str2Format(GetStrArg())) == ALIEN)
            HError(-2189,"HInit: Warning ALIEN Label file format set");
         break;
      case 'H':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: HMM macro file name expected");
         AddMMF(&hset,GetStrArg());
         break;
      case 'I':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: MLF file name expected");
         LoadMasterFile(GetStrArg());
         break;
      case 'L':
         if (NextArg()!=STRINGARG)
            HError(2119,"HInit: Label file directory expected");
         labDir = GetStrArg(); break;
      case 'M':
         if (NextArg()!=STRINGARG)
            HError(2119,"HInit: Output macro file directory expected");
         outDir = GetStrArg();
         break;
      case 'T':
         if (NextArg() != INTARG)
            HError(2119,"HInit: Trace value expected");
         trace = GetChkedInt(0,01777,s);
         break;
      case 'X':
         if (NextArg()!=STRINGARG)
            HError(2119,"HInit: Label file extension expected");
         labExt = GetStrArg(); break;
      default:
         HError(2119,"HInit: Unknown switch %s",s);
      }
   }
   if (NextArg()!=STRINGARG)
      HError(2119,"HInit: source HMM file name expected");
   hmmfn = GetStrArg();
   Initialise();
   do {
      if (NextArg()!=STRINGARG)
         HError(2119,"HInit: training data file name expected");
      datafn = GetStrArg();
      LoadFile(datafn);
   } while (NumArgs()>0);
   nSeg = NumSegs(segStore);
   if (nSeg < minSeg)
      HError(2121,"HInit: Too Few Observation Sequences [%d]",nSeg);
   if (trace&T_TOP) {
      printf("%d Observation Sequences Loaded\n",nSeg);
      fflush(stdout);
   }
   EstimateModel();
   SaveModel(outfn);
   if (trace&T_TOP)
      printf("Output written to directory %s\n",
             outDir==NULL?"current":outDir);
   Exit(0);
   return (0);          /* never reached -- make compiler happy */
}
예제 #4
0
파일: cp-offline.cpp 프로젝트: fated/libcp
int main(int argc, char *argv[]) {
  char train_file_name[256];
  char test_file_name[256];
  char output_file_name[256];
  char model_file_name[256];
  struct Problem *train, *test;
  struct Model *model;
  int num_correct = 0, num_empty = 0, num_multi = 0, num_incl = 0;
  double avg_conf = 0, avg_cred = 0;
  const char *error_message;

  ParseCommandLine(argc, argv, train_file_name, test_file_name, output_file_name, model_file_name);
  error_message = CheckParameter(&param);

  if (error_message != NULL) {
    std::cerr << error_message << std::endl;
    exit(EXIT_FAILURE);
  }

  train = ReadProblem(train_file_name);
  test = ReadProblem(test_file_name);

  std::ofstream output_file(output_file_name);
  if (!output_file.is_open()) {
    std::cerr << "Unable to open output file: " << output_file_name << std::endl;
    exit(EXIT_FAILURE);
  }

  std::chrono::time_point<std::chrono::steady_clock> start_time = std::chrono::high_resolution_clock::now();

  if (param.load_model == 1) {
    model = LoadModel(model_file_name);
    if (model == NULL) {
      exit(EXIT_FAILURE);
    }
  } else {
    model = TrainCP(train, &param);
  }

  if (param.save_model == 1) {
    if (SaveModel(model_file_name, model) != 0) {
      std::cerr << "Unable to save model file" << std::endl;
    }
  }

  for (int i = 0; i < test->num_ex; ++i) {
    double conf, cred;
    std::vector<int> predict_label;

    predict_label = PredictCP(train, model, test->x[i], conf, cred);

    avg_conf += conf;
    avg_cred += cred;

    output_file << std::resetiosflags(std::ios::fixed) << test->y[i] << ' ' << predict_label[0] << ' '
                << std::setiosflags(std::ios::fixed) << conf << ' ' << cred;
    if (predict_label[0] == test->y[i]) {
      ++num_correct;
    }

    if (predict_label.size() == 1) {
      ++num_empty;
      output_file << " Empty\n";
    } else {
      output_file << " set:";
      for (size_t j = 1; j < predict_label.size(); ++j) {
        output_file << ' ' << predict_label[j];
        if (predict_label[j] == test->y[i]) {
          ++num_incl;
        }
      }
      if (predict_label.size() > 2) {
        ++num_multi;
        output_file << " Multi\n";
      } else {
        output_file << " Single\n";
      }
    }
    std::vector<int>().swap(predict_label);
  }
  avg_conf /= test->num_ex;
  avg_cred /= test->num_ex;

  std::chrono::time_point<std::chrono::steady_clock> end_time = std::chrono::high_resolution_clock::now();

  std::cout << "Simple Accuracy: " << 100.0*num_correct/test->num_ex << '%'
            << " (" << num_correct << '/' << test->num_ex << ") "
            << "Mean Confidence: " << std::fixed << std::setprecision(4) << 100*avg_conf << "%, "
            << "Mean Credibility: " << 100*avg_cred << "%\n";
  std::cout << "Accuracy: " << 100.0*num_incl/test->num_ex << '%'
            << " (" << num_incl << '/' << test->num_ex << ") "
            << "Multi Prediction: " << std::fixed << std::setprecision(4) << 100.0*num_multi/test->num_ex << "%, "
            << "Empty Prediction: " << 100.0*num_empty/test->num_ex << "%\n";
  output_file.close();

  std::cout << "Time cost: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count()/1000.0 << " s\n";

  FreeProblem(train);
  FreeProblem(test);
  FreeModel(model);
  FreeParam(&param);

  return 0;
}
int mainHInit(int argc, char *argv[])
{
   static int ft=1;
   char *datafn, *s;
   int nSeg;
   zwangModify();
   if(InitShell(argc,argv,hinit_version,hinit_vc_id)<SUCCESS)
      HError(2100,"HInit: InitShell failed");
   if(ft)
   {
	   if(isMemInit==0)
	   {
		   InitMem();  
		   isMemInit=1;
	   }
	   InitLabel();
	   InitMath();  InitSigP();
	   InitWave();  InitAudio();
	   InitVQ();    InitModel();
	   if(InitParm()<SUCCESS)  
		  HError(2100,"HInit: InitParm failed");
	   InitTrain(); InitUtil();
	   ft=0;
	     /* Stacks for global structures requiring memory allocation */
	   CreateHeap(&segmentStack,"SegStore", MSTAK, 1, 0.0, 100000, LONG_MAX);
	   CreateHeap(&sequenceStack,"SeqStore", MSTAK, 1, 0.0, 1000, 1000);
	   CreateHeap(&clustSetStack,"ClustSetStore", MSTAK, 1, 0.0, 1000, 1000);
	   CreateHeap(&transStack,"TransStore", MSTAK, 1, 0.0, 1000, 1000);
	   CreateHeap(&traceBackStack,"TraceBackStore", MSTAK, 1, 0.0, 1000, 1000);
	   CreateHeap(&bufferStack,"BufferStore", MSTAK, 1, 0.0, 1000, 1000);

   }

    zwangHMemGetConf();
	zwangHWaveGetConf();
	zwangHLabelGetConf();
	zwangHMathGetConf();
	zwangHSigPGetConf();
	zwangHAudioGetConf();
	zwangHVQGetConf();
	zwangHModelGetConf();
	zwangHParmGetConf();
	zwangHTrainGetConf();
	zwangHUtilGetConf();

   SetConfParms();

	CreateHMMSet(&hset,&gstack,FALSE);
	
   while (NextArg() == SWITCHARG) {
      s = GetSwtArg();
      if (strlen(s)!=1) 
         HError(2119,"HInit: Bad switch %s; must be single letter",s);
      switch(s[0]){
      case 'e':
         epsilon = GetChkedFlt(0.0,1.0,s); break;
      case 'i':
         maxIter = GetChkedInt(0,100,s); break;
      case 'l':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: Segment label expected");
         segLab = GetStrArg();
         break;
      case 'm':
         minSeg = GetChkedInt(1,1000,s); break;
      case 'n':
         newModel = FALSE; break;
      case 'o':
         outfn = GetStrArg();
         break;
      case 'u':
         SetuFlags(); break;
      case 'v':
         minVar = GetChkedFlt(0.0,10.0,s); break;
      case 'w':
         mixWeightFloor = MINMIX * GetChkedFlt(0.0,10000.0,s); 
         break;
      case 'B':
         saveBinary = TRUE;
         break;
      case 'F':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: Data File format expected");
         if((dff = Str2Format(GetStrArg())) == ALIEN)
            HError(-2189,"HInit: Warning ALIEN Data file format set");
         break;
      case 'G':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: Label File format expected");
         if((lff = Str2Format(GetStrArg())) == ALIEN)
            HError(-2189,"HInit: Warning ALIEN Label file format set");
         break;
      case 'H':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: HMM macro file name expected");
         AddMMF(&hset,GetStrArg());
         break;
      case 'I':
         if (NextArg() != STRINGARG)
            HError(2119,"HInit: MLF file name expected");
         LoadMasterFile(GetStrArg());
         break;
      case 'L':
         if (NextArg()!=STRINGARG)
            HError(2119,"HInit: Label file directory expected");
         labDir = GetStrArg(); break;
      case 'M':
         if (NextArg()!=STRINGARG)
            HError(2119,"HInit: Output macro file directory expected");
         outDir = GetStrArg();
         break;
      case 'T':
         if (NextArg() != INTARG)
            HError(2119,"HInit: Trace value expected");
         trace = GetChkedInt(0,01777,s);
         break;
      case 'X':
         if (NextArg()!=STRINGARG)
            HError(2119,"HInit: Label file extension expected");
         labExt = GetStrArg(); break;
      default:
         HError(2119,"HInit: Unknown switch %s",s);
      }
   }
   if (NextArg()!=STRINGARG)
      HError(2119,"HInit: source HMM file name expected");
   hmmfn = GetStrArg();
   Initialise();
   do {
      if (NextArg()!=STRINGARG)
         HError(2119,"HInit: training data file name expected");
      datafn = GetStrArg();
      LoadFile(datafn);
   } while (NumArgs()>0);
   nSeg = NumSegs(segStore);
   if (nSeg < minSeg)
      HError(2121,"HInit: Too Few Observation Sequences [%d]",nSeg);
   EstimateModel();
   SaveModel(outfn);

   Dispose(hset.hmem,hset.mtab);
   ResetHeap(&gstack);
   ResetHeap(&segmentStack);
   ResetHeap(&sequenceStack);
   ResetHeap(&clustSetStack);
   ResetHeap(&transStack);
   ResetHeap(&traceBackStack);
   ResetHeap(&bufferStack);

   zwangInitParmClear();
   zwangInitShellClear();
   return (0);          /* never reached -- make compiler happy */
}
예제 #6
0
void Pipe::SaveModelByName(const string &model_name) {
  FILE *fs = fopen(model_name.c_str(), "wb");
  CHECK(fs) << "Could not open model file for writing: " << model_name; 
  SaveModel(fs);
  fclose(fs);
}
/* main func */
int mainHCompV(int argc, char *argv[])
{
   static int ft=1;
   char *datafn, *s;
   void SetCovs(void);
   void PutVFloor(void);
   SpkrAcc *sa = NULL;
	zwangModify();
   if(InitShell(argc,argv,hcompv_version,hcompv_vc_id)<SUCCESS)
      HError(2000,"HCompV: InitShell failed");
   if(ft)
   {
	   if(isMemInit==0)
	   {
		   InitMem();  
		   isMemInit=1;
	   }
	   InitLabel();
	   InitMath();  InitSigP();
	   InitWave();  InitAudio();
	   InitVQ();    InitModel();
	   if(InitParm()<SUCCESS)  
		  HError(2000,"HCompV: InitParm failed");   
	   ft=0;
	   CreateHeap(&iStack,"InBuf", MSTAK, 1, 0.5, 100000, LONG_MAX);
   }
   zwangHMemGetConf();
	zwangHWaveGetConf();
	zwangHLabelGetConf();
	zwangHMathGetConf();
	zwangHSigPGetConf();
	zwangHAudioGetConf();
	zwangHVQGetConf();
	zwangHModelGetConf();
	zwangHParmGetConf();

   SetConfParms();
CreateHMMSet(&hset,&gstack,FALSE);
   pathPattern[0]='\0';
   while (NextArg() == SWITCHARG) {
      s = GetSwtArg();
      if (strlen(s)!=1) 
         HError(2019,"HCompV: Bad switch %s; must be single letter",s);
      switch(s[0]){
      case 'f':
         if (NextArg() != FLOATARG)
            HError(2019,"HCompV: Variance floor scale expected");
         vFloorScale = GetChkedFlt(0.0,100.0,s);
         break;
      case 'l':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: Segment label expected");
         segLab = GetStrArg();
         break;
      case 'm':
         meanUpdate = TRUE;
         break;
      case 'o':
         outfn = GetStrArg();
         break;     
      case 'v':
         if (NextArg() != FLOATARG)
            HError(2019,"HCompV: Minimum variance level expected");
         minVar = GetChkedFlt(0.0,100.0,s);
         break;
      case 'k':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: speaker pattern expected");
         strcpy(spPattern,GetStrArg());
         if (strchr(spPattern,'%')==NULL)
            HError(2019,"HCompV: Speaker mask invalid");
         break;
      case 'c':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: CMV output dir expected");
         strcpy(cmDir,GetStrArg());
         DoCMV = TRUE;
         break;
      case 'p':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: path pattern expected");
         strcpy(pathPattern,GetStrArg());
         if (strchr(pathPattern,'%')==NULL)
            HError(2019,"HCompV: Path mask invalid");
         break;
      case 'q':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: output flags (nmv)");
         strcpy(oflags,GetStrArg());
         break;
      case 'B':
         saveBinary = TRUE;
         break;
      case 'F':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: Data File format expected");
         if((dff = Str2Format(GetStrArg())) == ALIEN)
            HError(-2089,"HCompV: Warning ALIEN Data file format set");
         break;
      case 'G':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: Label File format expected");
         if((lff = Str2Format(GetStrArg())) == ALIEN)
            HError(-2089,"HCompV: Warning ALIEN Label file format set");
         break;
      case 'H':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: HMM macro file name expected");
         AddMMF(&hset,GetStrArg());
         break;
      case 'I':
         if (NextArg() != STRINGARG)
            HError(2019,"HCompV: MLF file name expected");
         LoadMasterFile(GetStrArg());
         break;
      case 'L':
         if (NextArg()!=STRINGARG)
            HError(2019,"HCompV: Label file directory expected");
         labDir = GetStrArg();
         break;
      case 'M':
         if (NextArg()!=STRINGARG)
            HError(2019,"HCompV: Output macro file directory expected");
         outDir = GetStrArg();
         break;
      case 'T':
         if (NextArg() != INTARG)
            HError(2019,"HCompV: Trace value expected");
         trace = GetChkedInt(0,077,s); 
         break;
      case 'X':
         if (NextArg()!=STRINGARG)
            HError(2019,"HCompV: Label file extension expected");
         labExt = GetStrArg();
         break;
      default:
         HError(2019,"HCompV: Unknown switch %s",s);
      }
   }

   /* if not doing CMV, do standard HCompV */
   if (DoCMV == FALSE){
      if (NextArg()!=STRINGARG)
         HError(2019,"HCompV: Source HMM file name expected");
      hmmfn = GetStrArg();
      Initialise();
      do {
         if (NextArg()!=STRINGARG)
            HError(2019,"HCompV: Training data file name expected");
         datafn = GetStrArg();
         LoadFile(datafn);
      } while (NumArgs()>0);
      SetCovs();
      FixGConsts(hmmLink);
      SaveModel(outfn);   
      if (trace&T_TOP)
         printf("Output written to directory %s\n",(outDir==NULL)?"./":outDir);
      if (vFloorScale>0.0)
         PutVFloor();
   }
   else {
      /* report export data type */
      ReportOutput();
      /* init input buffer mem heap */
      CreateHeap(&iStack,"BufferIn",MSTAK,1,0.5,100000,5000000);
      do {
         if (NextArg()!=STRINGARG){
            HError(2019,"HCompV: Training data file name expected");
         }
         datafn = GetStrArg();       
         /* accumulate stats for current utterance file and update speaker list */
         sa = AccGenUtt(spPattern,datafn,sa);
         salist = UpdateSpkrAccList(salist,sa);
         /* reset for next utterance */
         ClrSpkrAcc(sa);
      } while (NumArgs()>0);
      /* compute the means and variances for each speaker */
      UpdateMeanVar(salist);
      /* export NMV for each speaker */
      ExportNMV(salist,cmDir,TargetPKStr);
   }
   Dispose(hset.hmem,hset.mtab);
   ResetHeap(&gstack);
   ResetHeap(&iStack);
   zwangInitParmClear();
zwangInitShellClear();
   //Exit(0);
   return (0);          /* never reached -- make compiler happy */
}
예제 #8
0
파일: dml.cpp 프로젝트: Nikraaaazy/bosen
void DML::Learn(float learn_rate, int epochs, const char * model_file) {
  // tmp buffers
  float *vec_buf_1 = new float[src_feat_dim];  // src_feat_dim
  float *vec_buf_2 = new float[dst_feat_dim];  // dst_feat_dim
  // assign id to threads
  if (!thread_id.get()) {
    thread_id.reset(new int(thread_counter++));
  }
  // get access to tables
  petuum::PSTableGroup::RegisterThread();
  mat L = petuum::PSTableGroup::GetTableOrDie<float>(0);
  // Run additional iterations to let stale values finish propagating
  for (int iter = 0; iter < staleness; ++iter) {
    petuum::PSTableGroup::Clock();
  }
  // initialize parameters
  if (client_id == 0 && (*thread_id) == 0) {
    std::cout << "init parameters" << std::endl;
    for (int i = 0; i < dst_feat_dim; i++) {
      petuum::DenseUpdateBatch<float> update_batch(0, src_feat_dim);

      for (int j = 0; j < src_feat_dim; j++) {
      float a = rand()%1000/1000.0/2000;
      update_batch[j]=a;
    }
    L.DenseBatchInc(i, update_batch);
  }
    std::cout << "init parameters done" << std::endl;
  }
  process_barrier->wait();
  if (client_id == 0 && (*thread_id) == 0)
    std::cout << "training starts" << std::endl;
  sleep((client_id+(*thread_id))*2);
  std::vector<int> idx_perm_simi_pairs;
  for (int i = 0; i < num_simi_pairs; i++)
    idx_perm_simi_pairs.push_back(i);
  std::random_shuffle(idx_perm_simi_pairs.begin(), \
    idx_perm_simi_pairs.end(), myrandom2);
  int * idx_perm_arr_simi_pairs = new int[num_simi_pairs];
  for (int i = 0; i < num_simi_pairs; i++)
    idx_perm_arr_simi_pairs[i] = idx_perm_simi_pairs[i];
  std::vector<int> idx_perm_diff_pairs;
  for (int i = 0; i < num_diff_pairs; i++)
    idx_perm_diff_pairs.push_back(i);
  std::random_shuffle(idx_perm_diff_pairs.begin(), \
       idx_perm_diff_pairs.end(), myrandom2);
  int * idx_perm_arr_diff_pairs = new int[num_diff_pairs];
  for (int i = 0; i < num_diff_pairs; i++)
    idx_perm_arr_diff_pairs[i] = idx_perm_diff_pairs[i];
  std::vector<int> idx_perm;
  for (int i = 0; i < dst_feat_dim; i++)
    idx_perm.push_back(i);
  std::random_shuffle(idx_perm.begin(), idx_perm.end(), myrandom2);
  int * idx_perm_arr = new int[dst_feat_dim];
  for (int i = 0; i < dst_feat_dim; i++)
    idx_perm_arr[i] = idx_perm[i];
  
  //local buffer of parameter
  float ** local_paras = new float *[dst_feat_dim];
  for (int i = 0; i < dst_feat_dim; i++)
    local_paras[i] = new float[src_feat_dim];

  float ** grad=new float *[dst_feat_dim];
  for ( int i=0;i<dst_feat_dim;i++)
    grad[i]=new float[src_feat_dim];

  int inner_iters=(num_simi_pairs+num_diff_pairs)/size_mb/num_clients/num_worker_threads;
  int * mb_idx=new int[size_mb/2];

  for (int e = 0; e < epochs; e++) {
    for(int it=0;it<inner_iters;it++){
      //copy parameters
      petuum::RowAccessor row_acc;
      for (int i = 0; i < dst_feat_dim; i++) {

        const petuum::DenseRow<float>& r = L.Get<petuum::DenseRow<float> >(i, &row_acc);

        for (int j = 0; j < src_feat_dim; j++) {
          local_paras[i][j] = r[j];
        }
      }
      //evaluate
      if (client_id == 0 && (*thread_id) == 0 && it%num_iters_evaluate==0) {
        // evaluate
        float simi_loss = 0, diff_loss = 0, total_loss = 0;
        Evaluate(local_paras, simi_loss, diff_loss, total_loss, vec_buf_1, vec_buf_2);
        //std::cout << "epoch:\t" << e << "\tsimi_loss:\t" << simi_loss \
        //<< "\tdiff_loss:\t" << diff_loss << "\ttotal_loss:\t" \
        //<< total_loss << std::endl;
        std::cout << "epoch: " << e << " iter: " << it << " loss: " << total_loss <<std::endl;
      }
      //set gradient to zero
      for(int i=0;i<dst_feat_dim;i++)
        memset(grad[i], 0, src_feat_dim*sizeof(float));

      rand_init_vec_int(mb_idx, size_mb/2,num_simi_pairs);
      for(int i=0;i<size_mb/2;i++){
        int idx = idx_perm_arr_simi_pairs[mb_idx[i]];
        Update(local_paras, grad, data[simi_pairs[idx].x], data[simi_pairs[idx].y], \
           1,  vec_buf_1, vec_buf_2);
      }
      rand_init_vec_int(mb_idx, size_mb/2,num_diff_pairs);

      for(int i=0;i<size_mb/2;i++){
        int idx = idx_perm_arr_diff_pairs[mb_idx[i]];
        Update(local_paras, grad, data[diff_pairs[idx].x], data[diff_pairs[idx].y], \
           0,  vec_buf_1, vec_buf_2);
      }
      //update parameters
      float coeff =- learn_rate*2/size_mb;
      for (int i = 0; i < dst_feat_dim; i++) {
        petuum::DenseUpdateBatch<float> update_batch(0,src_feat_dim);
        for (int j = 0; j < src_feat_dim; j++) 
          update_batch[j]= coeff*grad[i][j];
        L.DenseBatchInc(i, update_batch);
      }
      petuum::PSTableGroup::Clock();
    }
  }
  if (client_id == 0 && (*thread_id) == 0)
    SaveModel(L, model_file);

  delete[] mb_idx;
  delete[] vec_buf_1;
  delete[] vec_buf_2;
  for(int i=0;i< dst_feat_dim;i++)
    delete[]local_paras[i];
  delete[] local_paras;
  for(int i=0;i<dst_feat_dim;i++)
    delete[]grad[i];
  delete[]grad;
  petuum::PSTableGroup::DeregisterThread();
}
예제 #9
0
// This function performs the action after the delay is up.  It returns
// true if the program is still to quit
bool ImodClipboard::executeMessage()
{
  int returnValue, arg;
  int succeeded = 1;
  static int checkSum = 0;
  int newCheck;
  QString convName;
  QDir *curdir;
  ZapFuncs *zap;
  int movieVal, xaxis, yaxis, zaxis, taxis;
  int objNum, type, symbol, symSize, ptSize, mode, mask, interval;
  bool props1;
  Imod *imod;
  Iobj *obj;
  int symTable[] = 
    { IOBJ_SYM_NONE, IOBJ_SYM_CIRCLE, IOBJ_SYM_SQUARE, IOBJ_SYM_TRIANGLE };

  // Number of arguments required - for backward compatibility, going to
  // model mode does not require one but should have one
  int requiredArgs[] = {0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 5, 5, 0, 2, 4, 4, 3, 1, 1};
  int numArgs = sMessageStrings.count();

  // Loop on the actions in the list; set arg to numArgs to break loop
  for (arg = 0; arg < numArgs; arg++) {
    imod = App->cvi->imod;

    // Get the action, then check that there are enough values for it
    sMessageAction = sMessageStrings[arg].toInt();
    if (sMessageAction >= 0 && sMessageAction < sizeof(requiredArgs) / sizeof(int) &&
        arg + requiredArgs[sMessageAction] >= numArgs) {
        imodPrintStderr("imodExecuteMessage: not enough values sent"
                " with action command %d\n", sMessageAction);
        succeeded = 0;
        break;
    }

    // If still in initial load, skip the arguments (and one more if model mode message)
    if (sInitialLoad) {
      if (sMessageAction >= 0 && sMessageAction < sizeof(requiredArgs) / sizeof(int))
        arg += requiredArgs[sMessageAction];
      if (arg < numArgs - 1 && sMessageAction == MESSAGE_MODEL_MODE)
        arg++;

      // Stop processing after a plugin message because the length is unknown
      // and stop after seeing a quit
      if (sMessageAction == MESSAGE_PLUGIN_EXECUTE || sMessageAction == MESSAGE_QUIT) 
        arg = numArgs;
      continue;
    }

    if (Imod_debug) {
      QTime curTime = QTime::currentTime();
      wprint("%02d:%02d.%03d: Executing message action %d\n", 
             curTime.minute(), curTime.second(), curTime.msec(), sMessageAction);
      imodPrintStderr("%02d:%02d.%03d: imodHCM in executeMessage: executing message "
                      "action %d\n", 
                      curTime.minute(), curTime.second(), curTime.msec(), sMessageAction);
      if (imodDebug('C')) {
        newCheck = imodChecksum(App->cvi->imod);
        wprint("Checksum before = %d\n", newCheck);
        if (checkSum && newCheck != checkSum)
          wprint("\aIT CHANGED SINCE LAST TIME\n");
        checkSum = newCheck;
      }
    }

    if (ImodvClosed || !Imodv->standalone) {
      switch (sMessageAction) {
        
      case MESSAGE_OPEN_MODEL:
      case MESSAGE_OPEN_KEEP_BW:
        curdir = new QDir();
        convName = curdir->cleanPath(sMessageStrings[++arg]);
        delete curdir;
    
        // Since this could open a dialog with an indefinite delay, just send
        // the OK signal now
        succeeded = -1;
        sendResponse(1);
        inputRaiseWindows();

        // DNM 6/3/04: switch to keeping BW values in the first place
        returnValue = openModel(LATIN1(convName), 
                                sMessageAction == MESSAGE_OPEN_KEEP_BW, false);
        if(returnValue == IMOD_IO_SUCCESS) {
          wprint("%s loaded.\n", 
                 LATIN1(QDir::convertSeparators(QString(Imod_filename))));

        }
        else if(returnValue == IMOD_IO_SAVE_ERROR) {
          wprint("Error Saving Model. New model not loaded.\n");
          arg = numArgs;
        }
        else if(returnValue == IMOD_IO_SAVE_CANCEL) {
          wprint("Operation cancelled. New model not loaded.\n");
          arg = numArgs;
        }

        // The model does not exist yet.  Try creating a new model.
        else if(returnValue == IMOD_IO_DOES_NOT_EXIST) {
          returnValue = createNewModel(LATIN1(convName));
          if(returnValue == IMOD_IO_SUCCESS) {
        
            wprint("New model %s created.\n", 
                   LATIN1(QDir::convertSeparators(QString(Imod_filename))));
          }
          else {
            wprint("Could not create a new model %s.\n", 
                   LATIN1(sMessageStrings[arg]));
            arg = numArgs;
          }
        }
        else if(returnValue == IMOD_IO_NO_ACCESS_ERROR) {
          wprint("Error opening model. Check file permissions\n.");
          arg = numArgs;
        }
        else {
          wprint("Unknown return code, new model not loaded!!\n");
          arg = numArgs;
        }
        break;

      case MESSAGE_SAVE_MODEL:
        succeeded = -1;
        sendResponse(1);
        imod->blacklevel = App->cvi->black;
        imod->whitelevel = App->cvi->white;
        returnValue = SaveModel(imod);
        break;

      case MESSAGE_VIEW_MODEL:
        imod_autosave(imod);
        inputRaiseWindows();
        imodv_open();
        break;

      case MESSAGE_QUIT:
        arg = numArgs;
        break;

      case MESSAGE_RAISE_WINDOWS:
        inputRaiseWindows();
        break;

      case MESSAGE_MODEL_MODE:
        movieVal = 1;
        if (arg < numArgs - 1)
          movieVal = sMessageStrings[++arg].toInt();
        if (movieVal > 0)
          imod_set_mmode(IMOD_MMODEL);
        else {
          imod_set_mmode(IMOD_MMOVIE);
          if (movieVal < 0) {
            xaxis = (-movieVal) & 1 ? 1 : 0;
            yaxis = (-movieVal) & 2 ? 1 : 0;
            zaxis = (-movieVal) & 4 ? 1 : 0;
            taxis = (-movieVal) & 8 ? 1 : 0;
            imodMovieXYZT(App->cvi, xaxis, yaxis, zaxis, taxis);
          }
        }
        break;

      case MESSAGE_OPEN_BEADFIXER:
        imodPlugOpenByName("Bead Fixer");
        break;

      case MESSAGE_ONE_ZAP_OPEN:
        inputRaiseWindows();
        if (!imodDialogManager.windowCount(ZAP_WINDOW_TYPE) &&
            imodLoopStarted())
          imod_zap_open(App->cvi, 0);
        break;

      case MESSAGE_RUBBERBAND:
        zapReportRubberband();
        break;

      case MESSAGE_SLICER_ANGLES:
        slicerReportAngles();
        break;

      case MESSAGE_OBJ_PROPERTIES:
      case MESSAGE_NEWOBJ_PROPERTIES:
      case MESSAGE_OBJ_PROPS_2:
      case MESSAGE_NEWOBJ_PROPS_2:
        props1 = sMessageAction == MESSAGE_OBJ_PROPERTIES ||
          sMessageAction == MESSAGE_NEWOBJ_PROPERTIES;
        objNum = sMessageStrings[++arg].toInt();
        type = sMessageStrings[++arg].toInt();
        symbol = sMessageStrings[++arg].toInt();
        symSize = sMessageStrings[++arg].toInt();
        if (props1)
          ptSize = sMessageStrings[++arg].toInt();

        // Object is numbered from 1, so decrement and test for substituting
        // current object
        if (--objNum < 0)
          objNum = imod->cindex.object;
        if (objNum < 0 || objNum >= imod->objsize) {
          imodPrintStderr("imodExecuteMessage: illegal object # sent with "
                          "object property command\n");
          succeeded = 0;
          arg = numArgs;
          break;
        }
        obj = &imod->obj[objNum];

        // If object has contours, skip for NEWOBJ message
        if (obj->contsize && (sMessageAction == MESSAGE_NEWOBJ_PROPERTIES ||
                              sMessageAction == MESSAGE_NEWOBJ_PROPS_2))
          break;

        if (props1) {

          // Process the changes if not -1: object type
          if (type >= 0 && type < 3) {
            switch (type) {
            case 0:
              obj->flags &= ~(IMOD_OBJFLAG_OPEN | IMOD_OBJFLAG_SCAT);
              break;
            case 1:
              obj->flags |= IMOD_OBJFLAG_OPEN;
              obj->flags &= ~IMOD_OBJFLAG_SCAT;
              break;
            case 2:
              obj->flags |= IMOD_OBJFLAG_SCAT | IMOD_OBJFLAG_OPEN;
              break;
            }
          }
        
          // Symbol type and filled
          if (symbol >= 0) {
            if ((symbol & 7) < (sizeof(symTable) / sizeof(int)))
              obj->symbol = symTable[symbol & 7];
            utilSetObjFlag(obj, 0, (symbol & 8) != 0, IOBJ_SYMF_FILL);
          }
          
          // Symbol size, 3d point size
          if (symSize > 0)
            obj->symsize = symSize;
          if (ptSize >= 0)
            obj->pdrawsize = ptSize;
        } else {

          // Points per contour
          if (type >= 0)
            obj->extra[IOBJ_EX_PNT_LIMIT] = type;

          // Planar contours
          if (symbol >= 0)
            utilSetObjFlag(obj, 0, symbol != 0, IMOD_OBJFLAG_PLANAR);

          // Sphere on sec only
          if (symSize >= 0)
            utilSetObjFlag(obj, 0, symSize != 0, IMOD_OBJFLAG_PNT_ON_SEC);
        }        

        // The general draw updates object edit window, but need to call 
        // imodv object edit for it to update
        imodDraw(App->cvi, IMOD_DRAW_MOD);
        imodvObjedNewView();

        // If no contours and only 1 obj, set checksum to avoid save requests
        if (imod->objsize == 1 && !obj->contsize) {
          imod->csum = imodChecksum(imod);
          if (imodDebug('C'))
            wprint("handleMessage set checksum %d\n", imod->csum);
        }
        break;

      case MESSAGE_GHOST_MODE:
        mode = sMessageStrings[++arg].toInt();
        mask = sMessageStrings[++arg].toInt();
        App->cvi->ghostmode = (App->cvi->ghostmode & ~mask) | (mode & mask);
        interval = sMessageStrings[++arg].toInt();
        if (interval >= 0)
          App->cvi->ghostdist = interval;
        imodDraw(App->cvi, IMOD_DRAW_MOD);
        break;
        
      case MESSAGE_ZAP_HQ_MODE:
        mode = sMessageStrings[++arg].toInt() != 0 ? 1 : 0;
        zap = getTopZapWindow(false);
        if (zap) {
          zap->stateToggled(ZAP_TOGGLE_RESOL, mode);
          zap->mQtWindow->setToggleState(ZAP_TOGGLE_RESOL, mode);
        } else
          zapSetNextOpenHQstate(mode);
        break;

      case MESSAGE_OPEN_DIALOGS:
        if (!ImodvClosed)
          imodvOpenSelectedWindows(LATIN1(sMessageStrings[arg + 1]));
        ImodInfoWin->openSelectedWindows(LATIN1(sMessageStrings[++arg]),
                                         ImodvClosed ? 0 : 1);
        break;

      case MESSAGE_PLUGIN_EXECUTE:
        arg++;
        if (imodPlugMessage(App->cvi, &sMessageStrings, &arg)) {
          succeeded = 0;
          arg = numArgs;
          break;
        }
        break;

      default:
        imodPrintStderr("imodExecuteMessage: action %d not recognized\n"
                        , sMessageAction);
        succeeded = 0;
        arg = numArgs;
      }
    } else {
      
      // Messages for 3dmodv
      switch (sMessageAction) {
      case MESSAGE_QUIT:
        arg = numArgs;
        break;
        
      case MESSAGE_RAISE_WINDOWS:
        imodvInputRaise();
        break;
        
      case MESSAGE_OPEN_DIALOGS:
        imodvOpenSelectedWindows(LATIN1(sMessageStrings[++arg]));
        break;

      default:
        imodPrintStderr("imodExecuteMessage: action %d not recognized by"
                        " 3dmodv\n" , sMessageAction);
        succeeded = 0;
        arg = numArgs;
      }
    }
  }

  if (imodDebug('C')) {
    wprint("Checksum after = %d\n", newCheck);
    if (newCheck != checkSum)
      wprint("\aIT CHANGED IN THAT OPERATION\n");
    checkSum = newCheck;
  }

  // Now set the clipboard with the response as long as we aren't doing deferred
  if (succeeded >= 0 && mDeferredHandling <= 0)
    sendResponse(succeeded);

  // Only do deferred handling once
  if (mDeferredHandling > 0)
    mDeferredHandling = 0;
  return sMessageAction == MESSAGE_QUIT;
}
예제 #10
0
BOOL Start_File_Learn(char *filename)
{
	//OPENFILENAME ofn;
	//char filename[FILE_LEN];
	char buf[3];

	//filename[0] = '\0';
	//memset(&ofn, 0, sizeof(OPENFILENAME));
	//ofn.lStructSize = sizeof(OPENFILENAME);
	//ofn.lpstrFilter = "kifu file(*.new)\0*.new\0all file(*.*)\0*.*\0\0";
	//ofn.lpstrFile = filename;
	//ofn.nMaxFile = sizeof(filename);
	//ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	//ofn.lpstrDefExt = "new";

	//if(!GetOpenFileName(&ofn))
	//{
	//	return FALSE;
	//}

	int error;
	FILE *fp;
	char dir_path[256], file_path[256];
	strcpy_s(dir_path, "kifu\\");
	sprintf_s(file_path, "%s%s", dir_path, filename);

	if((error = fopen_s(&fp, file_path, "r")) != 0){
		return FALSE;
	}

	int i = 0, color = BLACK;
	BitBoard bk, wh, rev;
	BitBoard first_bk = 34628173824, first_wh = 68853694464;
	BitBoard temp_b[60], temp_w[60];
	int teban[60];
	int turn;
	int move, stone_diff, cnt = 0, bad_cnt = 0;
	bool bad_data = false, end_flag = false;
	//HDC hdc;

	//MessageBox(hWnd, "エラー", "エラー", MB_OK);
	/* ファイルの終端? */
	while(1)
	{
		bk = first_bk;
		wh = first_wh;
		color = BLACK;
		turn = 0;
		/* スペース? */
   		while(1)
		{
			/* 英字を読み込む */
			buf[0] = (char )fgetc(fp);
			/* オシマイ */
			if(buf[0] == EOF)
			{
				end_flag = true;
				break;
			}
			/* スペースなら定石との切れ目か石差 */
			if(buf[0] == ' ')
			{
				/* スペースの次が数字だった場合は抜ける */
				while((buf[0] = (char )fgetc(fp)) == ' ');
				if(isdigit(buf[0]) || buf[0] == '-')
				{
					break;
				}
				/* 続きがある場合はそのまま続行 */
			}
			else if(!isalpha(buf[0]))
			{
				bad_data = true;
				break;
			}
			/* 数字を読み込む */
			buf[1] = (char )fgetc(fp);

			/****** ここからエラー処理 ******/
			/* なぜかおかしな棋譜データが結構ある */
			if(!isdigit(buf[1]))
			{
				bad_data = true;
				break;
			}
			move = ConvertToMove_U(buf);
			/* a9 とか明らかに間違った手を含んでいる棋譜がある */
			if(move == -1)
			{
				bad_data = true;
				break;
			}
			/* なぜかすでに置いたマスに置いている棋譜がある */
			if(bk & (one << move) || wh & (one << move))
			{
				bad_data = true;
				break;
			}
			/****** ここまで ******/

			if(color == BLACK)
			{
				rev = GetRev[move](bk, wh);
				/* 黒パス? */
				if(rev == 0)
				{
					rev = GetRev[move](wh, bk);
					/* 一応合法手になっているかのチェック */
					if(rev == 0)
					{
						bad_data = true;
						break;
					}
					bk ^= rev;
					wh ^= ((one << move) | rev);
				}
				else
				{
					bk ^= ((one << move) | rev);
					wh ^= rev;
					color ^= 1;
				}
			}
			else
			{
				rev = GetRev[move](wh, bk);
				/* 白パス? */
				if(rev == 0)
				{
					rev = GetRev[move](bk, wh);
					/* 一応合法手になっているかのチェック */
					if(rev == 0)
					{
						bad_data = true;
						break;
					}
					bk ^= ((one << move) | rev);
					wh ^= rev;
				}
				else
				{
					bk ^= rev;
					wh ^= ((one << move) | rev);
					color ^= 1;
				}
			}

			turn++;
			/* 対局データ生成 */
			temp_b[(turn - 1)] = bk;
			temp_w[(turn - 1)] = wh;
			teban[(turn - 1)] = color;
		}

		if(end_flag == true)
		{
			break;
		}
		if(!bad_data)
		{
			/* 石差 */
			buf[1] = (char )fgetc(fp);
			buf[2] = (char )fgetc(fp);
			sscanf_s(buf, "%d", &stone_diff);

			for(i = 0; i < 56; i++)
			{
				init_index_board(temp_b[i], temp_w[i]);
				SaveModel(data_fp[i / 4], stone_diff, temp_b[i], temp_w[i], teban[i]);
			}

		}
		else
		{
			bad_data = false;
			bad_cnt++;
		}
		/* 次の棋譜までスキップ */
		while(fgetc(fp) != 0x0A);
		/*Sleep(500);
		black = bk;
		white = wh;
		hdc = GetDC(hWnd);
		DrawBoard(hWnd, hdc, hStatus, move / 8, move % 8);
		ReleaseDC(hWnd, hdc);*/
		cnt++;
	}
	char str[512];
	sprintf_s(str, "[%s]\ngame:%d, bad:%d", filename, cnt, bad_cnt);
	PostMessage(hStatus, SB_SETTEXT, (WPARAM)0 | 3, (LPARAM)str);
	fclose(fp);

	return TRUE;
}