class_docstr: An abstract class to define a nearest neighbors data structure. Classes
  implementing this interface are intended for computing proponents / opponents in
  certain implementations of `TracInCPBase`. In particular, it is for use in
  implementations which compute proponents / opponents of a test instance by
  1) storing representations of training instances within a nearest neighbors data
  structure, and 2) finding within that structure the nearest neighbor of the
  representation of a test instance. The assumption is that the data structure
  stores the tensors passed to the `setup` method, which we refer to as the "stored
  tensors". If this class is used to find proponents / opponents, the nearest
  neighbors of a tensor should be the stored tensors that have the largest
  dot-product with the query.
class_docstr_tok: ["An","abstract","class","to","define","a","nearest","neighbors","data","structure",".","Classes","implementing","this","interface","are","intended","for","computing","proponents","\/","opponents","in","certain","implementations","of","`","TracInCPBase","`",".","In","particular",",","it","is","for","use","in","implementations","which","compute","proponents","\/","opponents","of","a","test","instance","by","1",")","storing","representations","of","training","instances","within","a","nearest","neighbors","data","structure",",","and","2",")","finding","within","that","structure","the","nearest","neighbor","of","the","representation","of","a","test","instance",".","The","assumption","is","that","the","data","structure","stores","the","tensors","passed","to","the","`","setup","`","method",",","which","we","refer","to","as","the","``","stored","tensors","''",".","If","this","class","is","used","to","find","proponents","\/","opponents",",","the","nearest","neighbors","of","a","tensor","should","be","the","stored","tensors","that","have","the","largest","dot-product","with","the","query","."]
docstr: Given a `query`, a tensor of shape (N, *), returns the nearest neighbors in the
  "stored tensors" (see above). `query` represents a batch of N tensors, each
  of common but arbitrary shape *. We always assume the 0-th dimension indexes
  the batch. In use cases of this class for computing proponents / opponents,
  the nearest neighbors of a tensor should be the stored tensors with the largest
  dot-product with the tensor, and the tensors in `query` will all be 1D,
  so that `query` is 2D.
  
  Args:
      query (Tensor): tensor representing the batch of tensors for which k-nearest
              neighbors are desired. `query` is of shape (N, *), where N is the
              size of the batch, i.e. the 0-th dimension of `query` indexes the
              batch. * denotes an arbitrary shape, so that each tensor in the
              batch can be of a common, but arbitrary shape.
      k (int): The number of nearest neighbors to return.
  
  Returns:
      results (tuple): A tuple of `(indices, distances)` is returned. `indices`
              is a 2D tensor where `indices[i,j]` is the index (within the
              "stored tensors" passed to the `setup` method) of the `j`-th
              nearest neighbor of the `i`-th instance in query, and
              `distances[i,j]` is the corresponding distance. `indices` should
              be of dtype `torch.long` so that it can be used to index torch
              tensors.
docstr_tok: ["Given","a","`","query","`",",","a","tensor","of","shape","(","N",",","*",")",",","returns","the","nearest","neighbors","in","the","''","stored","tensors","''","(","see","above",")",".","`","query","`","represents","a","batch","of","N","tensors",",","each","of","common","but","arbitrary","shape","*",".","We","always","assume","the","0-th","dimension","indexes","the","batch",".","In","use","cases","of","this","class","for","computing","proponents","\/","opponents",",","the","nearest","neighbors","of","a","tensor","should","be","the","stored","tensors","with","the","largest","dot-product","with","the","tensor",",","and","the","tensors","in","`","query","`","will","all","be","1D",",","so","that","`","query","`","is","2D",".","Args",":","query","(","Tensor",")",":","tensor","representing","the","batch","of","tensors","for","which","k-nearest","neighbors","are","desired",".","`","query","`","is","of","shape","(","N",",","*",")",",","where","N","is","the","size","of","the","batch",",","i.e",".","the","0-th","dimension","of","`","query","`","indexes","the","batch",".","*","denotes","an","arbitrary","shape",",","so","that","each","tensor","in","the","batch","can","be","of","a","common",",","but","arbitrary","shape",".","k","(","int",")",":","The","number","of","nearest","neighbors","to","return",".","Returns",":","results","(","tuple",")",":","A","tuple","of","`","(","indices",",","distances",")","`","is","returned",".","`","indices","`","is","a","2D","tensor","where","`","indices","[","i",",","j","]","`","is","the","index","(","within","the","``","stored","tensors","''","passed","to","the","`","setup","`","method",")","of","the","`","j","`","-th","nearest","neighbor","of","the","`","i","`","-th","instance","in","query",",","and","`","distances","[","i",",","j","]","`","is","the","corresponding","distance",".","`","indices","`","should","be","of","dtype","`","torch.long","`","so","that","it","can","be","used","to","index","torch","tensors","."]
code: def get_nearest_neighbors(
      self, query: torch.Tensor, k: int
  ) -> Tuple[Tensor, Tensor]:
      r"""
      Given a `query`, a tensor of shape (N, *), returns the nearest neighbors in the
      "stored tensors" (see above). `query` represents a batch of N tensors, each
      of common but arbitrary shape *. We always assume the 0-th dimension indexes
      the batch. In use cases of this class for computing proponents / opponents,
      the nearest neighbors of a tensor should be the stored tensors with the largest
      dot-product with the tensor, and the tensors in `query` will all be 1D,
      so that `query` is 2D.
  
      Args:
          query (Tensor): tensor representing the batch of tensors for which k-nearest
                  neighbors are desired. `query` is of shape (N, *), where N is the
                  size of the batch, i.e. the 0-th dimension of `query` indexes the
                  batch. * denotes an arbitrary shape, so that each tensor in the
                  batch can be of a common, but arbitrary shape.
          k (int): The number of nearest neighbors to return.
  
      Returns:
          results (tuple): A tuple of `(indices, distances)` is returned. `indices`
                  is a 2D tensor where `indices[i,j]` is the index (within the
                  "stored tensors" passed to the `setup` method) of the `j`-th
                  nearest neighbor of the `i`-th instance in query, and
                  `distances[i,j]` is the corresponding distance. `indices` should
                  be of dtype `torch.long` so that it can be used to index torch
                  tensors.
      """
      pass
  
file_path: captum/captum/influence/_utils/nearest_neighbors.py
imports: from abc import ABC, abstractmethod
  from typing import Tuple
  import torch
  from torch import Tensor
label_desc: Use image node_id 1 for calling the NearestNeighbors obj's underlying member method code with example usage: obj.get_nearest_neighbors(query, k) without return types