docstr: Since the influence score of a test batch on a training data should be the dot
  product of their intermediate quantities, checks that this is the case, by
  computing the influence score 2 different ways and checking they give the same
  results: 1) with the `influence` method, and by using the
  `compute_intermediate_quantities` method on the test and training data, and
  taking the dot product. No projection should be done.  Otherwise, the
  projection will cause error. For 1), we use an implementation that does not use
  intermediate quantities, i.e. `TracInCPFast`.  For 2), we use a method that
  does use intermediate quantities, i.e. `TracInCPFastRandProj`. Since the
  methods for the 2 cases are different, we need to parametrize the test with 2
  different tracin constructors. `tracin_constructor` is the constructor for the
  tracin implementation for case 1.  `intermediate_quantities_tracin_constructor`
  is the constructor for the tracin implementation for case 2. Note that we also
  use this test for implementations of `InfluenceFunctionBase`, where for the
  same method, both ways should give the same result by definition.
docstr_tok: ["Since","the","influence","score","of","a","test","batch","on","a","training","data","should","be","the","dot","product","of","their","intermediate","quantities",",","checks","that","this","is","the","case",",","by","computing","the","influence","score","2","different","ways","and","checking","they","give","the","same","results",":","1",")","with","the","`","influence","`","method",",","and","by","using","the","`","compute_intermediate_quantities","`","method","on","the","test","and","training","data",",","and","taking","the","dot","product",".","No","projection","should","be","done",".","Otherwise",",","the","projection","will","cause","error",".","For","1",")",",","we","use","an","implementation","that","does","not","use","intermediate","quantities",",","i.e",".","`","TracInCPFast","`",".","For","2",")",",","we","use","a","method","that","does","use","intermediate","quantities",",","i.e",".","`","TracInCPFastRandProj","`",".","Since","the","methods","for","the","2","cases","are","different",",","we","need","to","parametrize","the","test","with","2","different","tracin","constructors",".","`","tracin_constructor","`","is","the","constructor","for","the","tracin","implementation","for","case","1",".","`","intermediate_quantities_tracin_constructor","`","is","the","constructor","for","the","tracin","implementation","for","case","2",".","Note","that","we","also","use","this","test","for","implementations","of","`","InfluenceFunctionBase","`",",","where","for","the","same","method",",","both","ways","should","give","the","same","result","by","definition","."]
code: def test_tracin_intermediate_quantities_consistent(
      self,
      reduction: str,
      tracin_constructor: Callable,
      intermediate_quantities_tracin_constructor: Callable,
      unpack_inputs: bool,
  ) -> None:
      """
      Since the influence score of a test batch on a training data should be the dot
      product of their intermediate quantities, checks that this is the case, by
      computing the influence score 2 different ways and checking they give the same
      results: 1) with the `influence` method, and by using the
      `compute_intermediate_quantities` method on the test and training data, and
      taking the dot product. No projection should be done.  Otherwise, the
      projection will cause error. For 1), we use an implementation that does not use
      intermediate quantities, i.e. `TracInCPFast`.  For 2), we use a method that
      does use intermediate quantities, i.e. `TracInCPFastRandProj`. Since the
      methods for the 2 cases are different, we need to parametrize the test with 2
      different tracin constructors. `tracin_constructor` is the constructor for the
      tracin implementation for case 1.  `intermediate_quantities_tracin_constructor`
      is the constructor for the tracin implementation for case 2. Note that we also
      use this test for implementations of `InfluenceFunctionBase`, where for the
      same method, both ways should give the same result by definition.
      """
      with tempfile.TemporaryDirectory() as tmpdir:
          (
              net,
              train_dataset,
              test_features,
              test_labels,
          ) = get_random_model_and_data(
              tmpdir, unpack_inputs, return_test_data=True
          )
  
          # create a dataloader that yields batches from the dataset
          train_dataset = DataLoader(train_dataset, batch_size=5)
  
          # create tracin instance
          criterion = nn.MSELoss(reduction=reduction)
          batch_size = 5
  
          tracin = tracin_constructor(
              net,
              train_dataset,
              tmpdir,
              batch_size,
              criterion,
          )
  
          # create tracin instance which exposes `intermediate_quantities`
          intermediate_quantities_tracin = (
              intermediate_quantities_tracin_constructor(
                  net,
                  train_dataset,
                  tmpdir,
                  batch_size,
                  criterion,
              )
          )
  
          # compute influence scores without using `compute_intermediate_quantities`
          test_batch = _format_batch_into_tuple(
              test_features, test_labels, unpack_inputs
          )
          scores = tracin.influence(
              test_batch,
          )
  
          # the influence score is the dot product of intermediate quantities
          intermediate_quantities_scores = torch.matmul(
              intermediate_quantities_tracin.compute_intermediate_quantities(
                  test_batch
              ),
              intermediate_quantities_tracin.compute_intermediate_quantities(
                  train_dataset
              ).T,
          )
  
          # the scores computed using the two methods should be the same
          assertTensorAlmostEqual(
              self,
              scores,
              intermediate_quantities_scores,
              delta=0.01,  # due to numerical issues, we can't set this to 0.0
              mode="max",
          )
  
file_path: captum/tests/influence/_core/test_tracin_intermediate_quantities.py
imports: import tempfile
  from typing import Callable
  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 _format_batch_into_tuple, build_test_name_func, DataInfluenceConstructor, get_random_model_and_data
  from torch.utils.data import DataLoader
label_desc: Use image node_id 3 for calling the TestTracInIntermediateQuantities obj's underlying member method code with example usage: obj.test_tracin_intermediate_quantities_consistent(reduction, tracin_constructor, intermediate_quantities_tracin_constructor, unpack_inputs) without return types