'BertModel' object has no attribute 'predict'

#1
by msperka - opened

Having an issue:


ModuleAttributeError Traceback (most recent call last)
Cell In[15], line 9
6 model.eval()
8 sentence = 'בשנת 1948 השלים אפרים קישון את לימודיו בפיסול מתכת ובתולדות האמנות והחל לפרסם מאמרים הומוריסטיים'
----> 9 print(model.predict([sentence], tokenizer))

File ~\AppData\Local\anaconda3\envs\nlp_env\lib\site-packages\torch\nn\modules\module.py:778, in Module.getattr(self, name)
776 if name in modules:
777 return modules[name]
--> 778 raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
779 type(self).name, name))

ModuleAttributeError: 'BertModel' object has no attribute 'predict'

DICTA: The Israel Center for Text Analysis org

How did you load in the model?

Thanks for the quick response
I cloned to my PC and loading as follows:
tokenizer = AutoTokenizer.from_pretrained('dicta-il/dictabert-morph', cache_dir= r"F:\nlp_project\py\dictabert-morph")
model = AutoModel.from_pretrained('dicta-il/dictabert-morph', cache_dir= r"F:\nlp_project\py\dictabert-morph")

'''
from transformers import AutoModel, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained('dicta-il/dictabert-morph', cache_dir= r"F:\nlp_project\py\dictabert-morph")
model = AutoModel.from_pretrained('dicta-il/dictabert-morph', cache_dir= r"F:\nlp_project\py\dictabert-morph")

model.eval()

sentence = 'בשנת 1948 השלים אפרים קישון את לימודיו בפיסול מתכת ובתולדות האמנות והחל לפרסם מאמרים הומוריסטיים'
print(model.predict([sentence], tokenizer))
'''

DICTA: The Israel Center for Text Analysis org

You need to add trust_remote_code=True in the initialization of the model.

i get TypeError: init() got an unexpected keyword argument 'trust_remote_code'


TypeError Traceback (most recent call last)
Cell In[22], line 4
1 from transformers import AutoModel, AutoTokenizer
3 tokenizer = AutoTokenizer.from_pretrained('dicta-il/dictabert-morph')
----> 4 model = AutoModel.from_pretrained('dicta-il/dictabert-morph', trust_remote_code=True)
6 model.eval()
8 sentence = 'בשנת 1948 השלים אפרים קישון את לימודיו בפיסול מתכת ובתולדות האמנות והחל לפרסם מאמרים הומוריסטיים'

File ~\AppData\Local\anaconda3\envs\nlp_env\lib\site-packages\transformers\modeling_auto.py:640, in AutoModel.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
635 config, kwargs = AutoConfig.from_pretrained(
636 pretrained_model_name_or_path, return_unused_kwargs=True, **kwargs
637 )
639 if type(config) in MODEL_MAPPING.keys():
--> 640 return MODEL_MAPPING[type(config)].from_pretrained(
641 pretrained_model_name_or_path, *model_args, config=config, **kwargs
642 )
643 raise ValueError(
644 "Unrecognized configuration class {} for this kind of AutoModel: {}.\n"
645 "Model type should be one of {}.".format(
646 config.class, cls.name, ", ".join(c.name for c in MODEL_MAPPING.keys())
647 )
648 )

File ~\AppData\Local\anaconda3\envs\nlp_env\lib\site-packages\transformers\modeling_utils.py:947, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
944 config.name_or_path = pretrained_model_name_or_path
946 # Instantiate model.
--> 947 model = cls(config, *model_args, **model_kwargs)
949 if state_dict is None and not from_tf:
950 try:

DICTA: The Israel Center for Text Analysis org

Try running pip install -U transformers
and then clearing the cache and rerunning the code.

Thank you. it works well now

msperka changed discussion status to closed

Sign up or log in to comment