amaiya / ktrain

ktrain is a Python library that makes deep learning and AI more accessible and easier to apply
Apache License 2.0
1.23k stars 269 forks source link

Loaded predictor: AttributeError: 'Transformer' object has no attribute 'tok' #193

Closed not-william closed 4 years ago

not-william commented 4 years ago

I've previously trained a BERT text classification model and saved the predictor using predictor.save(). However, when it comes to loading up the predictor, I get the following error.

predictor = ktrain.load_predictor('multiclass_predictor')
predictor.predict(["This is a test sentence"])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-f3b58ccc38ea> in <module>
----> 1 predictor.predict(["This is a test sentence"])

/usr/local/lib/python3.7/site-packages/ktrain/text/predictor.py in predict(self, texts, return_proba)
     48         # get predictions
     49         if U.is_huggingface(model=self.model):
---> 50             tseq = self.preproc.preprocess_test(texts, verbose=0)
     51             tseq.batch_size = self.batch_size
     52             texts = tseq.to_tfdataset(train=False)

/usr/local/lib/python3.7/site-packages/ktrain/text/preprocessor.py in preprocess_test(self, texts, y, verbose)
   1094         """
   1095         self.check_trained()
-> 1096         return self.preprocess_train(texts, y=y, mode='test', verbose=verbose)
   1097 
   1098 

/usr/local/lib/python3.7/site-packages/ktrain/text/preprocessor.py in preprocess_train(self, texts, y, mode, verbose)
   1064           TransformerDataset if self.use_with_learner = True else tf.Dataset
   1065         """
-> 1066         tseq = super().preprocess_train(texts, y=y, mode=mode, verbose=verbose)
   1067         if self.use_with_learner: return tseq
   1068         tseq.batch_size = self.batch_size

/usr/local/lib/python3.7/site-packages/ktrain/text/preprocessor.py in preprocess_train(self, texts, y, mode, verbose)
    871 
    872         # convert examples
--> 873         dataset = hf_convert_examples(texts, y=y, tokenizer=self.tok, max_length=self.maxlen,
    874                                       pad_on_left=bool(self.name in ['xlnet']),
    875                                       pad_token=self.tok.convert_tokens_to_ids([self.tok.pad_token][0]),

AttributeError: 'Transformer' object has no attribute 'tok'

Any feedback greatly appreciated. Thanks.

amaiya commented 4 years ago

Hello,

This may have something to do with the fact that ktrain was updated a while ago (after you trained your model) to instantiate the tokenizer "on-the-fly" instead of saving it as part of the the Preprocessor instance (i.e., Transformer object). This was done because some tokenizers in transformers library are not easily pickable (and it also results in a smaller Predictor footprint).

EDIT: I tried to reproduce the error by training a model and saving a predictor using a very old version of ktrain and then reloading and predicting using a newer version. But, everything worked correctly, so not sure what's happening on your end. You can try the steps below to try and fix things, though.

Can you try the following?

# load just the model
from transformers import *
model = TFAutoModelForSequenceClassification.from_pretrained('multiclass_predictor')
print(model.summary())

Does the model load correctly? If so, then the problem should be easy to fix, as you can re-create your predictor using this loaded model and then re-save your predictor using the latest version of ktrain (all without having to re-train your model). Upon doing so, you will be able to use the predictor normally to make predictions using the newest version of ktrain.

Please let me know the output of the above code. Also, make sure you're using the latest version of ktrain.

not-william commented 4 years ago

Many thanks, it was indeed a version issue. The original code

predictor = ktrain.load_predictor('multiclass_predictor')
predictor.predict(["This is a test sentence"])

works after upgrading to the latest version, so the machine I trained the model on must have had a higher version installed. Thanks again.