influence_interpreter
allennlp.interpret.influence_interpreters.influence_interpreter
InstanceInfluence¶
class InstanceInfluence(NamedTuple)
instance¶
class InstanceInfluence(NamedTuple):
| ...
| instance: Instance = None
loss¶
class InstanceInfluence(NamedTuple):
| ...
| loss: float = None
score¶
class InstanceInfluence(NamedTuple):
| ...
| score: float = None
The influence score associated with this training instance.
InterpretOutput¶
class InterpretOutput(NamedTuple)
The output associated with a single test instance.
test_instance¶
class InterpretOutput(NamedTuple):
| ...
| test_instance: Instance = None
loss¶
class InterpretOutput(NamedTuple):
| ...
| loss: float = None
The loss corresponding to the test_instance
.
top_k¶
class InterpretOutput(NamedTuple):
| ...
| top_k: List[InstanceInfluence] = None
The top k
most influential training instances along with their influence score.
InstanceWithGrads¶
class InstanceWithGrads(NamedTuple)
Wraps a training Instance
along with its associated loss and gradients.
InfluenceInterpreter.train_instances
is a list of these objects.
instance¶
class InstanceWithGrads(NamedTuple):
| ...
| instance: Instance = None
loss¶
class InstanceWithGrads(NamedTuple):
| ...
| loss: float = None
grads¶
class InstanceWithGrads(NamedTuple):
| ...
| grads: Sequence[torch.Tensor] = None
InfluenceInterpreter¶
class InfluenceInterpreter(Registrable):
| def __init__(
| self,
| model: Model,
| train_data_path: DatasetReaderInput,
| train_dataset_reader: DatasetReader,
| *, test_dataset_reader: Optional[DatasetReader] = None,
| *, train_data_loader: Lazy[DataLoader] = Lazy(SimpleDataLoader.from_dataset_reader),
| *, test_data_loader: Lazy[DataLoader] = Lazy(SimpleDataLoader.from_dataset_reader),
| *, params_to_freeze: Optional[List[str]] = None,
| *, cuda_device: int = -1
| ) -> None
An InfluenceInterpreter
interprets an AllenNLP models's outputs by finding the
training instances that had the most influence on the prediction for each test input.
See Understanding Black-box Predictions via Influence Functions for more information.
Subclasses are required to implement the _calculate_influence_scores()
method.
Parameters¶
-
model :
Model
-
train_data_path :
DatasetReaderInput
-
train_dataset_reader :
DatasetReader
-
test_dataset_reader :
Optional[DatasetReader]
, optional (default =None
)
This is the dataset reader to read the test set file. If not provided, thetrain_dataset_reader
is used. -
train_data_loader :
Lazy[DataLoader]
, optional (default =Lazy(SimpleDataLoader)
)
The data loader used to load training instances.Note
This data loader is only used to call
DataLoader.iter_instances()
, so certainDataLoader
settings likebatch_size
will have no effect. -
test_data_loader :
Lazy[DataLoader]
, optional (default =Lazy(SimpleDataLoader)
)
The data loader used to load test instances wheninterpret_from_file()
is called.Note
Like
train_data_loader
, this data loader is only used to callDataLoader.iter_instances()
, so certainDataLoader
settings likebatch_size
will have no effect. -
params_to_freeze :
Optional[List[str]]
, optional (default =None
)
An optional list of strings, each of which should be a regular expression that matches some parameter keys of the model. Any matching parameters will be haverequires_grad
set toFalse
. -
cuda_device :
int
, optional (default =-1
)
The index of GPU device we want to calculate scores on. If not provided, we uses-1
which correspond to using CPU.
default_implementation¶
class InfluenceInterpreter(Registrable):
| ...
| default_implementation = "simple-influence"
used_params¶
class InfluenceInterpreter(Registrable):
| ...
| @property
| def used_params(self) -> List[torch.nn.Parameter]
The parameters of the model that have non-zero gradients after a backwards pass.
This can be used to gather the corresponding gradients with respect to a loss
via the torch.autograd.grad
function.
Note
Accessing this property requires calling self._gather_train_instances_and_compute_gradients()
if it hasn't been called yet, which may take several minutes.
used_param_names¶
class InfluenceInterpreter(Registrable):
| ...
| @property
| def used_param_names(self) -> List[str]
The names of the corresponding parameters in self.used_params
.
Note
Accessing this property requires calling self._gather_train_instances_and_compute_gradients()
if it hasn't been called yet, which may take several minutes.
train_instances¶
class InfluenceInterpreter(Registrable):
| ...
| @property
| def train_instances(self) -> List[InstanceWithGrads]
The training instances along with their corresponding loss and gradients.
Note
Accessing this property requires calling self._gather_train_instances_and_compute_gradients()
if it hasn't been called yet, which may take several minutes.
from_path¶
class InfluenceInterpreter(Registrable):
| ...
| @classmethod
| def from_path(
| cls,
| archive_path: Union[str, PathLike],
| *, interpreter_name: Optional[str] = None,
| *, train_data_path: Optional[DatasetReaderInput] = None,
| *, train_data_loader: Lazy[DataLoader] = Lazy(SimpleDataLoader.from_dataset_reader),
| *, test_data_loader: Lazy[DataLoader] = Lazy(SimpleDataLoader.from_dataset_reader),
| *, params_to_freeze: Optional[List[str]] = None,
| *, cuda_device: int = -1,
| *, import_plugins: bool = True,
| *, overrides: Union[str, Dict[str, Any]] = "",
| **extras,
| *, ,
| ) -> "InfluenceInterpreter"
Load an InfluenceInterpreter
from an archive path.
Parameters¶
- archive_path :
Union[str, PathLike]
The path to the archive file. - interpreter_name :
Optional[str]
, optional (default =None
)
The registered name of the an interpreter class. If not specified, the default implementation (SimpleInfluence
) will be used. - train_data_path :
Optional[DatasetReaderInput]
, optional (default =None
)
If not specified,train_data_path
will be taken from the archive's config. - train_data_loader :
Lazy[DataLoader]
, optional (default =Lazy(SimpleDataLoader)
) - test_data_loader :
Lazy[DataLoader]
, optional (default =Lazy(SimpleDataLoader)
) - params_to_freeze :
Optional[List[str]]
, optional (default =None
) - cuda_device :
int
, optional (default =-1
) - import_plugins :
bool
, optional (default =True
)
IfTrue
, we attempt to import plugins before loading theInfluenceInterpreter
. This comes with additional overhead, but means you don't need to explicitly import the modules that your implementation depends on as long as those modules can be found byallennlp.common.plugins.import_plugins()
. - overrides :
Union[str, Dict[str, Any]]
, optional (default =""
)
JSON overrides to apply to the unarchivedParams
object. - **extras :
Any
Extra parameters to pass to the interpreter's__init__()
method.
from_archive¶
class InfluenceInterpreter(Registrable):
| ...
| @classmethod
| def from_archive(
| cls,
| archive: Archive,
| *, interpreter_name: Optional[str] = None,
| *, train_data_path: Optional[DatasetReaderInput] = None,
| *, train_data_loader: Lazy[DataLoader] = Lazy(SimpleDataLoader.from_dataset_reader),
| *, test_data_loader: Lazy[DataLoader] = Lazy(SimpleDataLoader.from_dataset_reader),
| *, params_to_freeze: Optional[List[str]] = None,
| *, cuda_device: int = -1,
| **extras,
| *, ,
| ) -> "InfluenceInterpreter"
Load an InfluenceInterpreter
from an Archive
.
The other parameters are the same as .from_path()
.
interpret¶
class InfluenceInterpreter(Registrable):
| ...
| def interpret(
| self,
| test_instance: Instance,
| k: int = 20
| ) -> InterpretOutput
Run the influence function scorer on the given instance, returning the top k
most influential train instances with their scores.
Note
Test instances should have targets
so that a loss can be computed.
interpret_from_file¶
class InfluenceInterpreter(Registrable):
| ...
| def interpret_from_file(
| self,
| test_data_path: DatasetReaderInput,
| k: int = 20
| ) -> List[InterpretOutput]
Runs interpret_instances
over the instances read from test_data_path
.
Note
Test instances should have targets
so that a loss can be computed.
interpret_instances¶
class InfluenceInterpreter(Registrable):
| ...
| def interpret_instances(
| self,
| test_instances: List[Instance],
| k: int = 20
| ) -> List[InterpretOutput]
Run the influence function scorer on the given instances, returning the top k
most influential train instances for each test instance.
Note
Test instances should have targets
so that a loss can be computed.
_calculate_influence_scores¶
class InfluenceInterpreter(Registrable):
| ...
| def _calculate_influence_scores(
| self,
| test_instance: Instance,
| test_loss: float,
| test_grads: Sequence[torch.Tensor]
| ) -> List[float]
Required to be implemented by subclasses.
Calculates the influence scores of self.train_instances
with respect to
the given test_instance
.