tensorflow - KeyError in nearest

Created on 10 Jun 2016  路  3Comments  路  Source: tensorflow/tensorflow

when i am running this code on small corpus i am getting key error because that key is not in dict and corpus vocab is also not that huge.

sim = similarity.eval()
for i in xrange(valid_size):
                valid_word = reverse_dictionary[valid_examples[i]]
                print("--",valid_word)
                top_k = 5 # number of nearest neighbors

                nearest = (-sim[i, :]).argsort()[1:top_k+1]
                print(nearest)
                log_str = "Nearest to %s:" % valid_word
                print(log_str)
                for k in xrange(top_k):

                  close_word = reverse_dictionary[nearest[k]]

My output are like this:

Average loss at step  0 :  139.830688477
[[ 0.01613899 -0.06088334 -0.043384   ...,  0.02021606 -0.10094199
   0.16063547]
 [ 1.00000012  0.10277888 -0.20193034 ..., -0.04780241  0.07802841
   0.13258868]
 [ 0.09824251 -0.17075592  0.10143445 ...,  0.09903113 -0.08740355
  -0.00371696]
 ..., 
 [-0.01591019  0.02056946  0.09188825 ..., -0.0506176   0.07684846
   0.06354721]
 [-0.06749535  0.0028128  -0.09138335 ...,  0.09473826  0.04847325
  -0.00853895]
 [ 0.01795161  0.01850585  0.04632751 ...,  0.11854959  0.11196665
  -0.00684015]]
16
[-0.01613899  0.06088334  0.043384   ..., -0.02021606  0.10094199
 -0.16063547]
<type 'numpy.ndarray'>
[ 31 113 118 ..., 650 353 233]
-- using
[113 118 555 298 150]
Nearest to using:
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-129-cf006e08ddb8> in <module>()
     87                 for k in xrange(top_k):
     88 
---> 89                   close_word = reverse_dictionary[nearest[k]]
     90                   log_str = "%s %s," % (log_str, close_word)
     91                 print(log_str)

KeyError: 555

vocab_length = 1155
batch_size = 16
embedding_size = 128
skip_window = 5
num_skips = 4

valid_size = 16
valid_window = 100
valid_examples = np.random.choice(valid_window, valid_size, replace=False)
num_sampled =64

Can anybody help please?

awaiting response

Most helpful comment

Hi Rahul,

I was having the exact same problem as you until I checked the size of the "reverse_dictionary" array using "print(len(reverse_dictionary))" just before the error occurred.

Your "vocabulary_size = 50000" line should be set lower. I set it to the value returned by printing the length of "reverse_dictionary" and no longer had any issues.

I hope this helps.

All 3 comments

Hi Rahul,

I was having the exact same problem as you until I checked the size of the "reverse_dictionary" array using "print(len(reverse_dictionary))" just before the error occurred.

Your "vocabulary_size = 50000" line should be set lower. I set it to the value returned by printing the length of "reverse_dictionary" and no longer had any issues.

I hope this helps.

that helped a lot!!!!!! @tcheightyeight

Automatically closing due to lack of recent activity. Please reopen when further information becomes available.

Was this page helpful?
0 / 5 - 0 ratings