fibber.paraphrase_strategies.rewrite_rollback_strategy module¶
-
class
fibber.paraphrase_strategies.rewrite_rollback_strategy.RewriteRollbackStrategy(arg_dict, dataset_name, strategy_gpu_id, output_dir, metric_bundle, field)[source]¶ Bases:
fibber.paraphrase_strategies.strategy_base.StrategyBaseInitialize the paraphrase_strategies.
This function initialize the
self._strategy_config,self._metric_bundle,self._device,self._output_dir,self._dataset_name.You should not overwrite this function.
self._strategy_config (dict): a dictionary that stores the strategy name and all hyperparameter values. The dict is also saved to the results.
self._metric_bundle (MetricBundle): the metrics that will be used to evaluate paraphrases. Strategies can compute metrics during paraphrasing.
self._device (torch.Device): any computation that requires a GPU accelerator should use this device.
self._output_dir (str): the dir name where the strategy can save files.
self._dataset_name (str): the dataset name.
- Parameters
arg_dict (dict) – all args load from command line.
dataset_name (str) – the name of the dataset.
strategy_gpu_id (int) – the gpu id to run the strategy.
output_dir (str) – a directory to save any models or temporary files.
metric_bundle (MetricBundle) – a MetricBundle object.
-
fit(trainset)[source]¶ Fit the paraphrase strategy on a training set.
- Parameters
trainset (dict) – a fibber dataset.
-
paraphrase_example(data_record, n)[source]¶ Paraphrase one data record.
This function should be overwritten by subclasses. When overwriting this class, you can use
self._strategy_config,self._metric_bundle,self._device,self._output_dir, andself._dataset_name- Parameters
data_record (dict) – a dict storing one data of a dataset.
n (int) – number of paraphrases.
- Returns
A list contain at most n strings.
- Return type
([str,])
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.all_accept_criteria(candidate_paraphrases, **kwargs)[source]¶ Always accept proposed words.
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.assign_candidates(paraphrases_with_mask, candidate_words, tokenizer, masked_index)[source]¶
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.clf_criteria_score(origin_list, paraphrases, data_record_list, field, clf_metric, clf_weight)[source]¶
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.compute_wpe_embedding(sents, word_embs, tokenizer, device)[source]¶
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.joint_weighted_criteria(origin_list, prev_paraphrases, candidate_paraphrases, data_record_list, field, sim_metric, sim_threshold, sim_weight, clf_metric, clf_weight, ppl_metric, ppl_weight, stats, state, log_prob_trans_forward, log_prob_trans_backward, edit_metric, masked_part_text, filled_in_text, **kwargs)[source]¶
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.ppl_criteria_score(origin_list, paraphrases, ppl_metric, ppl_weight)[source]¶ Estimate the score of a sentence using USE.
- Parameters
origin (str) – original sentence.
paraphrases ([str]) – a list of paraphrase_list.
ppl_metric (GPT2PerplexityMetric) – a GPT2PerplexityMetric metric object.
ppl_weight (float) – the weight parameter for the criteria.
- Returns
a numpy array of size
(batch_size,). All entries<=0.- Return type
(np.array)
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.sample_word_from_logits(logits, temperature=1.0, top_k=0)[source]¶ Sample a word from a distribution.
- Parameters
logits (torch.Tensor) – tensor of logits with size
(batch_size, vocab_size).temperature (float) – the temperature of softmax. The PMF is
softmax(logits/temperature).top_k (int) – if
k>0, only sample from the top k most probable words.
-
fibber.paraphrase_strategies.rewrite_rollback_strategy.sim_criteria_score(origin_list, paraphrases, sim_metric, sim_threshold, sim_weight)[source]¶ Estimate the score of a sentence using USE.
- Parameters
origin (str) – original sentence.
paraphrases ([str]) – a list of paraphrase_list.
sim_metric (MetricBase) – a similarity metric object.
sim_threshold (float) – the universal sentence encoder similarity threshold.
sim_weight (float) – the weight parameter for the criteria.
- Returns
a numpy array of size
(batch_size,). All entries<=0.- Return type
(np.array)