utterworks / fast-bert

Super easy library for BERT based NLP models
Apache License 2.0
1.85k stars 342 forks source link

Binary text classification: The size of tensor a (2) must match the size of tensor b (39) at non-singleton dimension 1 #245

Closed NawelAr closed 4 years ago

NawelAr commented 4 years ago

Hello,

I'm working on binary text classification with CamemBert using fast-bert.

When I run the code below

from fast_bert.data_cls import BertDataBunch from fast_bert.learner_cls import BertLearner

databunch = BertDataBunch(DATA_PATH,LABEL_PATH, tokenizer='camembert-base', train_file='train.csv', val_file='val.csv', label_file='labels.csv', text_col='text', label_col='label', batch_size_per_gpu=8, max_seq_length=512, multi_gpu=multi_gpu, multi_label=False, model_type='camembert-base')

learner = BertLearner.from_pretrained_model( databunch, pretrained_path='camembert-base', #'/content/drive/My Drive/model/model_out' metrics=metrics, device=device_cuda, logger=logger, output_dir=OUTPUT_DIR, finetuned_wgts_path=None, #WGTS_PATH warmup_steps=300, multi_gpu=multi_gpu, is_fp16=True, multi_label=False, logging_steps=50)

learner.fit(epochs=10, lr=9e-5, validate=True, schedule_type="warmup_cosine", optimizer_type="adamw") Everything works fine until training. I get this error message when I try to train my model:

RuntimeError Traceback (most recent call last)

in () 3 validate=True, 4 schedule_type="warmup_cosine", ----> 5 optimizer_type="adamw") 2 frames /usr/local/lib/python3.6/dist-packages/fast_bert/learner_cls.py in fit(self, epochs, lr, validate, return_results, schedule_type, optimizer_type) 421 # Evaluate the model against validation set after every epoch 422 if validate: --> 423 results = self.validate() 424 for key, value in results.items(): 425 self.logger.info( /usr/local/lib/python3.6/dist-packages/fast_bert/learner_cls.py in validate(self, quiet, loss_only) 515 for metric in self.metrics: 516 validation_scores[metric["name"]] = metric["function"]( --> 517 all_logits, all_labels 518 ) 519 results.update(validation_scores) /usr/local/lib/python3.6/dist-packages/fast_bert/metrics.py in fbeta(y_pred, y_true, thresh, beta, eps, sigmoid) 56 y_pred = (y_pred > thresh).float() 57 y_true = y_true.float() ---> 58 TP = (y_pred * y_true).sum(dim=1) 59 prec = TP / (y_pred.sum(dim=1) + eps) 60 rec = TP / (y_true.sum(dim=1) + eps) RuntimeError: The size of tensor a (2) must match the size of tensor b (39) at non-singleton 1 How can I fix this ?
jkhalsa-arabesque commented 4 years ago

which metrics are you using? I seem to face this when I am using F1/fbeta. there are different errors when using roc_auc and accuracy_thresh. Not sure which work best with multiclass vs multilabel. works fine when using only accuracy

NawelAr commented 4 years ago

which metrics are you using? I seem to face this when I am using F1/fbeta. there are different errors when using roc_auc and accuracy_thresh. Not sure which work best with multiclass vs multilabel. works fine when using only accuracy

I'm using fbeta, and accuracy. Setting multi_label to True fixed it, but I am still confused about it.

NawelAr commented 4 years ago

which metrics are you using? I seem to face this when I am using F1/fbeta. there are different errors when using roc_auc and accuracy_thresh. Not sure which work best with multiclass vs multilabel. works fine when using only accuracy

Hello, My accuracy is always down to 0. I know it's due to setting multi-label to True. Is there a proper way to do binary classification ?

jkhalsa-arabesque commented 4 years ago

Set multi-label to false and only use the accuracy metric. Follow the example in the readme.

NawelAr commented 4 years ago

Set multi-label to false and only use the accuracy metric. Follow the example in the readme.

Thank you, It worked ! Do you know how to get the vector of probabilities after training ? I would like to draw a ROC.

callmeYe commented 3 years ago

Set multi-label to false and only use the accuracy metric. Follow the example in the readme.

Thank you, It worked ! Do you know how to get the vector of probabilities after training ? I would like to draw a ROC.

Do you have a solution now?