Exemple #1
0
bool IExportedClass::check_consistency() const
{
//	get class name vector of all parents
	const std::vector<const char*>* vClassNames = class_names();

//	check if class name vector correct
	if(vClassNames==NULL)
	{
		UG_ERR_LOG("#### Registry ERROR:"
				" Class name vector of parent classes missing for "
				"class '"<<this->name()<<"'.\n");
		return false;
	}

//	loop all base classes
	for(size_t i = 0; i < (*vClassNames).size(); ++i)
	{
	//	get name of base class
		const char* baseName = (*vClassNames)[i];

	//	check the name
		if(baseName == NULL || *baseName == '\0' || baseName[0] == '[')
		{
			if(i>0){
			UG_ERR_LOG("#### Registry ERROR:"
					" base class "<<i<<" of class '"<<this->name()<<
					"' has not been named.\n");
				return false;
			}
			else{
			UG_ERR_LOG("#### Registry ERROR:"
					" Class '"<<this->name()<<"' has not been named.\n");
				return false;
			}
		}
	}

//	everything ok
	return true;
}
// [[Rcpp::export]]
List btm_gibbs(NumericVector token_ids, NumericVector doc_ids, int K, 
         double alpha, double eta, int iter) {
  NumericVector docs = unique(doc_ids);
  int D = docs.size();
  int V = unique(token_ids).size();
  
  std::vector<Biterm> bs;
  for (int d = 0; d < D; d++) {
    NumericVector doc_words = token_ids[doc_ids == docs[d]];
    for (int i = 0; i < doc_words.size(); i++) {
      for (int j = i+1; j < doc_words.size(); j++) {
        bs.push_back(Biterm(doc_words[i], doc_words[j], K));
      }
    }
  }
  
  Btm btm = Btm(V, K, alpha, eta, bs);
  
  int B = bs.size();
  arma::mat theta_trace(iter, K);
  theta_trace.zeros();
  arma::cube beta_trace(V, K, iter);
  
  for (int j = 0; j < iter; j++) {
    NumericVector u = runif(B);
    if ((j+1) % 100 == 0)
      Rcout << "Iteration: " << j << std::endl;
    
    // sample latent topic assignments
    for (int i = 0; i < B; i++) {
      // Rcout << "Biterm " << i << std::endl;
      NumericVector Q = btm.sample_prob(bs[i]);
      // Rcout << "----" << std::endl;
      for (int k = 0; k < K; k++) {
        if (u[i] < (Q[k] / Q[K-1])) {
          // Rcout << "assigned topic: " << k << std::endl;
          btm.update_counts(bs[i], k);
          break;
        }
      }
    }
    
    theta_trace.row(j) = btm.calc_theta();
    beta_trace.slice(j) = btm.calc_beta();
  }
  
  NumericVector zs(B);
  for (int b = 0; b < B; b++) 
    zs[b] = bs[b].get_z();
  
  List result;
  result["beta_trace"] = beta_trace;
  result["theta_trace"] = theta_trace;
  result["word_topic_count"] = btm.get_word_topic_count();
  result["topic_count_wd"] = btm.get_topic_count_wd();
  result["topic_count_bt"] = btm.get_topic_count_bt();
  result["z"] = zs;
  CharacterVector class_names(2); 
  class_names[0] = "btm"; 
  class_names[1] = "lda";
  result.attr("class") = class_names;
  
  return result;
}