在非单一维度 1 处,张量 a (2) 的大小必须与张量 b (39) 的大小匹配

2024-04-19

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

当我运行下面的代码时

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 

我怎样才能解决这个问题 ? 谢谢


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在非单一维度 1 处,张量 a (2) 的大小必须与张量 b (39) 的大小匹配 的相关文章

随机推荐