lczerolens.xai.lenses#

Lenses implementation for XAI

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.

classmethod from_name(name: str, **kwargs) Lens#

Returns the lens from its name.

classmethod get_subclass(name: str) Type[Lens]#

Returns the subclass.

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.