Skip to content

xpdeep_model

Define the xpdeep explainable model.

Classes:

Name Description
XpdeepModel

Xpdeep Model class.

XpdeepModel #

Xpdeep Model class.

Parameters:

Name Type Description Default

feature_extraction #

TorchModel

The feature extraction model, responsible to extract the most important and coherent features prior to the task you want to achieve.

required

task_learner #

TorchModel

The task learner model is responsible to achieve your task (classification etc.), given a set of meaningful extracted features.

required

backbone #

TorchModel | None

The backbone model, having the same role as a traditional backbone model on a neural network. default None.

None

decision_graph_parameters #

Internal parameters and architecture of the Xpdeep explainable model.

required

seed #

int

Seed for reproducibility.

0

Methods:

Name Description
from_torch

Build a xpdeep model from torch modules.

get_output_size

Infer the model output size without batch size as it is required to serialize a loss function.

__repr__

Represent the model.

Attributes:

Name Type Description
feature_extraction TorchModel
task_learner TorchModel
decision_graph_parameters ModelDecisionGraphParameters
backbone TorchModel | None
seed int

feature_extraction: TorchModel #

task_learner: TorchModel #

decision_graph_parameters: ModelDecisionGraphParameters #

backbone: TorchModel | None = None #

seed: int = 0 #

from_torch(fitted_schema: FittedSchema, feature_extraction: torch.nn.Module, task_learner: torch.nn.Module, decision_graph_parameters: ModelDecisionGraphParameters, backbone: torch.nn.Module | None = None) -> XpdeepModel #

Build a xpdeep model from torch modules.

Source code in src/xpdeep/model/xpdeep_model.py
@staticmethod
def from_torch(
    fitted_schema: FittedSchema,
    feature_extraction: torch.nn.Module,
    task_learner: torch.nn.Module,
    decision_graph_parameters: ModelDecisionGraphParameters,
    backbone: torch.nn.Module | None = None,
) -> "XpdeepModel":
    """Build a xpdeep model from torch modules."""
    input_size = fitted_schema.input_size[1:]
    if backbone is not None:
        backbone_torch_model = TorchModel.from_torch_module(backbone, input_size=input_size)
        input_size_feature_extraction_model = backbone(torch.zeros((5, *input_size))).shape[
            1:
        ]  # No batch dimension required.
    else:
        backbone_torch_model = None
        input_size_feature_extraction_model = input_size
    feature_extraction_model = TorchModel.from_torch_module(
        feature_extraction, input_size=input_size_feature_extraction_model
    )

    input_size_task_model = feature_extraction(torch.zeros((5, *input_size_feature_extraction_model))).shape[1:]
    task_torch_model = TorchModel.from_torch_module(task_learner, input_size=input_size_task_model)
    return XpdeepModel(
        feature_extraction=feature_extraction_model,
        task_learner=task_torch_model,
        decision_graph_parameters=decision_graph_parameters,
        backbone=backbone_torch_model,
    )

get_output_size(schema: FittedSchema) -> tuple[int, ...] #

Infer the model output size without batch size as it is required to serialize a loss function.

Source code in src/xpdeep/model/xpdeep_model.py
def get_output_size(self, schema: FittedSchema) -> tuple[int, ...]:
    """Infer the model output size without batch size as it is required to serialize a loss function."""
    x = torch.ones((5, *schema.input_size[1:]))  # Batch size of one is not accepted by batch normalization layer.
    if self.backbone is not None:
        x = self.backbone.original_module(x)
    x = self.feature_extraction.original_module(x)
    x = self.task_learner.original_module(x)
    x = cast(torch.Tensor, x)
    return x.shape[1:]

__repr__() -> str #

Represent the model.

Source code in src/xpdeep/model/xpdeep_model.py
def __repr__(self) -> str:
    """Represent the model."""
    model_string = repr(self.decision_graph_parameters)
    model_string += f"\n\nFeature extraction model:\n{self.feature_extraction.original_module}"
    model_string += f"\n\nTask learner model:\n{self.task_learner.original_module}"
    if self.backbone is not None:
        model_string += f"\n\nBackbone model:\n{self.backbone.original_module}"

    return model_string