prompting.validators.reward.blacklist
#
Module Contents#
Classes#
- class prompting.validators.reward.blacklist.BlacklistRewardEvent#
- class prompting.validators.reward.blacklist.Blacklist(boundary=6, n_min=5, n_max=14, word_limit=2000, A=1.3, preprocess='[^(\\w|\\s)]', partial_ratio_boundary=95, half_life=20000, support=0.01, error=0.001, memory_lim=1000000, frequency_multiplier=100)#
Bases:
prompting.validators.reward.reward.BaseRewardModel
- Parameters:
- add(texts)#
Extract and add n-grams from a list of texts to counter
- Parameters:
texts (list) – batch of completion texts
- extract_ngrams(text)#
Extract n-grams from text string
- _add_ngrams(ngrams)#
Adds n-grams to counter, removing old n-grams periodically. Counting and pruning method based on Lossy counter. Reference: https://files.ifi.uzh.ch/dbtg/sdbs13/T01.3.pdf
- Parameters:
ngrams (List[tuple]) – List of n-gram tuples
- prune()#
Prune the counter when the count is smaller then bucket index.
- reset()#
Reset counters to initial values.
- calculate_significance()#
Calculate significance of all n-grams in counter. By construction, n-grams with count 1 will have significance 0.
- Returns:
Dictionary of n-gram tuples and their significance scores
- Return type:
- get_significance()#
Get significance scores, only recalculating if the counter has been updated.
- Returns:
Dictionary of n-gram tuples and their significance scores
- Return type:
- most_common(n=10)#
Get most common n-grams in queue
- most_significant(n=10, force_update=True)#
Get most significant n-grams in queue based on significance scores
- set_counter_to_half()#
Set all the counters to half for a rolling window effect.
- reward(prompt, completion, name)#
Reward function for blacklist reward model. Returns 1 if completion contains an n-gram with significance above the boundary, 0 otherwise.
- get_rewards(prompt, completions, name)#
- Parameters:
- Return type:
List[BlacklistRewardEvent]
- normalize_rewards(rewards)#
This method normalizes the given rewards by updating the moving mean and variance statistics. The rewards are first standardized, and then scaled to the 0-1 range using a cumulative distribution function (CDF) to ensure they’re in a comparable range across different environments.
Args: rewards (torch.FloatTensor): The reward values to be normalized.
Returns: torch.FloatTensor: The normalized reward values.
Note: - This function uses Welford’s online algorithm to update the mean and variance. - It standardizes the reward values using the updated mean and variance. - It then scales the standardized values to the 0-1 range using the error function (erf) as a CDF.
- Parameters:
rewards (torch.FloatTensor) –
- Return type:
torch.FloatTensor