lczerolens.xai#
XAI module.
- class lczerolens.xai.ActivationLens(module_exp: str | None = None)#
Class for activation-based XAI methods.
- analyse_board(board: Board, wrapper: ModelWrapper, **kwargs) Any#
Cache the activations for a given model and input.
- analyse_dataset(dataset: Dataset, wrapper: ModelWrapper, batch_size: int, collate_fn: Callable | None = None, save_to: str | None = None, **kwargs) Dict[str, Any] | None#
Cache the activations for a given model and dataset.
- is_compatible(wrapper: ModelWrapper) bool#
Caching is compatible with all torch models.
- class lczerolens.xai.AndBinaryConcept(*concepts: BinaryConcept)#
Class for binary concept-based XAI methods.
- compute_label(board: Board) Any#
Compute the label for a given model and input.
- class lczerolens.xai.BestLegalMoveConcept(wrapper: ModelWrapper)#
Class for move concept-based XAI methods.
- compute_label(board: Board) int#
Compute the label for a given model and input.
- class lczerolens.xai.BinaryConcept#
Class for binary concept-based XAI methods.
- static compute_metrics(predictions, labels)#
Compute the metrics for a given model and input.
- class lczerolens.xai.ConceptDataset(file_name: str | None = None, boards: List[Board] | None = None, game_ids: List[str] | None = None, concept: Concept | None = None, labels: List[Any] | None = None, first_n: int | None = None)#
Class for concept
- save(file_name: str, n_history: int = 0, indices=None)#
Save the dataset to a file.
Note
As the board needs to be unpiled use the preprocess_game method.
- class lczerolens.xai.CrpLens#
Class for wrapping the LCZero models.
- analyse_board(board: Board, wrapper: ModelWrapper, **kwargs) Tensor#
Computes the heatmap for a given board.
- analyse_dataset(dataset: Dataset, wrapper: ModelWrapper, batch_size: int, collate_fn: Callable | None = None, save_to: str | None = None, **kwargs) dict#
Computes the statistics for a given board.
- is_compatible(wrapper: ModelWrapper) bool#
Returns whether the lens is compatible with the model.
- class lczerolens.xai.HasMateThreatConcept#
Class for material concept-based XAI methods.
- compute_label(board: Board) int#
Compute the label for a given model and input.
- class lczerolens.xai.HasMaterialAdvantageConcept(relative: bool = True)#
Class for material concept-based XAI methods.
- piece_values#
The piece values.
- Type:
Dict[int, int]
- compute_label(board: Board, piece_values: Dict[int, int] | None = None) int#
Compute the label for a given model and input.
- class lczerolens.xai.HasPieceConcept(piece: str, relative: bool = True)#
Class for material concept-based XAI methods.
- compute_label(board: Board) int#
Compute the label for a given model and input.
- class lczerolens.xai.HasThreatConcept(piece: str, relative: bool = True)#
Class for material concept-based XAI methods.
- compute_label(board: Board) int#
Compute the label for a given model and input.
- class lczerolens.xai.Lens#
Generic lens class.
- abstract analyse_board(board: Board, wrapper: ModelWrapper, **kwargs) Any#
Computes the heatmap for a given board.
- abstract analyse_dataset(dataset: Dataset, wrapper: ModelWrapper, batch_size: int, collate_fn: Callable | None = None, save_to: str | None = None, **kwargs) Dict[Any, Any] | None#
Computes the statistics for a given board.
- abstract is_compatible(wrapper: ModelWrapper) bool#
Returns whether the lens is compatible with the model.
- classmethod register(name: str)#
Registers the lens.
- class lczerolens.xai.LrpLens#
Class for wrapping the LCZero models.
- analyse_board(board: Board, wrapper: ModelWrapper, **kwargs) Tensor#
Runs basic LRP on the model.
- Parameters:
board (chess.Board) – The board to compute the heatmap for.
wrapper (ModelWrapper) – The model wrapper.
- Returns:
The heatmap for the given board.
- Return type:
torch.Tensor
- analyse_dataset(dataset: Dataset, wrapper: ModelWrapper, batch_size: int, collate_fn: Callable | None = None, save_to: str | None = None, **kwargs) Dict[int, Tensor] | None#
Cache the activations for a given model and dataset.
- static context(wrapper: ModelWrapper, composite: Composite | None = None, replace_onnx2torch: bool = True, linearise_softmax: bool = False)#
Context manager for the lens.
- is_compatible(wrapper: ModelWrapper) bool#
Returns whether the lens is compatible with the model.
- Parameters:
wrapper (ModelWrapper) – The model wrapper.
- Returns:
Whether the lens is compatible with the model.
- Return type:
bool
- class lczerolens.xai.OrBinaryConcept(*concepts: BinaryConcept)#
Class for binary concept-based XAI methods.
- compute_label(board: Board) Any#
Compute the label for a given model and input.
- class lczerolens.xai.PatchingLens(patching_dict: Dict[str, Callable])#
Class for patching-based XAI methods.
- analyse_board(board: Board, wrapper: ModelWrapper, **kwargs) Tensor#
Analyse a single board with the probing lens.
- analyse_dataset(dataset: Dataset, wrapper: ModelWrapper, batch_size: int, collate_fn: Callable | None = None, save_to: str | None = None, **kwargs) dict#
Analyse a dataset with the probing lens.
- is_compatible(wrapper: ModelWrapper) bool#
Returns whether the lens is compatible with the model.
- class lczerolens.xai.PieceBestLegalMoveConcept(wrapper: ModelWrapper, piece: str)#
Class for move concept-based XAI methods.
- compute_label(board: Board) int#
Compute the label for a given model and input.
- class lczerolens.xai.PolicyLens#
Class for wrapping the LCZero models.
- analyse_board(board: Board, wrapper: ModelWrapper, **kwargs) Tensor#
Compute the policy for a given board.
- analyse_dataset(dataset: Dataset, wrapper: ModelWrapper, batch_size: int, collate_fn: Callable | None = None, save_to: str | None = None, **kwargs) Dict[int, Any] | None#
Computes the statistics for a given board.
- is_compatible(wrapper: ModelWrapper) bool#
Returns whether the lens is compatible with the model.
- class lczerolens.xai.ProbingLens(probe_dict: Dict[str, Probe])#
Class for probing-based XAI methods.
- analyse_board(board: Board, wrapper: ModelWrapper, **kwargs) Tensor#
Analyse a single board with the probing lens.
- analyse_dataset(dataset: Dataset, wrapper: ModelWrapper, batch_size: int, collate_fn: Callable | None = None, save_to: str | None = None, **kwargs) dict#
Analyse a dataset with the probing lens.
- is_compatible(wrapper: ModelWrapper) bool#
Returns whether the lens is compatible with the model.