lm_polygraph.estimators package

Submodules

lm_polygraph.estimators.claim_conditioned_probability module

class lm_polygraph.estimators.claim_conditioned_probability.ClaimConditionedProbability[source]

Bases: Estimator

lm_polygraph.estimators.common module

lm_polygraph.estimators.common.compute_sim_score(answers, affinity, similarity_score)[source]

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: Estimator

Estimates 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: Estimator

Estimates 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: Estimator

Estimates 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).

U_DegMat(i, stats)[source]

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: Estimator

Estimates 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).

U_Eccentricity(i, stats)[source]

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: Estimator

Estimates 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).

U_EigVal_Laplacian(i, stats)[source]

lm_polygraph.estimators.ensemble_sequence_measures module

class lm_polygraph.estimators.ensemble_sequence_measures.EPSrmi[source]

Bases: Estimator

class lm_polygraph.estimators.ensemble_sequence_measures.EPSrmiabs[source]

Bases: Estimator

class lm_polygraph.estimators.ensemble_sequence_measures.EPStu[source]

Bases: Estimator

class lm_polygraph.estimators.ensemble_sequence_measures.PESrmi[source]

Bases: Estimator

class lm_polygraph.estimators.ensemble_sequence_measures.PESrmiabs[source]

Bases: Estimator

class lm_polygraph.estimators.ensemble_sequence_measures.PEStu[source]

Bases: Estimator

lm_polygraph.estimators.ensemble_sequence_measures.all_ep_estimators()[source]
lm_polygraph.estimators.ensemble_sequence_measures.all_pe_estimators()[source]
lm_polygraph.estimators.ensemble_sequence_measures.get_seq_level_ue(sequence_level_data: Dict[str, Tensor]) Dict[str, ndarray][source]

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.EnsembleEstimator[source]

Bases: Estimator

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.ensemble_token_measures.aggregate(posterior_mode, ue_name, token_level_data) ndarray[source]
lm_polygraph.estimators.ensemble_token_measures.all_token_estimators()[source]

lm_polygraph.estimators.estimator module

class lm_polygraph.estimators.estimator.Estimator(**kwargs)[source]

Bases: ABC

Abstract estimator class, which estimates the uncertainty of a language model.

lm_polygraph.estimators.fisher_rao module

class lm_polygraph.estimators.fisher_rao.FisherRao(verbose: bool = False, temperature: float = 2)[source]

Bases: Estimator

Estimates 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: Estimator

Estimates 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.mahalanobis_distance.compute_inv_covariance(centroids, train_features, jitters=None)[source]

This function computes inverse covariance matrix that is required by Mahalanobis distance: MD = sqrt((h(x) - mu)^{T} Sigma^{-1} (h(x) - mu))

lm_polygraph.estimators.mahalanobis_distance.create_cuda_tensor_from_numpy(array)[source]
lm_polygraph.estimators.mahalanobis_distance.mahalanobis_distance_with_known_centroids_sigma_inv(centroids, centroids_mask, sigma_inv, eval_features)[source]
  • This function takes in centroids, centroids_mask, sigma_inv, and eval_features.

  • tensor of Mahalanobis distances is returned.

lm_polygraph.estimators.max_probability module

class lm_polygraph.estimators.max_probability.MaximumSequenceProbability[source]

Bases: Estimator

Estimates 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).

class lm_polygraph.estimators.max_probability.MaximumTokenProbability[source]

Bases: Estimator

Estimates the token-level uncertainty of a language model by calculating the log-probability for each token during autoregressive generation.

lm_polygraph.estimators.monte_carlo_normalized_sequence_entropy module

class lm_polygraph.estimators.monte_carlo_normalized_sequence_entropy.MonteCarloNormalizedSequenceEntropy[source]

Bases: Estimator

lm_polygraph.estimators.monte_carlo_sequence_entropy module

