pythonnumpygpuspacy

Numpy Error : Implicit conversion to a NumPy array is not allowed. Please use `.get()` to construct a NumPy array explicitly


I 'am trying to find similar vector with spacy and numpy. I found the code following url : Mapping word vector to the most similar/closest word using spaCy

But I'm getting type error

import numpy as np

your_word = "country"

ms = nlp.vocab.vectors.most_similar(
    np.asarray([nlp.vocab.vectors[nlp.vocab.strings[your_word]]]), 
    n=10, )

words = [nlp.vocab.strings[w] for w in ms[0][0]] distances = ms[2] print(words)

error :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[139], line 6
      1 import numpy as np
      3 your_word = "country"
      5 ms = nlp.vocab.vectors.most_similar(
----> 6     np.asarray([nlp.vocab.vectors[nlp.vocab.strings[your_word]]]), 
      7     n=10,
      8 )
     10 words = [nlp.vocab.strings[w] for w in ms[0][0]]
     11 distances = ms[2]

File cupy/_core/core.pyx:1475, in cupy._core.core._ndarray_base.__array__()

TypeError: Implicit conversion to a NumPy array is not allowed. Please use `.get()` to construct a NumPy array explicitly.

I'm using gpu, how can I fix this?


Solution

  • From the

    Reference - Mapping word vector to the most similar/closest word using spaCy
    Reference - https://docs.cupy.dev/en/stable/reference/ndarray.html

    You need to convert CuPy arrays to be explicitly converted to NumPy arrays before operations on the CPU

    Edit lets make reshape the word vector into a 2D array with one row and multiple columns

    import numpy as np
    
    your_word = "country"
    
    
    word_vector = nlp.vocab.vectors[nlp.vocab.strings[your_word]]
    
    
    word_vector_2d = word_vector.reshape(1, -1)  # '-1' lets numpy infer the correct size for the second dimension
    
    
    ms = nlp.vocab.vectors.most_similar(
        word_vector_2d, 
        n=10,
    )
    
    
    words = [nlp.vocab.strings[w] for w in ms[0][0]]
    distances = ms[2]
    print(words)