IndexError - Implementing the test of CBOW with pytorch
I am not very used to python &amp; machine learning code. I am testing on pytorch CBOW test, but it says something about Index Error. Can anyone help?

# model class
class CBOW(nn.Module):

    ...
    def get_word_embedding(self, word):
    word = torch.cuda.LongTensor([word_to_ix[word]])
    return self.embeddings(word).view(1, -1)

# test method
def test_cbow(model, train_words, word_to_ix):
    # test word similarity
    word_1 = train_words[2] #randomly chosen word
    word_2 = train_words[3] #randomly chosen word

    word_1_vec = model.get_word_embedding(word_1)[0].cpu()
    word_2_vec = model.get_word_embedding(word_2)[0].cpu()

    print(word_1_vec)
    print(word_2_vec)


    word_similarity = (word_1_vec.dot(word_2_vec) / (torch.norm(word_1_vec) * torch.norm(word_2_vec))).data.numpy()[0]
    print("Similarity between '{}' &amp; '{}' : {:0.4f}".format(word_1, word_2, word_similarity))

# executing the test
test_cbow(model, train_words, word_to_ix)


BELOW IS THE RESULT:

tensor([ 0.8978,  1.0713, -1.6856, -1.0967, -0.0114,  0.4107, -0.4293, -0.7351,
         0.4410, -1.5937, -1.3773,  0.7744,  0.0739, -0.3263,  1.0342,  1.0420,
        -1.1333,  0.4158,  1.1316, -0.0141, -0.8383,  0.2544, -2.2409, -1.1858,
         0.2652, -0.3232,  0.1287, -1.5274,  0.3199, -2.1822,  0.9464, -0.6619,
         1.1549,  0.5276,  0.0849, -0.1594, -1.7922,  1.3567, -0.4376, -0.9093,
         1.0701,  1.5373, -1.3277, -1.1833,  1.8070, -0.0551, -0.8439,  1.5236,
        -0.3890, -0.2306, -0.7392, -1.6435,  0.4485,  0.8988, -0.5958, -0.6989,
         1.6123, -1.6668,  0.0583,  0.6698, -0.6998,  1.1942,  0.6355,  0.7437,
        -1.0006, -0.5398,  1.3197,  1.3696, -0.3221,  0.9004,  0.6268,  0.0221,
         0.0269, -1.7966, -1.6153, -0.1695, -0.0339, -0.5145,  1.5744, -0.3388,
        -0.9617,  0.6750, -1.1334,  0.0377,  1.1123,  1.1002, -0.3605,  0.2105,
        -1.6570,  1.3818,  0.9183,  0.0274,  0.9072,  0.8414,  0.3424,  0.2199,
         1.6546, -0.1357,  1.1291, -0.5309], grad_fn=&lt;CopyBackwards&gt;)
tensor([-0.6263, -0.5639,  2.1590, -0.3659,  0.2862, -0.4542, -0.4825, -0.1776,
        -0.4242,  0.9525,  0.7138, -0.3107,  1.8733, -0.3406,  0.0277,  1.6775,
         2.1893,  2.0332,  0.7185,  0.0050, -0.1627, -0.1113,  1.0444,  1.4057,
         0.2183,  0.3405,  0.0930,  1.2428, -0.0740,  0.3991, -0.2722,  1.4980,
         0.9207,  0.5008, -1.9297,  0.5600,  1.6416,  1.1550,  0.1440,  0.0739,
        -0.7465, -0.2458,  0.9217,  0.7156, -1.2558, -0.9891, -0.7313,  0.8501,
        -1.2851, -0.3068, -0.0796,  0.9361,  0.0927, -1.2988,  0.7422,  0.1388,
         1.3895, -0.7935,  0.4008, -0.1338,  1.5563,  0.5864,  0.6606, -0.2341,
         0.1218, -0.7313,  0.5073, -0.2941,  0.0316, -2.5356, -0.0885,  2.5765,
         0.2090,  0.2819, -0.0386,  0.7986,  2.1165, -0.0271, -0.2987,  0.2905,
         0.0149,  0.2403,  0.0752, -1.5535,  0.3794,  2.0638,  1.0603,  0.0703,
        -0.3643, -1.5671, -0.4736, -1.3035,  0.6583,  0.2531,  0.9829, -0.6025,
        -0.8148, -0.3457, -0.7339,  0.6758], grad_fn=&lt;CopyBackwards&gt;)




What I am also confused is that I have to convert cuda datatype to numpy, since I used cuda in get_word_embedding method. Is adding .cpu() to convert the datatype correct?

IndexError                                Traceback (most recent call last)
&lt;ipython-input-68-39d73aa6e0de&gt; in &lt;module&gt;()
     17     print("Similarity between '{}' &amp; '{}' : {:0.4f}".format(word_1, word_2, word_similarity))
     18 
---&gt; 19 test_cbow(model, train_words, word_to_ix)

&lt;ipython-input-68-39d73aa6e0de&gt; in test_cbow(model, train_words, word_to_ix)
     14     print(type(word_1_vec))
     15 
---&gt; 16     word_similarity = (word_1_vec.dot(word_2_vec) / (torch.norm(word_1_vec) * torch.norm(word_2_vec))).data.numpy()[0]
     17     print("Similarity between '{}' &amp; '{}' : {:0.4f}".format(word_1, word_2, word_similarity))
     18 

**IndexError: too many indices for array**


In your code, word_similarity is not an array, so you can't access it's 0th element. Just modify your code as:

word_similarity = (word_1_vec.dot(word_2_vec) / (torch.norm(word_1_vec) * torch.norm(word_2_vec))).data.numpy()


You can also use:

word_similarity = (word_1_vec.dot(word_2_vec) / (torch.norm(word_1_vec) * torch.norm(word_2_vec))).item()


Here, the .item() in PyTorch would directly give you a float value since the similarity value is a scalar.


  When to use .cpu()?


To convert a cuda tensor to cpu tensor, you need to use .cpu(). You cannot directly convert a cuda tensor to numpy. You have to convert to cpu tesnor first and then call .numpy().
