Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get attribute 'callback' on <module '__main__' (built-in)> when calling load() #2835

Closed
AkibSadmanee opened this issue May 10, 2020 · 1 comment
Labels
duplicate This issue duplicates another issue

Comments

@AkibSadmanee
Copy link

AkibSadmanee commented May 10, 2020

Problem description

I have trained a word2vec model using the following code
https://gist.github.com/AkibSadmanee/d946f14c397c317f938eeea66dca79dc

But when I am trying to laod the model using the load() function, I get an AttributeError: Can't get attribute 'callback' on <module 'main' (built-in)>

from gensim.models import Word2Vec
w2v = Word2Vec.load("word2vec_dim300_skipgram_iter35.model")

However, for your infirmation, the model was trained on Bangla language corpora which contain unicode characters.
I tried reinstalling the package but the issue persists. I guess I am missing something important.

The detailed error I am getting:

Traceback (most recent call last):
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\word2vec.py", line 1141, in load
    model = super(Word2Vec, cls).load(*args, **kwargs)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\base_any2vec.py", line 1230, in load
    model = super(BaseWordEmbeddingsModel, cls).load(*args, **kwargs)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\base_any2vec.py", line 602, in load
    return super(BaseAny2VecModel, cls).load(fname_or_handle, **kwargs)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\utils.py", line 435, in load
    obj = unpickle(fname)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\utils.py", line 1398, in unpickle
    return _pickle.load(f, encoding='latin1')
AttributeError: Can't get attribute 'callback' on <module '__main__' (built-in)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\word2vec.py", line 1152, in load
    return load_old_word2vec(*args, **kwargs)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\deprecated\word2vec.py", line 169, in load_old_word2vec      
    old_model = Word2Vec.load(*args, **kwargs)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\deprecated\word2vec.py", line 1617, in load
    model = super(Word2Vec, cls).load(*args, **kwargs)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\deprecated\old_saveload.py", line 87, in load
    obj = unpickle(fname)
  File "C:\Users\akibs\Anaconda3\lib\site-packages\gensim\models\deprecated\old_saveload.py", line 379, in unpickle
    return _pickle.loads(file_bytes, encoding='latin1')
AttributeError: Can't get attribute 'callback' on <module '__main__' (built-in)>

This is the package information of my installed version,

Name: gensim
Version: 3.8.3
Summary: Python framework for fast Vector Space Modelling
Home-page: http://radimrehurek.com/gensim
Author: Radim Rehurek
Author-email: [email protected]
License: LGPLv2.1
Location: c:\users\akibs\anaconda3\lib\site-packages
Requires: smart-open, scipy, numpy, Cython, six
Required-by:
Please provide the output of:

@piskvorky
Copy link
Owner

piskvorky commented May 10, 2020

Gensim uses pickle to save models, and pickle stores functions as just names (references). The actual code is not stored. Your callback was a function named callback in the module __main__.

So when you did Word2Vec.load, Python tried to load your function by its name __main__.callback. That function in that module doesn't exist => you got an error.

To work around this, use properly named functions from importable modules, such as my_module.callback. Note that my_module has to be importable at the time you do Word2Vec.load.

Having said that, in the future Gensim will not serialize the callbacks at all. So this issue won't even come up. Closing as duplicate of #2136 – please continue there.

@mpenkov mpenkov added the duplicate This issue duplicates another issue label Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue duplicates another issue
Projects
None yet
Development

No branches or pull requests

3 participants