title: Using annoy with Torchtext for nearest neighbor search
body: <p>I'm using Torchtext for some NLP tasks, specifically using the built-in embeddings.</p>
  
  <p>I want to be able to do a inverse vector search: Generate a noisy vector, find the vector that is closest to it, then get back the word that is "closest" to the noisy vector.</p>
  
  <p>From the <a href="https://pytorch.org/text/datasets.html" rel="nofollow noreferrer">torchtext docs</a>, here's how to attach embeddings to a built-in dataset:</p>
  
  <pre class="lang-py prettyprint-override"><code>from torchtext.vocab import GloVe
  from torchtext import data
  
  embedding = GloVe(name='6B', dim=100)
  
  # Set up fields
  TEXT = data.Field(lower=True, include_lengths=True, batch_first=True)
  LABEL = data.Field(sequential=False, is_target=True)
  
  # make splits for data
  train, test = datasets.IMDB.splits(TEXT, LABEL)
  
  # build the vocabulary
  TEXT.build_vocab(train, vectors=embedding, max_size=100000)
  LABEL.build_vocab(train)
  
  # Get an example vector
  embedding.get_vecs_by_tokens("germany")
  </code></pre>
  
  <p>Then we can build the annoy index:</p>
  
  <pre><code>from annoy import AnnoyIndex
  
  num_trees = 50
  
  ann_index = AnnoyIndex(embedding_dims, 'angular')
  
  # Iterate through each vector in the embedding and add it to the index
  for vector_num, vector in enumerate(TEXT.vocab.vectors):
      ann_index.add_item(vector_num, vector) # Here's the catch: will vector_num correspond to torchtext.vocab.Vocab.itos?
  
  ann_index.build(num_trees)
  </code></pre>
  
  <p>Then say I want to retrieve a word using a noisy vector:</p>
  
  <pre><code># Get an existing vector
  original_vec = embedding.get_vecs_by_tokens("germany")
  # Add some noise to it
  noise = generate_noise_vector(ndims=100)
  noisy_vector = original_vec + noise
  # Get the vector closest to the noisy vector
  closest_item_idx = ann_index.get_nns_by_vector(noisy_vector, 1)[0]
  # Get word from noisy item
  noisy_word = TEXT.vocab.itos[closest_item_idx]
  </code></pre>
  
  <p>My question comes in for the last two lines above: The <code>ann_index</code> was built using <code>enumerate</code> over the <code>embedding</code> object, which is a Torch tensor.</p>
  
  <p>The <code>[vocab][2]</code> object has its own <code>itos</code> list that given an index returns a word.</p>
  
  <p>My question is this: Can I be certain that the order in which words appear in the itos list, is the same as the order in <code>TEXT.vocab.vectors</code>? How can I map one index to the other?</p>