scanvi_explainer.scanvi_deep.SCANVIDeep#

class scanvi_explainer.scanvi_deep.SCANVIDeep(model: SCANVI, train_size: float = 0.8, batch_size: int = 128)#

SCANVIDeep is an extension of DeepExplainer [Lundberg and Lee, 2017] for models trained using SCANVI [Xu et al., 2021].

Parameters:

Explainer – Main Explainer class from shap package

Methods table#

add_handles(model, forward_handle, ...)

Add handles to all non-container layers in the model.

add_target_handle(layer)

explain_row(*row_args, max_evals, ...)

Explains a single row and returns the tuple (row_values, row_expected_values, row_mask_shapes, main_effects).

get_train_test()

gradient(idx, input_x, input_batch)

load(in_file[, model_loader, masker_loader, ...])

Load an Explainer from the given file stream.

memory_stats()

Helper function to track CUDA memory usage.

remove_attributes(model)

Removes the x and y attributes which were added by the forward handles Recursively searches for non-container layers

save(out_file[, model_saver, masker_saver])

Write the explainer to the given file stream.

shap_values([with_labels])

Estimate SHAP values

supports_model_with_masker(model, masker)

Determines if this explainer can handle the given model.

Methods#

SCANVIDeep.add_handles(model, forward_handle, backward_handle)#

Add handles to all non-container layers in the model. Recursively for non-container layers

SCANVIDeep.add_target_handle(layer)#
SCANVIDeep.explain_row(*row_args, max_evals, main_effects, error_bounds, outputs, silent, **kwargs)#

Explains a single row and returns the tuple (row_values, row_expected_values, row_mask_shapes, main_effects).

This is an abstract method meant to be implemented by each subclass.

Returns:

A tuple of (row_values, row_expected_values, row_mask_shapes), where row_values is an array of the attribution values for each sample, row_expected_values is an array (or single value) representing the expected value of the model for each sample (which is the same for all samples unless there are fixed inputs present, like labels when explaining the loss), and row_mask_shapes is a list of all the input shapes (since the row_values is always flattened),

Return type:

tuple

SCANVIDeep.get_train_test() tuple[dict[str, Tensor], dict[str, Tensor]]#
SCANVIDeep.gradient(idx, input_x, input_batch)#
classmethod SCANVIDeep.load(in_file, model_loader=<bound method Model.load of <class 'shap.models._model.Model'>>, masker_loader=<bound method Serializable.load of <class 'shap.maskers._masker.Masker'>>, instantiate=True)#

Load an Explainer from the given file stream.

Parameters:

in_file (The file stream to load objects from.)

SCANVIDeep.memory_stats()#

Helper function to track CUDA memory usage.

SCANVIDeep.remove_attributes(model)#

Removes the x and y attributes which were added by the forward handles Recursively searches for non-container layers

SCANVIDeep.save(out_file, model_saver='.save', masker_saver='.save')#

Write the explainer to the given file stream.

SCANVIDeep.shap_values(with_labels: bool = False) ndarray | list[ndarray]#

Estimate SHAP values

Parameters:

with_labels (bool) – Return labels for X_test

Returns:

3D dataset where index 0: classifier index 1: X_test 2: Features

Return type:

np.ndarray | list[np.ndarray]

static SCANVIDeep.supports_model_with_masker(model, masker)#

Determines if this explainer can handle the given model.

This is an abstract static method meant to be implemented by each subclass.