Inside the Token classification pipeline (PyTorch)
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 usedsshleifer/tiny-dbmdz-bert-large-cased-finetuned-conll03-englishas a model-checkpoint. However, I had the following error,AttributeError: ‘list’ object has no attribute ‘argmax’ in `import torchprobabilities = 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
2022-01-18T09:44:41Z
You are converting the tensor to a Python list which does not have anargmaxfunction. Try it like this: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.
2022-01-18T09:48:19Z
Thanks, but how to fetch the results,Capture1192×630 71.4 KBif the predictions variable isn’t itertable?
2022-01-18T10:00:28Z
The code snippet from before looks at the prediction for the first token, which makespredictionsanintrather than an iterablelist. You can get predictions for all the tokens by changing it like this:probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
predictions = probabilities.argmax(dim=-1).tolist()
print(predictions)
2022-01-18T10:19:57Z
Yes, I got it. Thanks! However, the final output of this piece of code doesn’t perform as expected. Here is the link to thecode.Capture2777×632 56.4 KB
2022-01-18T10:41:35Z
You are welcome, I don’t know about the last problem. My output looks like this:image970×488 18.2 KB
2022-01-18T10:54:12Z
Yes, same for me. In the end, we need to merge the B- and I- labels entities.@sguggerany clue?
2022-01-21T14:37:06Z
Hey@ghadeermobasherthere are several strategies that you can use to merge the entities and my suggestion would be to inspect the implementation in thetoken-classificationpipeline. As you can see in thedocstringthere are quite some subtleties associated with merging entities correctly (depends on the language, tokenizer etc)!