示例#1
0
int MHSampler::doc_proposal(SLDADoc& doc, Sentence& sent) {
    int old_topic = sent.topic;
    int new_topic = -1;

    double dart = rand() * (doc.size() + _model->alpha_sum());
    if (dart < doc.size()) {
        int token_index = static_cast<int>(dart);
        new_topic = doc.sent(token_index).topic;
    } else {
        // 命中文档先验部分, 则随机进行主题采样
        new_topic = rand_k(_model->num_topics());
    }

    if (new_topic != old_topic) {
        float proportion_old = proportional_funtion(doc, sent, old_topic);
        float proportion_new = proportional_funtion(doc, sent, new_topic);
        float proposal_old = doc_proposal_distribution(doc, old_topic);
        float proposal_new = doc_proposal_distribution(doc, new_topic);
        double transition_prob = (proportion_new * proposal_old) / (proportion_old * proposal_new);
        double rejection = rand();
        int mask = -(rejection < transition_prob);
        return (new_topic & mask) | (old_topic & ~mask);
    }

    return new_topic;
}
示例#2
0
void test_init(uint32_t* seed)
{
  // Randomize variable locations
  uint32_t rs[NUM_VARS];
  for (int i = 0; i < NUM_VARS; i++) {
    retry:
      rs[i] = rand_k(seed, NUM_LOCS - 1);
      for (int j = 0; j < i; j++)
        if (rs[i] == rs[j]) goto retry;
  }
  // Intialise variables
  test.locs = (var_t*) LOCS_BASE;
  for (int i = 0; i < NUM_VARS; i++) {
    test.vars[i] = &test.locs[rs[i]*LOC_GRAIN];
    *test.vars[i] = 0;
  }
  // Set random start delays
  uint32_t max = 0;
  for (int i = 0; i < NUM_PROCESSES; i++)
    if (test.start_times[i] > max) max = test.start_times[i];
  for (int i = 0; i < NUM_PROCESSES; i++)
    test.delays[i] = rand_k(seed, test.start_times[i] < max ? 6 : 2);
}