Base Classes & Utilities¶
The base interpretation class defines the shared interface for making concept
dimensions interpretable. The extract_ngrams utility provides reusable
preprocessing for text-based interpretations.
API Reference¶
interpreto.concepts.interpretations.base.BaseConceptInterpretationMethod
¶
BaseConceptInterpretationMethod(concept_explainer, activation_granularity=None, aggregation_strategy=MEAN, concept_encoding_batch_size=1024, use_vocab=False, use_unique_words=0, unique_words_kwargs={})
Bases: ABC
Code: concepts/interpretations/base.py
Abstract class defining an interface for concept interpretation. Its goal is to make the dimensions of the concept space interpretable by humans.
Attributes:
| Name | Type | Description |
|---|---|---|
concept_explainer |
ConceptEncoderExplainer
|
The concept explainer used to compute the concept activations. |
activation_granularity |
ActivationGranularity
|
The granularity of the activations to use for the interpretation.
See :method: |
aggregation_strategy |
GranularityAggregationStrategy
|
The aggregation strategy to use for the activations.
See :method: |
concept_encoding_batch_size |
int
|
The batch size to use for the concept encoding. |
use_vocab |
bool
|
Whether to use the vocabulary to extract the granular inputs. If True, the granular inputs are extracted from the vocabulary. If False, the granular inputs are extracted from the inputs. |
use_unique_words |
bool
|
If True, the interpretation will be computed from the unique words of the inputs.
Incompatible with |
unique_words_kwargs |
dict
|
The kwargs to pass to the |
Source code in interpreto/concepts/interpretations/base.py
concepts_activations_from_source
¶
concepts_activations_from_source(*, inputs=None, latent_activations=None, concepts_activations=None)
Computes the concepts activations from the given samples.
Samples can be provided as raw text (inputs), latent activations (latent_activations),
or directly concept activations (concepts_activations).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[str] | None
|
The indices of the concepts to interpret. |
None
|
|
Float[Tensor, 'nl d'] | None
|
The latent activations |
None
|
|
Float[Tensor, 'nl cpt'] | None
|
The concepts activations |
None
|
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'nl cpt']
|
Float[torch.Tensor, "nl cpt"] : |
Source code in interpreto/concepts/interpretations/base.py
concepts_activations_from_vocab
¶
Computes the concepts activations for each token of the vocabulary
Returns:
| Type | Description |
|---|---|
tuple[list[str], Float[Tensor, 'nl cpt']]
|
tuple[list[str], Float[torch.Tensor, "nl cpt"]]: - The list of tokens in the vocabulary - The concept activations for each token |
Source code in interpreto/concepts/interpretations/base.py
get_granular_inputs
¶
get_granular_inputs(inputs)
Split texts from the inputs based on the target granularity (for instance into tokens, words, sentences, ...)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[str]
|
n text samples |
required |
Returns:
| Name | Type | Description |
|---|---|---|
granular_flattened_texts |
list[str]
|
The granular texts elements from the inputs, flattened. [Example1_Tok1, Example1_Tok2, ... Example2_Tok1, Example2_Tok2, ...] |
granular_flattened_sample_id |
list[int]
|
The sample id for each granular text, to keep track of which sample the text belongs to.
It should have the same length as |
Source code in interpreto/concepts/interpretations/base.py
get_granular_inputs_and_concept_activations
¶
get_granular_inputs_and_concept_activations(concepts_indices, inputs=None, latent_activations=None, concepts_activations=None)
Compute the granular inputs and concept activations for the specified concepts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int | list[int] | Literal['all']
|
The indices of the concepts to interpret. If "all", all concepts are interpreted. |
required |
|
list[str] | None
|
The inputs to use for the interpretation.
Necessary if not |
None
|
|
Float[Tensor, 'nl d'] | None
|
The latent activations matching the inputs. If not provided, it is computed from the inputs. |
None
|
|
Float[Tensor, 'nl cpt'] | None
|
The concepts activations matching the inputs. If not provided, it is computed from the inputs or latent activations. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
sure_concepts_indices |
list[int]
|
The indices of the concepts to interpret. |
granular_inputs |
list[str]
|
The granular inputs for the specified concepts. Each element of the list is a single granular input, such as a word. |
sure_concepts_activations |
Float[Tensor, 'nl cpt']
|
The concepts activations matching the granular inputs. |
granular_sample_ids |
list[int]
|
The granular sample ids for the specified concepts.
Each element of the list is the index of the input sample from which the corresponding granular input was extracted.
It has the same length as |
Source code in interpreto/concepts/interpretations/base.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | |
interpret
abstractmethod
¶
interpret(concepts_indices, inputs=None, latent_activations=None, concepts_activations=None)
Interpret the concepts dimensions in the latent space into a human-readable format. The interpretation is a mapping between the concepts indices and an object allowing to interpret them. It can be a label, a description, examples, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int | list[int] | Literal['all']
|
The indices of the concepts to interpret. If "all", all concepts are interpreted. |
required |
|
list[str] | None
|
The inputs to use for the interpretation.
Necessary if not |
None
|
|
Float[Tensor, 'nl d'] | None
|
The latent activations matching the inputs. If not provided, it is computed from the inputs. |
None
|
|
Float[Tensor, 'nl cpt'] | None
|
The concepts activations matching the inputs. If not provided, it is computed from the inputs or latent activations. |
None
|
Returns:
| Type | Description |
|---|---|
Mapping[int, Any]
|
Mapping[int, Any]: The interpretation of each of the specified concepts. |
Source code in interpreto/concepts/interpretations/base.py
interpreto.concepts.interpretations.extract_ngrams
¶
extract_ngrams(inputs, n=1, count_min_threshold=1, return_counts=False, lemmatize=False, words_to_ignore=None)
Extract n-grams (from 1-gram up to n-gram of words) from a list of texts.
If n=3, it extracts 1-grams, 2-grams, and 3-grams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Iterable[str]
|
The texts to extract n-grams from. |
required |
|
int
|
The maximum n-gram size. All sizes from 1 to n are extracted. |
1
|
|
int
|
The minimum total number of occurrences of an n-gram in the whole |
1
|
|
bool
|
Whether to return the counts of each n-gram. Defaults to False. |
False
|
|
bool
|
Whether to lemmatize words before counting. |
False
|
|
list[str] | None
|
A list of words to ignore (applied to individual tokens before forming n-grams). |
None
|
Returns:
| Type | Description |
|---|---|
list[str] | Counter[str]
|
list[str] | Counter[str]: The list of unique n-grams or the counts of each n-gram. |