    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.
        """
        # We have these checks here for two reasons:
        #  1. as a sanity check to make sure we actually have a non-empty set of training
        #     instances as well as parameters that get non-zero gradients,
        #  2. and to ensure these attributes (self.used_params and self.training_instances)
        #     have been collected before proceeding further.
        if not self.train_instances:
            raise ValueError(f"No training instances collected from {self._train_data_path}")
        if not self.used_params:
            raise ValueError("Model has no parameters with non-zero gradients")

        outputs: List[InterpretOutput] = []
        for test_idx, test_instance in enumerate(Tqdm.tqdm(test_instances, desc="test instances")):
            test_batch = Batch([test_instance])
            test_batch.index_instances(self.vocab)
            test_tensor_dict = move_to_device(test_batch.as_tensor_dict(), self.device)

            # Prepare model for loss and gradient calculations.
            self.model.eval()
            self.model.zero_grad()

            # Compute loss with respect to the test instance.
            test_output_dict = self.model(**test_tensor_dict)
            test_loss = test_output_dict["loss"]
            test_loss_float = test_loss.detach().item()

            # Get the (parameter) gradients with respect to the test loss.
            test_grads = autograd.grad(test_loss, self.used_params)
            # Sanity check.
            assert len(test_grads) == len(self.used_params)

            # Get influence scores.
            influence_scores = torch.zeros(len(self.train_instances))
            for idx, score in enumerate(
                self._calculate_influence_scores(test_instance, test_loss_float, test_grads)
            ):
                influence_scores[idx] = score

            # Gather top k.
            top_k_scores, top_k_indices = torch.topk(influence_scores, k)
            top_k = self._gather_instances(top_k_scores, top_k_indices)

            outputs.append(
                InterpretOutput(
                    test_instance=test_instance,
                    loss=test_loss_float,
                    top_k=top_k,
                )
            )
        return outputs
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.
allennlp/interpret/influence_interpreters/influence_interpreter.py