AttributeError: 'WmdSimilarity' object has no attribute 'w2v_model'

22 views
Skip to first unread message

Tedo Vrbanec

unread,
Nov 20, 2021, 1:16:36 PM11/20/21
to Gensim
I wanted to check if WmdSimilarity is any better (it should not be) from  multiprocessing wmdistance, but it did not work at all. 
So, I made a little example and the result was exactly the same...
Could someone check what is wrong with me or gensim? ;)

from gensim.models import Word2Vec
from gensim.similarities import WmdSimilarity

a = "Trump’s decision not to join the Trans-Pacific Partnership (TPP) came as little surprise. During his election campaign he railed against international trade deals, blaming them for job losses and focusing anger in the industrial heartland. Obama had argued that this deal would provide an effective counterweight to China in the region."
b = "Trump's decision to withdraw the US from TPP is also a first step in the administration's efforts to amass a governing coalition to push the new President's agenda, one that includes the blue-collar workers who defected from Democrats and flocked to Trump's candidacy in November."
c = "Although the Trans-Pacific Partnership had not been approved by Congress, Mr. Trump’s decision to withdraw not only doomed former President Barack Obama’s signature trade achievement, but also carried broad geopolitical implications in a fast-growing region. The deal, which was to link a dozen nations from Canada and Chile to Australia and Japan in a complex web of trade rules, was sold as a way to permanently tie the United States to East Asia and create an economic bulwark against a rising China."

def w2v(corpus):
    w2v_model = Word2Vec(corpus, alpha=0.025, vector_size=50,
         window=5, min_count=1, workers=2, epochs=50, sg = 0)
    return w2v_model


corpus = "\n".join([a,b,c])
print(corpus)
model = w2v(corpus)
print(model, type(model))
print(WmdSimilarity([a,b], model))

OUTPUT:
Word2Vec(vocab=47, vector_size=50, alpha=0.025) <class 'gensim.models.word2vec.Word2Vec'>
Traceback (most recent call last):
  File "/home/tedo/diagnose/proba.py", line 18, in <module>
    print(WmdSimilarity([a,b], model))
  File "/usr/local/lib/python3.9/dist-packages/gensim/similarities/docsim.py", line 1099, in __str__
    return "%s<%i docs, %i features>" % (self.__class__.__name__, len(self), self.w2v_model.wv.syn0.shape[1])
AttributeError: 'WmdSimilarity' object has no attribute 'w2v_model'


Radim Řehůřek

unread,
Nov 21, 2021, 5:23:16 AM11/21/21
to Gensim
Hi Tedo,

that looks like a bug in WmdSimilarity.__str__ (which is called by print()). Could you please open a ticket at https://github.com/RaRe-Technologies/gensim?

Many thanks,
Radim

Tedo Vrbanec

unread,
Nov 21, 2021, 12:36:00 PM11/21/21
to Gensim
I signed up a bug, thank you.
Reply all
Reply to author
Forward
0 new messages