pytorch TypeError:不支持/的操作数类型:'序列分类器输出'和'整数'

ndasle7k  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(120)

我正在使用hugginface库来训练一个关于分类问题的bert模型。

model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=10) 

    def training_step(self, batch, batch_nb):
       sequence, label = batch
       input_ids, attention_mask, labels = self.prepare_batch(sequence=sequence, label=label)
       loss = self.model(input_ids=input_ids,
                      attention_mask=attention_mask,
                      labels=labels) 
       tensorboard_logs = {'train_loss': loss}

我在培训开始前收到以下错误:

in training_step
closure_loss = closure_loss / self.trainer.accumulate_grad_batches 
TypeError: unsupported operand type(s) for /: 'SequenceClassifierOutput' and 'int'

我用的是火焰 lightning

fcg9iug3

fcg9iug31#

调用self.model()返回一个 SequenceClassifierOutput 类型的对象。要访问loss,需要调用它的loss属性:
替换

loss = self.model(input_ids=input_ids,
                      attention_mask=attention_mask,
                      labels=labels)

output = self.model(input_ids=input_ids,
                      attention_mask=attention_mask,
                      labels=labels) 
loss = output.loss

相关问题