docstr: All implementations of `TracInCPBase` can accept `test_loss_fn` in
  initialization, which sets the loss function applied to test examples, which
  can thus be different from the loss function applied to training examples.
  This test passes `test_loss_fn` to be a constant function. Then, the influence
  scores should all be 0, because gradients w.r.t. `test_loss_fn` will all be 0.
  It re-uses the dataset and model from `test_tracin_identity_regression`.
  
  The reduction for `loss_fn` and `test_loss_fn` initialization arguments is
  the same for all parameterized tests, for simplicity, and also because for
  `TracInCP`, both loss functions must both be reduction loss functions (i.e.
  reduction is "mean" or "sum"), or both be per-example loss functions (i.e.
  reduction is "none"). Recall that for `TracInCP`, the
  `sample_wise_grads_per_batch` initialization argument determines which of
  those cases holds.
docstr_tok: ["All","implementations","of","`","TracInCPBase","`","can","accept","`","test_loss_fn","`","in","initialization",",","which","sets","the","loss","function","applied","to","test","examples",",","which","can","thus","be","different","from","the","loss","function","applied","to","training","examples",".","This","test","passes","`","test_loss_fn","`","to","be","a","constant","function",".","Then",",","the","influence","scores","should","all","be","0",",","because","gradients","w.r.t",".","`","test_loss_fn","`","will","all","be","0",".","It","re-uses","the","dataset","and","model","from","`","test_tracin_identity_regression","`",".","The","reduction","for","`","loss_fn","`","and","`","test_loss_fn","`","initialization","arguments","is","the","same","for","all","parameterized","tests",",","for","simplicity",",","and","also","because","for","`","TracInCP","`",",","both","loss","functions","must","both","be","reduction","loss","functions","(","i.e",".","reduction","is","``","mean","''","or","``","sum","''",")",",","or","both","be","per-example","loss","functions","(","i.e",".","reduction","is","``","none","''",")",".","Recall","that","for","`","TracInCP","`",",","the","`","sample_wise_grads_per_batch","`","initialization","argument","determines","which","of","those","cases","holds","."]
code: def test_tracin_constant_test_loss_fn(
      self,
      reduction: Optional[str],
      test_reduction: Optional[str],
      tracin_constructor: Callable,
  ) -> None:
      """
      All implementations of `TracInCPBase` can accept `test_loss_fn` in
      initialization, which sets the loss function applied to test examples, which
      can thus be different from the loss function applied to training examples.
      This test passes `test_loss_fn` to be a constant function. Then, the influence
      scores should all be 0, because gradients w.r.t. `test_loss_fn` will all be 0.
      It re-uses the dataset and model from `test_tracin_identity_regression`.
  
      The reduction for `loss_fn` and `test_loss_fn` initialization arguments is
      the same for all parameterized tests, for simplicity, and also because for
      `TracInCP`, both loss functions must both be reduction loss functions (i.e.
      reduction is "mean" or "sum"), or both be per-example loss functions (i.e.
      reduction is "none"). Recall that for `TracInCP`, the
      `sample_wise_grads_per_batch` initialization argument determines which of
      those cases holds.
      """
      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))
  
          self.assertTrue(isinstance(reduction, str))
          criterion = nn.MSELoss(reduction=cast(str, reduction))
  
          # the output of `net`, i.e. `input` for the loss functions below, is a
          # batch_size x 1 2D tensor
          if test_reduction == "none":
              # loss function returns 1D tensor of all 0's, so is constant
              def test_loss_fn(input, target):
                  return input.squeeze() * 0.0
  
          elif test_reduction in ["sum", "mean"]:
              # loss function returns scalar tensor of all 0's, so is constant
              def test_loss_fn(input, target):
                  return input.mean() * 0.0
  
          tracin = tracin_constructor(
              net,
              dataset,
              tmpdir,
              batch_size,
              criterion,
              test_loss_fn=test_loss_fn,
          )
  
          # check influence scores of training data. they should all be 0
          train_scores = tracin.influence(
              (train_inputs, train_labels), k=None
          )
          assertTensorAlmostEqual(
              self, train_scores, torch.zeros(train_scores.shape)
          )
  
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 6 for calling the TestTracInRegression obj's underlying member method code with example usage: obj.test_tracin_constant_test_loss_fn(reduction, test_reduction, tracin_constructor) and returns: unknown, unknown