First Order Influence Calculator¶
View source | 📰 Original Paper | 📰 Paper Groups | 📰 Paper RelatIF |
This method is an implementation of the famous technique introduced by Koh & Liang in 2017. In essence, by performing a first-order taylor approximation, it proposes that the influence function of a neural network model can be computed as follows:
where \(H_{\hat{\theta}}^{-1}\) is the inverse of the mean hessian of the loss wrt the model's parameters over the whole dataset, \(\ell\) is the loss function with which the model was trained and \(z\), a point we wish to leave out of the training dataset.
In particular, this computation is carried out by the InverseHessianVectorProduct
class, which allows
to do it in different ways, with each implementation having its pros and cons.
It can be used to compute the self-influence of individual and groups of points, and the influence of training points (or groups) on other test points (or groups).
It also implements the RelatIF technique, which can be computed by setting the normalize
attribute
to True
.
Notebooks¶
FirstOrderInfluenceCalculator
¶
A class implementing the necessary methods to compute the different influence quantities
using a first-order approximation. This makes it ideal for individual points and small
groups of data, as it does so (relatively) efficiently.
__init__(self,
model: deel.influenciae.common.model_wrappers.InfluenceModel,
dataset: tf.Dataset,
ihvp_calculator: Union[str, deel.influenciae.common.inverse_hessian_vector_product.InverseHessianVectorProduct, deel.influenciae.common.inverse_hessian_vector_product.IHVPCalculator] = 'exact',
n_samples_for_hessian: Optional[int] = None,
shuffle_buffer_size: Optional[int] = 10000,
normalize=False)
¶
model: deel.influenciae.common.model_wrappers.InfluenceModel,
dataset: tf.Dataset,
ihvp_calculator: Union[str, deel.influenciae.common.inverse_hessian_vector_product.InverseHessianVectorProduct, deel.influenciae.common.inverse_hessian_vector_product.IHVPCalculator] = 'exact',
n_samples_for_hessian: Optional[int] = None,
shuffle_buffer_size: Optional[int] = 10000,
normalize=False)
Parameters
-
model : deel.influenciae.common.model_wrappers.InfluenceModel
The TF2.X model implementing the InfluenceModel interface.
-
dataset : tf.Dataset
A batched TF dataset containing the training dataset over which we will estimate the inverse-hessian-vector product.
-
ihvp_calculator : Union[str, deel.influenciae.common.inverse_hessian_vector_product.InverseHessianVectorProduct, deel.influenciae.common.inverse_hessian_vector_product.IHVPCalculator] = 'exact'
Either a string containing the IHVP method ('exact' or 'cgd'), an IHVPCalculator object or an InverseHessianVectorProduct object.
-
n_samples_for_hessian : Optional[int] = None
An integer indicating the amount of samples to take from the provided train dataset.
-
shuffle_buffer_size : Optional[int] = 10000
An integer indicating the buffer size of the train dataset's shuffle operation -- when choosing the amount of samples for the hessian.
-
normalize : normalize=False
Implement "RelatIF: Identifying Explanatory Training Examples via Relative Influence" https://arxiv.org/pdf/2003.11630.pdf if True, compute the relative influence by normalizing the influence function.
assert_compatible_datasets(dataset_a: tf.Dataset,
dataset_b: tf.Dataset) -> int
¶
dataset_b: tf.Dataset) -> int
Assert that the datasets are compatible: that they contain the same number of points. Else,
throw an error.
Parameters
-
dataset_a : tf.Dataset
First batched tensorflow dataset to check.
-
dataset_b : tf.Dataset
Second batched tensorflow dataset to check.
Return
-
size : int
The size of the dataset.
compute_influence_values(self,
train_set: tf.Dataset,
device: Optional[str] = None) -> tf.Dataset
¶
train_set: tf.Dataset,
device: Optional[str] = None) -> tf.Dataset
Compute the influence score for each sample of the provided (full or partial) model's training dataset.
Parameters
-
train_set : tf.Dataset
A TF dataset with the (full or partial) model's training dataset.
-
device : Optional[str] = None
Device where the computation will be executed
Return
-
train_set : tf.Dataset
A dataset containing the tuple: (batch of training samples, influence score)
compute_influence_vector(self,
train_set: tf.Dataset,
save_influence_vector_ds_path: Optional[str] = None,
device: Optional[str] = None) -> tf.Dataset
¶
train_set: tf.Dataset,
save_influence_vector_ds_path: Optional[str] = None,
device: Optional[str] = None) -> tf.Dataset
Compute the influence vector for each sample of the provided (full or partial) model's training dataset.
Parameters
-
train_set : tf.Dataset
A TF dataset with the (full or partial) model's training dataset.
-
save_influence_vector_ds_path : Optional[str] = None
The path to save or load the influence vector of the training dataset. If specified, load the dataset if it has already been computed, otherwise, compute the influence vector and then save it in the specified path.
-
device : Optional[str] = None
Device where the computation will be executed
Return
-
inf_vect_ds : tf.Dataset
A dataset containing the tuple: (batch of training samples, influence vector)
compute_influence_vector_group(self,
group: tf.Dataset) -> tf.Tensor
¶
group: tf.Dataset) -> tf.Tensor
Computes the influence function vector -- an estimation of the weights difference when
removing the points -- of the whole group of points.
Parameters
-
group : tf.Dataset
A batched TF dataset containing the group of points of which we wish to compute the influence of removal.
Return
-
influence_group : tf.Tensor
A tensor containing one vector for the whole group.
compute_top_k_from_training_dataset(self,
train_set: tf.Dataset,
k: int,
order: deel.influenciae.utils.sorted_dict.ORDER = ) -> Tuple[tf.Tensor, tf.Tensor]
¶
train_set: tf.Dataset,
k: int,
order: deel.influenciae.utils.sorted_dict.ORDER =
Compute the k most influential data-points of the model's training dataset by computing
Cook's distance for each point individually.
Parameters
-
train_set : tf.Dataset
A TF dataset containing the points on which the model was trained.
-
k : int
An integer with the number of most important samples we wish to keep
-
order : 2>
Either ORDER.DESCENDING or ORDER.ASCENDING depending on if we wish to find the top-k or bottom-k samples, respectively.
Return
-
training_samples, influences_values : Tuple[tf.Tensor, tf.Tensor]
A tuple of tensor.
- training_samples: A tensor containing the k most influential samples of the training dataset for the model provided.
- influences_values: The influence score corresponding to these k most influential samples.
estimate_influence_values_group(self,
group_train: tf.Dataset,
group_to_evaluate: Optional[tf.Dataset] = None) -> tf.Tensor
¶
group_train: tf.Dataset,
group_to_evaluate: Optional[tf.Dataset] = None) -> tf.Tensor
Computes Cook's distance of the whole group of points provided, giving measure of the
influence that the group carries on the model's weights.
Parameters
-
group_train : tf.Dataset
A batched TF dataset containing the group of points we wish to remove.
-
group_to_evaluate : Optional[tf.Dataset] = None
A batched TF dataset containing the group of points with respect to whom we wish to measure the influence of removing the training points.
Return
-
influence_values_group : tf.Tensor
A tensor containing one influence value for the whole group.
estimate_influence_values_in_batches(self,
dataset_to_evaluate: tf.Dataset,
train_set: tf.Dataset,
influence_vector_in_cache: deel.influenciae.common.base_influence.CACHE = ,
load_influence_vector_path: Optional[str] = None,
save_influence_vector_path: Optional[str] = None,
save_influence_value_path: Optional[str] = None,
device: Optional[str] = None) -> tf.Dataset
¶
dataset_to_evaluate: tf.Dataset,
train_set: tf.Dataset,
influence_vector_in_cache: deel.influenciae.common.base_influence.CACHE =
load_influence_vector_path: Optional[str] = None,
save_influence_vector_path: Optional[str] = None,
save_influence_value_path: Optional[str] = None,
device: Optional[str] = None) -> tf.Dataset
Estimates the influence that each point in the provided training dataset has on each of the test points.
This can provide some insights as to what makes the model predict a certain way for the given test points,
and thus presents data-centric explanations.
Parameters
-
dataset_to_evaluate : tf.Dataset
A TF dataset containing the test samples for which to compute the effect of removing each of the provided training points (individually).
-
train_set : tf.Dataset
A TF dataset containing the model's training dataset (partial or full).
-
influence_vector_in_cache : 0>
An enum indicating if intermediary values are to be cached (either in memory or on the disk) or not.
Options include CACHE.MEMORY (0) for caching in memory, CACHE.DISK (1) for the disk and CACHE.NO_CACHE (2) for no optimization.
-
load_influence_vector_path : Optional[str] = None
The path to load the influence vectors (if they have already been calculated).
-
save_influence_vector_path : Optional[str] = None
The path to save the computed influence vector.
-
save_influence_value_path : Optional[str] = None
The path to save the computed influence values.
-
device : Optional[str] = None
Device where the computation will be executed
Return
-
influence_value_dataset : tf.Dataset
A dataset containing the tuple: (samples_to_evaluate, dataset).
- samples_to_evaluate: The batch of sample to evaluate.
- dataset: Dataset containing tuples of batch of the training dataset and their influence score.
top_k(self,
dataset_to_evaluate: tf.Dataset,
train_set: tf.Dataset,
k: int = 5,
nearest_neighbors: deel.influenciae.utils.nearest_neighbors.BaseNearestNeighbors = ,
influence_vector_in_cache: deel.influenciae.common.base_influence.CACHE = ,
load_influence_vector_ds_path: Optional[str] = None,
save_influence_vector_ds_path: Optional[str] = None,
save_top_k_ds_path: Optional[str] = None,
order: deel.influenciae.utils.sorted_dict.ORDER = ,
d_type: tensorflow.python.framework.dtypes.DType = tf.float32,
device: Optional[str] = None) -> tf.Dataset
¶
dataset_to_evaluate: tf.Dataset,
train_set: tf.Dataset,
k: int = 5,
nearest_neighbors: deel.influenciae.utils.nearest_neighbors.BaseNearestNeighbors =
influence_vector_in_cache: deel.influenciae.common.base_influence.CACHE =
load_influence_vector_ds_path: Optional[str] = None,
save_influence_vector_ds_path: Optional[str] = None,
save_top_k_ds_path: Optional[str] = None,
order: deel.influenciae.utils.sorted_dict.ORDER =
d_type: tensorflow.python.framework.dtypes.DType = tf.float32,
device: Optional[str] = None) -> tf.Dataset
Find the top-k closest elements for each element of dataset to evaluate in the training dataset
The method will return a dataset containing a tuple of:
(Top-k influence values for each sample to evaluate, Top-k training sample for each sample to evaluate)
Parameters
-
dataset_to_evaluate : tf.Dataset
The dataset which contains the samples which will be compare to the training dataset
-
train_set : tf.Dataset
The dataset used to train the model.
-
k : int = 5
the number of most influence samples to retain in training dataset
-
nearest_neighbors : deel.influenciae.utils.nearest_neighbors.BaseNearestNeighbors =
The nearest neighbor method. The default method is a linear search
-
influence_vector_in_cache : 0>
An enum indicating if intermediary values are to be cached (either in memory or on the disk) or not.
Options include CACHE.MEMORY (0) for caching in memory, CACHE.DISK (1) for the disk and CACHE.NO_CACHE (2) for no optimization.
-
load_influence_vector_ds_path : Optional[str] = None
The path to load the influence vectors (if they have already been calculated).
-
save_influence_vector_ds_path : Optional[str] = None
The path to save the computed influence vector.
-
save_top_k_ds_path : Optional[str] = None
The path to save the result of the computation of the top-k elements
-
order : 2>
Either ORDER.DESCENDING or ORDER.ASCENDING depending on if we wish to find the top-k or bottom-k samples, respectively.
-
d_type : tensorflow.python.framework.dtypes.DType = tf.float32
The data-type of the tensors.
-
device : Optional[str] = None
Device where the computation will be executed
Return
-
top_k_dataset : tf.Dataset
A dataset containing the tuple (samples_to_evaluate, influence_values, training_samples).
- samples_to_evaluate: Top-k samples to evaluate.
- influence_values: Top-k influence values for each sample to evaluate.
- training_samples: Top-k training sample for each sample to evaluate.