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()
In line 13, it seems that the variable
logitsreturned by themodel()function is an instance of a python Class namedTokenClassifierOutput. You can addprint(logits.__dict__)after line 13 to figure out what inside this instance. Anyaway, if you runlogits.detach()method,logitsshould be an instance of the pytorch.Tensor object.