activations¶
Activation Index¶
Activation Reader¶
- class diagnnose.activations.activation_reader.ActivationReader(activations_dir: Optional[str] = None, activation_dict: Optional[Dict[Tuple[int, str], Tensor]] = None, activation_names: Optional[List[Tuple[int, str]]] = None, activation_ranges: Optional[List[Tuple[int, int]]] = None, selection_func: Optional[Callable[[int, Example], bool]] = None, store_multiple_activations: bool = False, cat_activations: bool = False)[source]¶
Bases:
object
Reads in pickled activations that have been extracted.
An
ActivationReader
can also be created directly from anActivationDict
, in which case the correspondingActivationRanges
andSelectionFunc
should be provided too.- Parameters
activations_dir (str, optional) – Directory containing the extracted activations.
activation_dict (ActivationDict, optional) – If activations have not been extracted to disk, the activation_dict containing all extracted activations can be provided directly as well.
activation_names (ActivationNames, optional) – Activation names, provided as a list of
(layer, name)
tuples. If not provided the index to__getitem__()
must always contain the activation_name that is being requested, as theActivationReader
can not infer it automatically.activation_ranges (ActivationRanges, optional) –
ActivationRanges
dictionary that should be provided ifactivation_dict
is passed directly.selection_func (SelectionFunc, optional) –
SelectionFunc
that was used for extraction and that should be passed ifactivation_dict
is passed directly.store_multiple_activations (bool, optional) – Set to true to store multiple activation arrays in RAM at once. Defaults to False, meaning that only one activation type will be stored in the class.
cat_activations (bool, optional) – Toggle to concatenate the activations returned by
__getitem__()
. Otherwise the activations will be split into a tuple with each each tuple item containing the activations of one sentence.
- __getitem__(key: Union[int, slice, List[int], Tensor, Tuple[Union[int, slice, List[int], Tensor], Tuple[int, str]]]) Union[Tensor, Iterator[Tensor]] [source]¶
Allows for concise and efficient indexing of activations.
The
key
argument should be either anActivationIndex
(i.e. an iterable that can be used to index a tensor), or a(index, activation_name)
tuple. Anactivation_name
is a tuple of shape(layer, name)
.If multiple activation_names have been extracted the
activation_name
must be provided, otherwise it can be left out.The return value is a generator of tensors, with each tensor of shape (sen_len, nhid), or a concatenated tensor if
self.cat_activations
is set toTrue
.Example usage:
activation_reader = ActivationReader( dir, activation_names=[(0, "hx"), (1, "hx")], **kwargs ) # activation_name must be passed because ActivationReader # contains two activation_names. activations_first_sen = activation_reader[0, (1, "hx")] all_activations = activation_reader[:, (1, "hx")] activation_reader2 = ActivationReader( dir, activation_names=[(1, "hx")], **kwargs ) # activation_name can be left implicit. activations_first_10_sens = activation_reader2[:10]
- Parameters
key (ActivationKey) –
ActivationIndex
or(index, activation_name)
, as explained above.- Returns
split_activations (Tensor | Iterator[Tensor, …]) – Tensor, if
self.cat_activations
is set to True. Otherwise a Generator of tensors, with each item corresponding to the extracted activations of a specific sentence... automethod:: __getitem__
- property activation_ranges: List[Tuple[int, int]]¶
- property selection_func: Callable[[int, Example], bool]¶
Activation Writer¶
- class diagnnose.activations.activation_writer.ActivationWriter(activations_dir: str)[source]¶
Bases:
object
Writes activations to file, using an ExitStack.
- Parameters
activations_dir (str, optional) – Directory to which activations will be written
- concat_pickle_dumps(overwrite: bool = True) None [source]¶
Concatenates a sequential pickle dump and pickles to file .
Note that this overwrites the sequential pickle dump by default.
- Parameters
overwrite (bool, optional) – Set to True to overwrite the file containing the sequential pickle dump, otherwise creates a new file. Defaults to True.
- create_output_files(stack: ExitStack, activation_names: List[Tuple[int, str]]) None [source]¶
Opens a file for each to-be-extracted activation.
Selection Funcs¶
- diagnnose.activations.selection_funcs.final_sen_token(w_idx: int, item: Example) bool [source]¶
Only returns the final token of a sentence.
- diagnnose.activations.selection_funcs.final_token(sen_column: str = 'sen') Callable[[int, Example], bool] [source]¶
Only returns the final token of a sentence.
Wrapper allows a different
sen_column
to be set, that indicates thesen
attribute of a corpus item that is being processed.
- diagnnose.activations.selection_funcs.first_n(n: int) Callable[[int, Example], bool] [source]¶
Wrapper that creates a selection_func that only returns True for the first n items of a corpus.
- diagnnose.activations.selection_funcs.in_sen_ids(sen_ids: List[int]) Callable[[int, Example], bool] [source]¶
Wrapper that creates a selection_func that only returns True for a sen_id if it is part of the provided list of sen_ids.
- diagnnose.activations.selection_funcs.intersection(selection_funcs: Iterable[Callable[[int, Example], bool]]) Callable[[int, Example], bool] [source]¶
Returns the intersection of an iterable of selection_funcs.
- diagnnose.activations.selection_funcs.negate(selection_func: Callable[[int, Example], bool]) Callable[[int, Example], bool] [source]¶
Returns the negation of a selection_func.
- diagnnose.activations.selection_funcs.no_special_tokens(tokenizer: transformers.PreTrainedTokenizer, sen_column: str = 'sen') Callable[[int, Example], bool] [source]¶
- diagnnose.activations.selection_funcs.nth_token(n: int) Callable[[int, Example], bool] [source]¶
Wrapper that creates a selection_func that only returns True for the n^{th} token of a sentence.
- diagnnose.activations.selection_funcs.only_mask_token(mask_token: str, sen_column: str = 'sen') Callable[[int, Example], bool] [source]¶