I finetune Bert on multicalssification task. The best model during training was saved in a directory called âcheckpoint_10â.
Now I want to use it to get predictions on new data but I get this error ? why does it ask for a num_labels
emb_feature = tokenizer(x, padding="max_length", truncation=True, max_length=512)
model ='./checkpoint_10'
model = XXXForSequenceClassification.from_pretrained(model, num_labels=3)
# Define test trainer
test_trainer = Trainer(model)
# Make prediction
raw_pred, _, _ = test_trainer.predict(emb_feature) # list of dict with "input_id, attention_mask, token_input_ids"
print(raw_pred)
Error
File "/aitements/classifying.py", line 355, in <module>
pred = predict(test, test_model)
File "/aitements/classifying.py", line 219, in predict
model =XXXForSequenceClassification.from_pretrained(model, num_labels=3)
File "/uw/.conda/envs/bert/lib/python3.9/site-packages/transformers/modeling_utils.py", line 1179, in from_pretrained
model = cls(config, *model_args, **model_kwargs)
TypeError: __init__() missing 1 required positional argument: 'num_labels'
this is the beguinning of my sequenceclassification task :
class XXXForSequenceClassification(XXXModel):
"""
xxx Model for Classification Tasks.
"""
def __init__(self, config, num_labels, freeze_encoder=False):
"""
@param XXXBert: a XXXBertModel object
@param classifier: a torch.nn.Module classifier
@param freeze_encoder (bool): Set `False` to fine-tune the XXXBERT model
"""
# instantiate the parent class XXXbertModel
super().__init__(config)
# Specify hidden size of FB hidden size of our classifier, and number of labels
# instantiate num. of classes
self.num_labels = num_labels
# instantiate and load a pretrained XXXbertModel
self.encoder = XXXbertModel.from_pretrained(PRE_TRAINED_MODEL_NAME)