AttributeError: 'TokenClassifierOutput' object has no attribute 'detach'

1.6k Views Asked by At

Could you please assist me in solving the following error: 'TokenClassifierOutput' object has no attribute 'detach' The error persists even when modifying the code like output = model(input_ids, token_type_ids=None, attention_mask=input_mask,) logits = output.loss.detach().cpu().numpy()

AttributeError                            Traceback (most recent call last)
<ipython-input-49-3e5217186317> in <module>
     13         logits = model(input_ids, token_type_ids=None, attention_mask=input_mask,)
     14 
---> 15     logits = logits.detach().cpu().numpy()
     16     logits = [list(p) for p in np.argmax(logits, axis=2)]
     17 

AttributeError: 'TokenClassifierOutput' object has no attribute 'detach'
model.eval()

y_true = []
y_pred = []
eval_loss, eval_accuracy = 0, 0
nb_eval_steps, nb_eval_examples = 0, 0

for batch in valid_dataloader:
    batch = tuple(t.to(device) for t in batch)
    input_ids, input_mask, label_ids = batch

    with torch.no_grad():
        logits = model(input_ids, token_type_ids=None, attention_mask=input_mask,)

    logits = logits.detach().cpu().numpy()
    logits = [list(p) for p in np.argmax(logits, axis=2)]
    
    label_ids = label_ids.to('cpu').numpy()
    input_mask = input_mask.to('cpu').numpy()
    
2

There are 2 best solutions below

0
Suneburg On

In line 13, it seems that the variable logits returned by the model() function is an instance of a python Class named TokenClassifierOutput. You can add print(logits.__dict__) after line 13 to figure out what inside this instance. Anyaway, if you run logits.detach() method, logits should be an instance of the pytorch.Tensor object.

1
Ahmad Khadra On

logits = logits.logits.detach().cpu().numpy()

It works