MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / Sample

Method Sample

external/sentencepiece/src/unigram_model.cc:511–542  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

509}
510
511std::vector<Lattice::Node *> Lattice::Sample(float inv_theta) {
512 const int len = size();
513 if (len == 0) return {};
514
515 std::vector<float> alpha(node_allocator_.size(), 0.0);
516
517 alpha = ForwardAlgorithm(inv_theta);
518
519 auto *mt = random::GetRandomGenerator();
520
521 std::vector<Node *> results;
522 std::vector<float> probs;
523
524 float Z = alpha[eos_node()->node_id];
525 Node *node = eos_node();
526 while (true) {
527 probs.clear();
528 for (const Node *lnode : end_nodes_[node->pos]) {
529 probs.push_back(std::exp(static_cast<double>(
530 alpha[lnode->node_id] + inv_theta * lnode->score - Z)));
531 }
532 std::discrete_distribution<int> dist(probs.begin(), probs.end());
533 node = end_nodes_[node->pos][dist(*mt)];
534 if (node == bos_node()) break;
535
536 Z = alpha[node->node_id];
537 results.push_back(node);
538 }
539
540 std::reverse(results.begin(), results.end());
541 return results;
542}
543
544// Model::Model() {}
545// Model::~Model() {}

Callers 3

TESTFunction · 0.80
SampleEncodeMethod · 0.80
SampleEncodeAndScoreMethod · 0.80

Calls 5

GetRandomGeneratorFunction · 0.85
sizeMethod · 0.45
clearMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by 1

TESTFunction · 0.64