mllm_shap.shap.base package#

Submodules#

mllm_shap.shap.base.approx module#

Base class for SHAP explainers using approximation methods.

class mllm_shap.shap.base.approx.BaseShapApproximation(*args: Any, num_samples: int | None = None, fraction: float = 0.6, **kwargs: Any)[source]#

Bases: BaseShapExplainer, ABC

Base class for SHAP explainers using approximation methods.

allow_mask_duplicates: bool#

Whether to allow duplicate masks during generation.

embedding_model: BaseExternalEmbedding | None#

The external embedding model to use. If provided, overrides mode.

embedding_reducer: BaseEmbeddingReducer#

The embedding reduction strategy to use.

fraction: float | None#

Fraction of total possible masks to generate if num_samples is None.

include_minimal_masks: bool = True#

Whether to include minimal masks (single-feature and empty masks) in the sampling.

mode: Mode#

The SHAP mode, either STATIC or CONTEXTUAL. Used if no embedding_model is provided.

normalizer: BaseNormalizer#

The SHAP value normalizer to use.

num_samples: int | None#

Number of random masks to generate. If None, uses fraction. -1 stands for minimal number of samples (only single-feature masks and empty mask).

similarity_measure: BaseEmbeddingSimilarity#

The embedding similarity measure to use.

total_n_calls: int = 0#

Total number of MLLM calls made for last explanation.

mllm_shap.shap.base.complementary module#

Base class for SHAP explainers using approximation methods.

class mllm_shap.shap.base.complementary.BaseComplementaryShapApproximation(*args: Any, num_samples: int | None = None, fraction: float = 0.6, **kwargs: Any)[source]#

Bases: BaseShapApproximation, ABC

Complementary SHAP implementation class.

allow_mask_duplicates: bool#

Whether to allow duplicate masks during generation.

embedding_model: BaseExternalEmbedding | None#

The external embedding model to use. If provided, overrides mode.

embedding_reducer: BaseEmbeddingReducer#

The embedding reduction strategy to use.

fraction: float | None#

Fraction of total possible masks to generate if num_samples is None.

include_minimal_masks: bool = True#

Whether to include minimal masks (single-feature and empty masks) in the sampling.

mode: Mode#

The SHAP mode, either STATIC or CONTEXTUAL. Used if no embedding_model is provided.

normalizer: BaseNormalizer#

The SHAP value normalizer to use.

num_samples: int | None#

Number of random masks to generate. If None, uses fraction. -1 stands for minimal number of samples (only single-feature masks and empty mask).

similarity_measure: BaseEmbeddingSimilarity#

The embedding similarity measure to use.

total_n_calls: int = 0#

Total number of MLLM calls made for last explanation.

mllm_shap.shap.base.embeddings module#

Base class for embedding calculation reduction strategies.

class mllm_shap.shap.base.embeddings.BaseEmbeddingReducer(n: int | None = None)[source]#

Bases: ABC

Base class for embedding reduction strategies.

n: int | None#

Maximum number of embeddings to sample before reduction. None means no sampling.

class mllm_shap.shap.base.embeddings.BaseExternalEmbedding[source]#

Bases: ABC

Base class for external embeddings.

mllm_shap.shap.base.explainer module#

Base class for SHAP-based explanations.

class mllm_shap.shap.base.explainer.BaseExplainer(model: BaseMllmModel, shap_explainer: BaseShapExplainer)[source]#

Bases: ABC

Convenience base client for SHAP explainers.

model: BaseMllmModel#

The model connector instance.

shap_explainer: BaseShapExplainer#

The SHAP explainer instance.

total_n_calls: int = 0#

Total number of MLLM calls made for last explanation.

mllm_shap.shap.base.normalizers module#

Base class for SHAP value normalizers.

class mllm_shap.shap.base.normalizers.BaseNormalizer[source]#

Bases: ABC

Base class for SHAP value normalizers.

mllm_shap.shap.base.shap_explainer module#

Base class for SHAP-based explanations.

class mllm_shap.shap.base.shap_explainer.BaseShapExplainer(mode: Mode = Mode.CONTEXTUAL, embedding_model: BaseExternalEmbedding | None = None, embedding_reducer: BaseEmbeddingReducer | None = None, similarity_measure: BaseEmbeddingSimilarity | None = None, normalizer: BaseNormalizer | None = None, allow_mask_duplicates: bool = False)[source]#

Bases: ABC

Base class for SHAP-based explanations.

allow_mask_duplicates: bool#

Whether to allow duplicate masks during generation.

embedding_model: BaseExternalEmbedding | None#

The external embedding model to use. If provided, overrides mode.

embedding_reducer: BaseEmbeddingReducer#

The embedding reduction strategy to use.

mode: Mode#

The SHAP mode, either STATIC or CONTEXTUAL. Used if no embedding_model is provided.

normalizer: BaseNormalizer#

The SHAP value normalizer to use.

similarity_measure: BaseEmbeddingSimilarity#

The embedding similarity measure to use.

total_n_calls: int = 0#

Total number of MLLM calls made for last explanation.

exception mllm_shap.shap.base.shap_explainer.NotEnoughTokensToExplainError[source]#

Bases: Exception

Raised when there are not enough tokens to explain in the chat.

add_note()#

Exception.add_note(note) – add a note to the exception

args#
with_traceback()#

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

mllm_shap.shap.base.similarity module#

Base class for embedding similarity calculations.

class mllm_shap.shap.base.similarity.BaseEmbeddingSimilarity[source]#

Bases: ABC

Base class for embedding similarity calculations.

operates_on_embeddings: bool = True#

Indicates that the similarity operates on embeddings. If False, it operates on raw tokens.

Used to resolve input to __call__().

Module contents#

Base module for mllm_shap.shap package.