【问题标题】:The size of tensor a (2) must match the size of tensor b (39) at non-singleton dimension 1张量 a (2) 的大小必须与非单维 1 处的张量 b (39) 的大小相匹配
【发布时间】:2020-11-24 10:14:07
【问题描述】:

这是我第一次从事文本分类工作。我正在使用主要受 fastai 启发的 fast-bert 库使用 CamemBert 进行二进制文本分类。

当我运行下面的代码时

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")

在训练之前一切正常。 当我尝试训练我的模型时收到此错误消息:

RuntimeError                              Traceback (most recent call last)
<ipython-input-13-9b5c6ad7c8f0> in <module>()
      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 dimension 1 

我该如何解决这个问题? 谢谢

【问题讨论】:

  • 你能解释一下你是如何以及在哪里执行准确度函数的吗?,
  • 你能修复它吗?也坚持这一点。

标签: nlp pytorch bert-language-model huggingface-transformers fast-ai


【解决方案1】:

fbeta 不适用于二元分类。仅使用准确性解决了这个问题。

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 2021-03-09
    • 2019-11-09
    • 2021-07-12
    • 2020-12-18
    • 2020-12-13
    • 1970-01-01
    • 2022-08-13
    • 2019-09-02
    相关资源
    最近更新 更多