lm_polygraph.estimators package
Submodules
lm_polygraph.estimators.claim_conditioned_probability module
lm_polygraph.estimators.common module
lm_polygraph.estimators.conditional_pointwise_mutual_information module
- class lm_polygraph.estimators.conditional_pointwise_mutual_information.ConditionalPointwiseMutualInformation(tau: float = 0.0656, lambd: float = 3.599)[source]
Bases:
EstimatorEstimates the token-level uncertainty of a language model following the method of Conditional Pointwise Mutual Information (CPMI) as provided in the paper https://arxiv.org/abs/2210.13210. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
- class lm_polygraph.estimators.conditional_pointwise_mutual_information.MeanConditionalPointwiseMutualInformation(tau: float = 0.0656, lambd: float = 3.599)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of Conditional Pointwise Mutual Information (CPMI) as provided in the paper https://arxiv.org/abs/2210.13210. The sequence-level estimation is calculated as average token-level CPMI estimations. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
lm_polygraph.estimators.deg_mat module
- class lm_polygraph.estimators.deg_mat.DegMat(similarity_score: Literal['NLI_score', 'Jaccard_score'] = 'NLI_score', affinity: Literal['entail', 'contra'] = 'entail', verbose: bool = False)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “The Degree Matrix” as provided in the paper https://arxiv.org/abs/2305.19187. Works with both whitebox and blackbox models (initialized using lm_polygraph.utils.model.BlackboxModel/WhiteboxModel).
Elements on diagonal of matrix D are sums of similarities between the particular number (position in matrix) and other answers. Thus, it is an average pairwise distance (lower values indicated smaller distance between answers which means greater uncertainty).
lm_polygraph.estimators.eccentricity module
- class lm_polygraph.estimators.eccentricity.Eccentricity(similarity_score: Literal['NLI_score', 'Jaccard_score'] = 'NLI_score', affinity: Literal['entail', 'contra'] = 'entail', verbose: bool = False, thres: float = 0.9)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Eccentricity” as provided in the paper https://arxiv.org/abs/2305.19187. Works with both whitebox and blackbox models (initialized using lm_polygraph.utils.model.BlackboxModel/WhiteboxModel).
Method calculates a frobenious (euclidian) norm between all eigenvectors that are informative embeddings of graph Laplacian (lower norm -> closer embeddings -> higher eigenvectors -> greater uncertainty).
lm_polygraph.estimators.eig_val_laplacian module
- class lm_polygraph.estimators.eig_val_laplacian.EigValLaplacian(similarity_score: Literal['NLI_score', 'Jaccard_score'] = 'NLI_score', affinity: Literal['entail', 'contra'] = 'entail', verbose: bool = False)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Sum of Eigenvalues of the Graph Laplacian” as provided in the paper https://arxiv.org/abs/2305.19187. Works with both whitebox and blackbox models (initialized using lm_polygraph.utils.model.BlackboxModel/WhiteboxModel).
A continuous analogue to the number of semantic sets (higher values means greater uncertainty).
lm_polygraph.estimators.ensemble_sequence_measures module
lm_polygraph.estimators.ensemble_token_measures module
- class lm_polygraph.estimators.ensemble_token_measures.EPEnsembleEstimator[source]
Bases:
EnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTdu[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTent10[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTent15[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTent5[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTepkl[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTmi[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTrmi[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.EPTtu[source]
Bases:
EPEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PEEnsembleEstimator[source]
Bases:
EnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETdu[source]
Bases:
PEEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETent10[source]
Bases:
PEEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETent15[source]
Bases:
PEEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETent5[source]
Bases:
PEEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETepkl[source]
Bases:
PEEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETmi[source]
Bases:
PEEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETrmi[source]
Bases:
PEEnsembleEstimator
- class lm_polygraph.estimators.ensemble_token_measures.PETtu[source]
Bases:
PEEnsembleEstimator
lm_polygraph.estimators.estimator module
lm_polygraph.estimators.fisher_rao module
- class lm_polygraph.estimators.fisher_rao.FisherRao(verbose: bool = False, temperature: float = 2)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “FisherRao” as provided in the paper https://arxiv.org/pdf/2212.09171.pdf. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
This method calculates the generation Fisher-Rao distance between probability distribution for each token and uniform distribution. Code adapted from https://github.com/icannos/Todd/blob/master/Todd/itscorers.py
lm_polygraph.estimators.lexical_similarity module
- class lm_polygraph.estimators.lexical_similarity.LexicalSimilarity(metric: str = 'rougeL')[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Lexical Similarity” as provided in the paper https://arxiv.org/abs/2302.09664. Works with both whitebox and blackbox models (initialized using lm_polygraph.utils.model.BlackboxModel/WhiteboxModel).
The method calculates mean similarity between all pairs of sampled generations with minus sign. The number of samples is controlled by lm_polygraph.stat_calculators.sample.SamplingGenerationCalculator ‘samples_n’ parameter.
lm_polygraph.estimators.mahalanobis_distance module
- class lm_polygraph.estimators.mahalanobis_distance.MahalanobisDistanceSeq(embeddings_type: str = 'decoder', parameters_path: str | None = None, normalize: bool = False)[source]
Bases:
Estimator
lm_polygraph.estimators.max_probability module
- class lm_polygraph.estimators.max_probability.MaximumSequenceProbability[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model by calculating the log-probability of the generation with minus sign. It is calculated as the sum of log-probabilities in each token. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
lm_polygraph.estimators.monte_carlo_normalized_sequence_entropy module
lm_polygraph.estimators.monte_carlo_sequence_entropy module
lm_polygraph.estimators.num_sem_sets module
- class lm_polygraph.estimators.num_sem_sets.NumSemSets(verbose: bool = False)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Number of Semantic Sets” as provided in the paper https://arxiv.org/abs/2305.19187. Works with both whitebox and blackbox models (initialized using lm_polygraph.utils.model.BlackboxModel/WhiteboxModel).
lm_polygraph.estimators.p_true module
- class lm_polygraph.estimators.p_true.PTrue[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “P(True)” as provided in the paper https://arxiv.org/abs/2207.05221. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
- On input question q and model generation a, the method uses the following prompt:
Question: {q} Possible answer: {a} Is the possible answer:
True
False
The possible answer is:
and calculates the log-probability of ‘True’ token with minus sign.
lm_polygraph.estimators.p_true_sampling module
- class lm_polygraph.estimators.p_true_sampling.PTrueSampling[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “P(True)” as provided in the paper https://arxiv.org/abs/2207.05221 and model samples. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
On input question q, model generation a and several text samples s, the method uses the following prompt:
Question: {q} Here are some ideas that were brainstormed: {s} Possible answer: {a} Is the possible answer:
True
False
The possible answer is:
and calculates the log-probability of True token with minus sign.
lm_polygraph.estimators.perplexity module
lm_polygraph.estimators.pointwise_mutual_information module
- class lm_polygraph.estimators.pointwise_mutual_information.MeanPointwiseMutualInformation[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model using Pointwise Mutual Information. The sequence-level estimation is calculated as average token-level PMI estimations. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
lm_polygraph.estimators.ppl_md module
lm_polygraph.estimators.rde module
- class lm_polygraph.estimators.rde.RDESeq(embeddings_type: str = 'decoder', parameters_path: str | None = None, normalize: bool = False)[source]
Bases:
EstimatorThe RDE method improves over MD by reducing the dimensionality of h(x) via PCA decomposition. It also computes the covariance matrix in a robust way using the Minimum Covariance Determinant estimate (Rousseeuw, 1984).
lm_polygraph.estimators.relative_mahalanobis_distance module
- class lm_polygraph.estimators.relative_mahalanobis_distance.RelativeMahalanobisDistanceSeq(embeddings_type: str = 'decoder', parameters_path: str | None = None, normalize: bool = False)[source]
Bases:
EstimatorRen et al. (2023) showed that it might be useful to adjust the Mahalanobis distance score by subtracting from it the other Mahalanobis distance MD_0(x) computed for some large general purpose dataset covering many domain. RMD(x) = MD(x) - MD_0(x)
lm_polygraph.estimators.renyi_neg module
- class lm_polygraph.estimators.renyi_neg.RenyiNeg(verbose: bool = False, alpha: float = 0.5, temperature: float = 2)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “RenyiNeg” as provided in the paper https://arxiv.org/pdf/2212.09171.pdf. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
This method calculates the generation Rényi divergence between probability distribution for each token and uniform distribution. Code adapted from https://github.com/icannos/Todd/blob/master/Todd/itscorers.py
lm_polygraph.estimators.sar module
- class lm_polygraph.estimators.sar.SAR(verbose: bool = False)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Token SAR” as provided in the paper https://arxiv.org/abs/2307.01379. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
This method calculates the sum of corrected probability using tokenSAR of the generated text and text relevance relative to all other generations.
lm_polygraph.estimators.semantic_entropy module
- class lm_polygraph.estimators.semantic_entropy.SemanticEntropy(verbose: bool = False, class_probability_estimation: str = 'sum')[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Semantic entropy” as provided in the paper https://arxiv.org/abs/2302.09664. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
This method calculates the generation entropy estimations merged by semantic classes using Monte-Carlo. The number of samples is controlled by lm_polygraph.stat_calculators.sample.SamplingGenerationCalculator ‘samples_n’ parameter.
lm_polygraph.estimators.semantic_entropy_token module
lm_polygraph.estimators.sentence_sar module
- class lm_polygraph.estimators.sentence_sar.SentenceSAR(verbose: bool = False)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Sentence SAR” as provided in the paper https://arxiv.org/abs/2307.01379. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
This method calculates the sum of the probability of the generated text and text relevance relative to all other generations.
lm_polygraph.estimators.token_entropy module
- class lm_polygraph.estimators.token_entropy.MeanTokenEntropy[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model by calculating the mean entropy among all tokens in the generation. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
lm_polygraph.estimators.token_sar module
- class lm_polygraph.estimators.token_sar.TokenSAR(verbose: bool = False)[source]
Bases:
EstimatorEstimates the sequence-level uncertainty of a language model following the method of “Token SAR” as provided in the paper https://arxiv.org/abs/2307.01379. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).
This method calculates the weighted sum of log_likelihoods with weights computed using token relevance.