lczerolens.xai.lenses#
Lenses implementation for XAI
- class lczerolens.xai.lenses.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.lenses.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.lenses.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.lenses.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.lenses.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.lenses.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.
Abstract Lens#
- 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.
Policy Lens#
PolicyLens class for wrapping the LCZero models.
- 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.
LRP Lens#
Compute LRP heatmap for a given model and input.
- 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
Probing Lens#
Probing lens for XAI.
- 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.