class_docstr: This is an implementation of `NearestNeighbors` that uses the Annoy module. At a
  high level, Annoy finds nearest neighbors by constructing binary trees in which
  vectors reside at leaf nodes. Vectors near each other will tend to be in the same
  leaf node. See https://tinyurl.com/2p89sb2h and https://github.com/spotify/annoy
  for more details. Annoy has 1 key parameter: the number of trees to construct.
  Increasing the number of trees leads to more accurate results, but longer time to
  create the trees and memory usage. As mentioned in the `NearestNeighbors`
  documentation, for the use case of computing proponents / opponents, the nearest
  neighbors returned should be those with the largest dot product with the query
  vector. The term "vector" is used here because Annoy stores 1D vectors. However
  in our wrapper around Annoy, we will allow the stored tensors to be of a common
  but arbitrary shape *, and flatten them before storing in the Annoy data structure.
class_docstr_tok: ["This","is","an","implementation","of","`","NearestNeighbors","`","that","uses","the","Annoy","module",".","At","a","high","level",",","Annoy","finds","nearest","neighbors","by","constructing","binary","trees","in","which","vectors","reside","at","leaf","nodes",".","Vectors","near","each","other","will","tend","to","be","in","the","same","leaf","node",".","See","https",":","\/\/tinyurl.com\/2p89sb2h","and","https",":","\/\/github.com\/spotify\/annoy","for","more","details",".","Annoy","has","1","key","parameter",":","the","number","of","trees","to","construct",".","Increasing","the","number","of","trees","leads","to","more","accurate","results",",","but","longer","time","to","create","the","trees","and","memory","usage",".","As","mentioned","in","the","`","NearestNeighbors","`","documentation",",","for","the","use","case","of","computing","proponents","\/","opponents",",","the","nearest","neighbors","returned","should","be","those","with","the","largest","dot","product","with","the","query","vector",".","The","term","``","vector","''","is","used","here","because","Annoy","stores","1D","vectors",".","However","in","our","wrapper","around","Annoy",",","we","will","allow","the","stored","tensors","to","be","of","a","common","but","arbitrary","shape","*",",","and","flatten","them","before","storing","in","the","Annoy","data","structure","."]
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. This implementation returns the stored tensors
  that have the largest dot-product with the query tensor, and does not constrain
  the tensors in `query` or in the stored tensors to be 1D. If tensors are of
  dimension greater than 1D, their dot-product will be defined to be the
  dot-product of the flattened version of tensors.
  
  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",".","This","implementation","returns","the","stored","tensors","that","have","the","largest","dot-product","with","the","query","tensor",",","and","does","not","constrain","the","tensors","in","`","query","`","or","in","the","stored","tensors","to","be","1D",".","If","tensors","are","of","dimension","greater","than","1D",",","their","dot-product","will","be","defined","to","be","the","dot-product","of","the","flattened","version","of","tensors",".","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. This implementation returns the stored tensors
      that have the largest dot-product with the query tensor, and does not constrain
      the tensors in `query` or in the stored tensors to be 1D. If tensors are of
      dimension greater than 1D, their dot-product will be defined to be the
      dot-product of the flattened version of tensors.
  
      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.
      """
      query = query.view((len(query), -1))
      indices_and_distances = [
          self.knn_index.get_nns_by_vector(
              instance, k, include_distances=True
          )
          for instance in query
      ]
      indices, distances = zip(*indices_and_distances)
      indices = torch.Tensor(indices).type(torch.long)
      distances = torch.Tensor(distances)
      return indices, distances
  
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 3 for calling the AnnoyNearestNeighbors obj's underlying member method code with example usage: obj.get_nearest_neighbors(query, k) and returns: indices,  distances