yangheng95 / PyABSA

Sentiment Analysis, Text Classification, Text Augmentation, Text Adversarial defense, etc.;
https://pyabsa.readthedocs.io
MIT License
937 stars 159 forks source link

AttributeError: 'Trainer' object has no attribute 'batch_infer'[Bug Report] #87

Closed hamzaakramsybrid closed 2 years ago

hamzaakramsybrid commented 2 years ago

Hello dear, I hope you are fine. First of all, thank you so much for your great effort in this repository. I am following the quickstart of Aspect Polarity Classification in which I have trained the APCModelList.SLIDE_LCFS_BERT model with 5 epochs. After 5 epochs when I run the 5th block of sentiment inference it gave me the error given below. I tried to locate the function batch_infer in Trainer class but could not find it. Maybe I am mistaken but kindly resolve this issue and guide me. Thank you

Aspect Polarity Classification

APCModelList.SLIDE_LCFS_BERT

SemEval

AttributeError: 'Trainer' object has no attribute 'batch_infer' AttributeError Traceback (most recent call last)

in () 1 # batch inferring_tutorials returns the results, save the result if necessary using save_result=True 2 inference_dataset = ABSADatasetList.SemEval # or set your local dataset ----> 3 results = sent_classifier.batch_infer(target_file=inference_dataset, 4 print_result=True, 5 save_result=True, AttributeError: 'Trainer' object has no attribute 'batch_infer'
yangheng95 commented 2 years ago

Hello,

I notice this problem, this error occurs in above 1.2.7 version, please roll back to 1.2.6 by pip install pyabsa==1.2.6 and use the demo scripts with commit tag "1.2.6".

I will revert the last commit on github to fix the demo scripts, please mind the commit tag.

yangheng95 commented 2 years ago

Please follow the latest demo scripts and version 1.2.8, and feel free to report any problem. Thanks!

hamzaakramsybrid commented 2 years ago

I have tried version 1.2.8 but I am still getting the same error.

hamzaakramsybrid commented 2 years ago

image

yangheng95 commented 2 years ago

Can you show me your entire script?

hamzaakramsybrid commented 2 years ago

from pyabsa.functional import Trainer from pyabsa.functional import APCConfigManager from pyabsa.functional import ABSADatasetList

Get model list for Bert-based APC models

from pyabsa.functional import APCModelList

Get model list for Bert-based APC baseline models

from pyabsa.functional import BERTBaselineAPCModelList

Get model list for GloVe-based APC baseline models

from pyabsa.functional import GloVeAPCModelList

Choose a Bert-based APC models param_dict

apc_config_english = APCConfigManager.get_apc_config_english()

Choose a Bert-based APC baseline models param_dict

apc_config_english = APCConfigManager.get_apc_config_bert_baseline()

Choose a GloVe-based APC baseline models param_dict

apc_config_english = APCConfigManager.get_apc_config_glove()

Specify a Bert-based APC model

apc_config_english.model = APCModelList.SLIDE_LCFS_BERT

Specify a Bert-based APC baseline model

apc_config_english.model = BERTBaselineAPCModelList.ASGCN_BERT

Specify a GloVe-based APC baseline model

apc_config_english.model = GloVeAPCModelList.ASGCN

apc_config_english.similarity_threshold = 1 apc_config_english.max_seq_len = 80 apc_config_english.dropout = 0.5 apc_config_english.log_step = 5 apc_config_english.num_epoch = 5 apc_config_english.evaluate_begin = 4 apc_config_english.l2reg = 0.0005 apc_config_english.seed = {1, 2, 3} apc_config_english.cross_validate_fold = -1

dataset_path = "/content/drive/SemEval/" #or set your local dataset

dataset_path = ABSADatasetList.SemEval #or set your local dataset print(dataset_path) sent_classifier = Trainer(config=apc_config_english, dataset=dataset_path, # train set and test set will be automatically detected checkpoint_save_mode=1, # = None to avoid save model auto_device=True # automatic choose CUDA or CPU )

batch inferring_tutorials returns the results, save the result if necessary using save_result=True

inference_dataset = ABSADatasetList.SemEval # or set your local dataset results = sent_classifier.batch_infer(target_file=inference_dataset, print_result=True, save_result=True, ignore_error=True, )

I am following the quick start of Aspect Polarity Classification while changing the num_epoch to 5 from 10.

yangheng95 commented 2 years ago

The usage in the readme is outdated, please follow the specific demo scripts.

This release-note of 1.2.8 indicates that we should use Trainer().load_trained_model() to get the inference model:

sent_classifier = Trainer(config=apc_config_english,
dataset=dataset_path, # train set and test set will be automatically detected
checkpoint_save_mode=1, # = None to avoid save model
auto_device=True # automatic choose CUDA or CPU
).load_trained_model() 
hamzaakramsybrid commented 2 years ago

It's working now. I am using load_trained_model() function then I am able to use batch_infer function to get the results. Thank you.