docstr: This test uses a linear model with positive coefficients, where input feature
  matrix is the identity matrix.  Since the dot product between 2 different
  training instances is always 0, when calculating influence scores on the
  training data, only self influence scores will be nonzero.  Since the linear
  model has positive coefficients, self influence scores will be positive.
  Thus, the training instance with the largest influence on another training
  instance is itself.
docstr_tok: ["This","test","uses","a","linear","model","with","positive","coefficients",",","where","input","feature","matrix","is","the","identity","matrix",".","Since","the","dot","product","between","2","different","training","instances","is","always","0",",","when","calculating","influence","scores","on","the","training","data",",","only","self","influence","scores","will","be","nonzero",".","Since","the","linear","model","has","positive","coefficients",",","self","influence","scores","will","be","positive",".","Thus",",","the","training","instance","with","the","largest","influence","on","another","training","instance","is","itself","."]
code: def test_tracin_identity_regression(
      self,
      mode: str,
      reduction: Optional[str],
      tracin_constructor: Callable,
  ) -> None:
      """
      This test uses a linear model with positive coefficients, where input feature
      matrix is the identity matrix.  Since the dot product between 2 different
      training instances is always 0, when calculating influence scores on the
      training data, only self influence scores will be nonzero.  Since the linear
      model has positive coefficients, self influence scores will be positive.
      Thus, the training instance with the largest influence on another training
      instance is itself.
      """
  
      with tempfile.TemporaryDirectory() as tmpdir:
          batch_size = 4
  
          dataset, net = self._test_tracin_identity_regression_setup(
              tmpdir
          )
  
          train_inputs = dataset.samples
          train_labels = dataset.labels
  
          self.assertTrue(callable(tracin_constructor))
  
          if mode == "check_idx":
              self.assertTrue(isinstance(reduction, str))
              criterion = nn.MSELoss(reduction=cast(str, reduction))
  
              tracin = tracin_constructor(
                  net,
                  dataset,
                  tmpdir,
                  batch_size,
                  criterion,
              )
  
              # check influence scores of training data
  
              train_scores = tracin.influence(
                  (train_inputs, train_labels)
              )
              idx, _ = tracin.influence(
                  (train_inputs, train_labels),
                  k=len(dataset),
                  proponents=True,
              )
  
              # check that top influence for an instance is itself
              for i in range(len(idx)):
                  self.assertEqual(idx[i][0], i)
  
          if mode == "sample_wise_trick":
              criterion = nn.MSELoss(reduction="none")
  
              tracin = tracin_constructor(
                  net,
                  dataset,
                  tmpdir,
                  batch_size,
                  criterion,
                  sample_wise_grads_per_batch=False,
              )
  
              # With sample-wise trick
              criterion = nn.MSELoss(reduction="sum")
              tracin_sample_wise_trick = tracin_constructor(
                  net,
                  dataset,
                  tmpdir,
                  batch_size,
                  criterion,
                  sample_wise_grads_per_batch=True,
              )
  
              train_scores = tracin.influence(
                  (train_inputs, train_labels)
              )
              train_scores_tracin_sample_wise_trick = (
                  tracin_sample_wise_trick.influence(
                      (train_inputs, train_labels)
                  )
              )
              assertTensorAlmostEqual(
                  self,
                  train_scores,
                  train_scores_tracin_sample_wise_trick,
              )
  
file_path: captum/tests/influence/_core/test_tracin_regression.py
imports: import os
  import tempfile
  from typing import Callable, cast, Optional
  import torch
  import torch.nn
  from captum.influence._core.arnoldi_influence_function import ArnoldiInfluenceFunction
  from captum.influence._core.influence_function import NaiveInfluenceFunction
  from captum.influence._core.tracincp import TracInCP
  from captum.influence._core.tracincp_fast_rand_proj import TracInCPFast, TracInCPFastRandProj
  from parameterized import parameterized
  from tests.helpers.basic import assertTensorAlmostEqual, BaseTest
  from tests.influence._utils.common import _isSorted, _wrap_model_in_dataparallel, build_test_name_func, CoefficientNet, DataInfluenceConstructor, IdentityDataset, RangeDataset
label_desc: Use image node_id 5 for calling the TestTracInRegression obj's underlying member method code with example usage: obj.test_tracin_identity_regression(mode, reduction, tracin_constructor) without return types