class lm_polygraph.estimators.monte_carlo_sequence_entropy.MonteCarloSequenceEntropy[source]

Bases: Estimator

lm_polygraph.estimators.num_sem_sets module

class lm_polygraph.estimators.num_sem_sets.NumSemSets(verbose: bool = False)[source]

Bases: Estimator

Estimates 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).

U_NumSemSets(i, stats)[source]
find_connected_components(graph)[source]

lm_polygraph.estimators.p_true module

class lm_polygraph.estimators.p_true.PTrue[source]

Bases: Estimator

Estimates 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:

  1. True

  2. 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: Estimator

Estimates 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:

  1. True

  2. False

The possible answer is:

and calculates the log-probability of True token with minus sign.

lm_polygraph.estimators.perplexity module

class lm_polygraph.estimators.perplexity.Perplexity[source]

Bases: Estimator

lm_polygraph.estimators.pointwise_mutual_information module

class lm_polygraph.estimators.pointwise_mutual_information.MeanPointwiseMutualInformation[source]

Bases: Estimator

Estimates 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).

class lm_polygraph.estimators.pointwise_mutual_information.PointwiseMutualInformation[source]

Bases: Estimator

Estimates the token-level uncertainty of a language model using Pointwise Mutual Information. Works only with whitebox models (initialized using lm_polygraph.utils.model.WhiteboxModel).

lm_polygraph.estimators.ppl_md module

class lm_polygraph.estimators.ppl_md.PPLMDSeq(embeddings_type: str = 'decoder', md_type: str = 'MD', parameters_path: str | None = None, normalize: bool = False)[source]

Bases: Estimator

lm_polygraph.estimators.ppl_md.load_array(filename)[source]
lm_polygraph.estimators.ppl_md.rank(target_array, source_array)[source]
lm_polygraph.estimators.ppl_md.save_array(array, filename)[source]

lm_polygraph.estimators.rde module

lm_polygraph.estimators.rde.MCD_covariance(X, y=None, label=None, seed=42)[source]
class lm_polygraph.estimators.rde.RDESeq(embeddings_type: str = 'decoder', parameters_path: str | None = None, normalize: bool = False)[source]

Bases: Estimator

The 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).

load_mcd()[source]
load_pca()[source]
save_mcd()[source]
save_pca()[source]
lm_polygraph.estimators.rde.load_array(filename)[source]
lm_polygraph.estimators.rde.save_array(array, filename)[source]

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: Estimator

Ren 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.relative_mahalanobis_distance.load_array(filename)[source]
lm_polygraph.estimators.relative_mahalanobis_distance.save_array(array, filename)[source]

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: Estimator

Estimates 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: Estimator

Estimates 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: Estimator

Estimates 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.

batched_call(hyps_list: List[List[str]], loglikelihoods_list: List[List[float]] | None, log_weights: List[List[float]] | None = None) array[source]

lm_polygraph.estimators.semantic_entropy_token module

class lm_polygraph.estimators.semantic_entropy_token.SemanticEntropyToken(tokenizer_path: str, tokenizer_save_path: str, semantic_bert_path: str = 'sentence-transformers/bert-base-nli-mean-tokens', batch_size: int = 10)[source]

Bases: Estimator

lm_polygraph.estimators.semantic_entropy_token.split_classes(tokens: List[str], batch_size: int, semantic_bert_path: str = 'sentence-transformers/bert-base-nli-mean-tokens', sim_threshold: float = 0.85) ndarray[source]

lm_polygraph.estimators.sentence_sar module

class lm_polygraph.estimators.sentence_sar.SentenceSAR(verbose: bool = False)[source]

Bases: Estimator

Estimates 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: Estimator

Estimates 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).

class lm_polygraph.estimators.token_entropy.TokenEntropy[source]

Bases: Estimator

Estimates the token-level uncertainty of a language model by calculating the entropy for each token 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: Estimator

Estimates 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.

Module contents