Skip to content

Model Wrappers

trulens_explain.trulens.nn.models

The TruLens library is designed to support models implemented via a variety of different popular python neural network frameworks: Keras (with TensorFlow or Theano backend), TensorFlow, and Pytorch. Models developed with different frameworks implement things (e.g., gradient computations) a number of different ways. We define framework specific ModelWrapper instances to create a unified model API, providing the same functionality to models that are implemented in disparate frameworks. In order to compute attributions for a model, we provide a trulens.nn.models.get_model_wrapper function that will return an appropriate ModelWrapper instance.

Some parameters are exclusively utilized for specific frameworks and are outlined in the parameter descriptions.

Functions

get_model_wrapper

get_model_wrapper(
    model: ModelLike,
    *,
    logit_layer=None,
    replace_softmax: bool = False,
    softmax_layer=-1,
    custom_objects=None,
    device: str = None,
    input_tensors=None,
    output_tensors=None,
    internal_tensor_dict=None,
    default_feed_dict=None,
    session=None,
    backend=None,
    force_eval=True,
    **kwargs
)

Returns a ModelWrapper implementation that exposes the components needed for computing attributions.

PARAMETER DESCRIPTION
model

The model to wrap. If using the TensorFlow 1 backend, this is expected to be a graph object.

TYPE: ModelLike

logit_layer

Supported for Keras and Pytorch models. Specifies the name or index of the layer that produces the logit predictions.

DEFAULT: None

replace_softmax

Supported for Keras models only. If true, the activation function in the softmax layer (specified by softmax_layer) will be changed to a 'linear' activation.

TYPE: bool DEFAULT: False

softmax_layer

Supported for Keras models only. Specifies the layer that performs the softmax. This layer should have an activation attribute. Only used when replace_softmax is true.

DEFAULT: -1

custom_objects

Optional, for use with Keras models only. A dictionary of custom objects used by the Keras model.

DEFAULT: None

device

Optional, for use with Pytorch models only. A string specifying the device to run the model on.

TYPE: str DEFAULT: None

input_tensors

Required for use with TensorFlow 1 graph models only. A list of tensors representing the input to the model graph.

DEFAULT: None

output_tensors

Required for use with TensorFlow 1 graph models only. A list of tensors representing the output to the model graph.

DEFAULT: None

internal_tensor_dict

Optional, for use with TensorFlow 1 graph models only. A dictionary mapping user-selected layer names to the internal tensors in the model graph that the user would like to expose. This is provided to give more human-readable names to the layers if desired. Internal tensors can also be accessed via the name given to them by tensorflow.

DEFAULT: None

default_feed_dict

Optional, for use with TensorFlow 1 graph models only. A dictionary of default values to give to tensors in the model graph.

DEFAULT: None

session

Optional, for use with TensorFlow 1 graph models only. A tf.Session object to run the model graph in. If None, a new temporary session will be generated every time the model is run.

DEFAULT: None

backend

Optional, for forcing a specific backend. String values recognized are pytorch, tensorflow, keras, or tf.keras.

DEFAULT: None

force_eval

_Optional, True will force a model.eval() call for PyTorch models. False will retain current model state

DEFAULT: True

Returns: ModelWrapper