how to use cosssim in gensim

大兔子大兔子 提交于 2021-01-29 08:07:15

问题


My questioon is about cossim usage.

I have this fragment of a very big fuction:

for elem in lList:
    temp = []
    try:
        x = dict(np.ndenumerate(np.asarray(model[elem])))
    except:
        if x not in embedDict.keys():
            x = np.random.uniform(low=0.0, high=1.0, size=300)
            embedDict[elem] = x
        else:
            x  =  dict(np.ndenumerate(np.asarray(embedDict[elem])))

    for w in ListWords:
        try:
            y =  dict(np.ndenumerate(np.asarray(model[w])))
        except:
            if y not in embedDict.keys():
                y = np.random.uniform(low=0.0, high=1.0, size=300)
                embedDict[w] = y
            else:
                y =  dict(np.ndenumerate(np.asarray(embedDict[w])))

        temp.append(gensim.matutils.cossim(x,y))

I get the following exception:

File "./match.py", line 129, in getEmbedding
    test.append(gensim.matutils.cossim(x,y))
  File "./Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/matutils.py", line 746, in cossim
    vec1, vec2 = dict(vec1), dict(vec2)
TypeError: cannot convert dictionary update sequence element #0 to a sequence

Can you please help me and explain to me what this exception means?


回答1:


The arguments of gensim.matutils.cossim are expected to be of type list of (int, float) but you are using dictionaries.

The exception happens in the cossim function with the following cossim implementation:

vec1, vec2 = dict(vec1), dict(vec2)

With the correct type, dict(vec) works:

dict([(1, 2.), (3, 4.), (5, 6.)])

But if you do not provide the correct type, it throws the exception, for instance with:

dict([1, 2, 3])


来源:https://stackoverflow.com/questions/53449019/how-to-use-cosssim-in-gensim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!