0

I'm fine-tuning the BERT model in Named Entity Recognition task using json file dataset, but when I run that code get an error:

    with torch.no_grad():
         token_classifier_output1 = model(b_input_ids,
                            token_type_ids=None,
                            attention_mask=b_input_mask,
                            labels=b_labels)
         token_classifier_output2 = model(b_input_ids, token_type_ids=None,
                       attention_mask=b_input_mask)
    logits = token_classifier_output2.detach().cpu().numpy()
    label_ids = b_labels.to('cpu').numpy()
    predictions.extend([list(p) for p in np.argmax(logits, axis=2)])
    true_labels.append(label_ids)
    
    tmp_eval_accuracy = flat_accuracy(logits, label_ids)
    
    eval_loss += tmp_eval_loss.mean().item()
    eval_accuracy += tmp_eval_accuracy
    
    nb_eval_examples += b_input_ids.size(0)
    nb_eval_steps += 1

when I run that code I get this error: AttributeError: 'TokenClassifierOutput' object has no attribute 'detach'

  • Welcome to Stackoverflow! Please edit your question to add the full error traceback to your question, this will help reviewers to answer your question more quickly. – Koedlt Dec 27 '22 at 16:17
  • This question is almost a duplicate of [your previous question](https://stackoverflow.com/a/74925499/12122460) that I answered before. You want `token_classifier_output2.loss.detach()`. The return type was [changed by PR 5438](https://github.com/huggingface/transformers/pull/5438), which was merged in July, 2020. You can [find out the release numbers before the return type changed](https://github.com/huggingface/transformers/tags?after=v3.0.1) from their tags, and from these old versions, [install an old version](https://stackoverflow.com/q/5226311/12122460) that works with your code. – kotatsuyaki Dec 27 '22 at 18:58
  • Flagging this as a duplicate of [AttributeError: 'TokenClassifierOutput' object has no attribute 'backward'](https://stackoverflow.com/questions/74923250/attributeerror-tokenclassifieroutput-object-has-no-attribute-backward) – kotatsuyaki Dec 27 '22 at 18:58

0 Answers0