# Inside the Token classification pipeline (PyTorch)

**URL:** <https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838>\
**Category:** Course\
**Created:** [January 18, 2022, 9:40am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838 "2022-01-18T09:40:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghadeermobasher](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@ghadeermobasher](https://discuss.huggingface.co/u/ghadeermobasher)\
**Post date:** [January 18, 2022, 9:40am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/1 "2022-01-18T09:40:13Z")

</div>

Hi,  
I have been looking through one of the part 2 course tutorials for Inside the Token classification pipeline (PyTorch) illustrated by @sgugger. I have tried to use the example in `(https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/videos/token_pipeline_pt.ipynb)` and used `sshleifer/tiny-dbmdz-bert-large-cased-finetuned-conll03-english` as a model-checkpoint. However, I had the following error,  
AttributeError: ‘list’ object has no attribute ‘argmax’ in `import torch

probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)[0].tolist()  
predictions = probabilities.argmax(dim=-1)[0].tolist()  
print(predictions)`

Am I missing something?

Best,  
Ghadeer

---

<div class="post-metadata">

**Author:** ![ehalit](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ehalit/32/28854_2.png) [@ehalit](https://discuss.huggingface.co/u/ehalit)\
**Post date:** [January 18, 2022, 9:44am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/2 "2022-01-18T09:44:41Z")

</div>

You are converting the tensor to a Python list which does not have an `argmax` function. Try it like this:

```auto
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
predictions = probabilities.argmax(dim=-1)[0].tolist()
print(predictions)

```

Which basically delays list conversion until the very end.

---

<div class="post-metadata">

**Author:** ![ghadeermobasher](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@ghadeermobasher](https://discuss.huggingface.co/u/ghadeermobasher)\
**Post date:** [January 18, 2022, 9:48am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/3 "2022-01-18T09:48:19Z")

</div>

Thanks, but how to fetch the results,

 ![Capture](https://us1.discourse-cdn.com/hellohellohello/original/2X/a/a2d466ed2e4bdd53f6b19f92afa5f74a78773859.jpeg)  
if the predictions variable isn’t itertable?

---

<div class="post-metadata">

**Author:** ![ehalit](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ehalit/32/28854_2.png) [@ehalit](https://discuss.huggingface.co/u/ehalit)\
**Post date:** [January 18, 2022, 10:00am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/4 "2022-01-18T10:00:28Z")

</div>

The code snippet from before looks at the prediction for the first token, which makes `predictions` an `int` rather than an iterable `list`. You can get predictions for all the tokens by changing it like this:

```auto
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
predictions = probabilities.argmax(dim=-1).tolist()
print(predictions)

```

---

<div class="post-metadata">

**Author:** ![ghadeermobasher](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@ghadeermobasher](https://discuss.huggingface.co/u/ghadeermobasher)\
**Post date:** [January 18, 2022, 10:19am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/5 "2022-01-18T10:19:57Z")

</div>

Yes, I got it. Thanks! However, the final output of this piece of code doesn’t perform as expected. Here is the link to the [code](https://colab.research.google.com/drive/1BVoYsxolY1YhVLwDtgkHLkS1bazJ0C7K?usp=sharing).

 ![Capture2](https://us1.discourse-cdn.com/hellohellohello/original/2X/8/84cd19ae4f031a7b6de5d9c9c6bdc1362b4fc69b.jpeg)

---

<div class="post-metadata">

**Author:** ![ehalit](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ehalit/32/28854_2.png) [@ehalit](https://discuss.huggingface.co/u/ehalit)\
**Post date:** [January 18, 2022, 10:41am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/6 "2022-01-18T10:41:35Z")

</div>

You are welcome, I don’t know about the last problem. My output looks like this:

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/8/81673ccd1b10489061fa2ba4f09f053ff8a6c458.png)

---

<div class="post-metadata">

**Author:** ![ghadeermobasher](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@ghadeermobasher](https://discuss.huggingface.co/u/ghadeermobasher)\
**Post date:** [January 18, 2022, 10:54am UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/7 "2022-01-18T10:54:12Z")

</div>

Yes, same for me. In the end, we need to merge the B- and I- labels entities. @sgugger any clue?

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](https://discuss.huggingface.co/u/lewtun)\
**Post date:** [January 21, 2022, 2:37pm UTC](https://discuss.huggingface.co/t/inside-the-token-classification-pipeline-pytorch/13838/8 "2022-01-21T14:37:06Z")

</div>

Hey @ghadeermobasher there are several strategies that you can use to merge the entities and my suggestion would be to inspect the implementation in the [`token-classification` pipeline](https://github.com/huggingface/transformers/blob/833635e25997a00117076c50bde5c45f9b883ada/src/transformers/pipelines/token_classification.py#L226). As you can see in the [docstring](https://github.com/huggingface/transformers/blob/833635e25997a00117076c50bde5c45f9b883ada/src/transformers/pipelines/token_classification.py#L67) there are quite some subtleties associated with merging entities correctly (depends on the language, tokenizer etc)!
