Large web-scale text datasets often contain a heavy long tail of repeated phrases (boilerplate, common idioms, frequent captions). To improve diversity during training, samples can be down-weighted based on how common their n-grams are.
Implement ngram_keep_probabilities(samples, n, threshold) that computes a per-sample keep probability based on n-gram frequencies in the corpus.
n).
where is the multiset of n-grams in sample , is the global frequency of n-gram , and is the threshold. Intuitively, samples that contain at least one rare n-gram receive a high keep probability, while samples consisting entirely of very common n-grams are down-weighted.
5. If a sample has no n-grams (e.g. it has fewer than tokens), set its keep probability to 1.0.
6. Round each probability to 4 decimals and return the list of probabilities in the original sample order.
samples: list of strings.n: positive integer, the n-gram size.threshold: positive float .