# T5 for Named Entity Recognition

**URL:** <https://discuss.huggingface.co/t/t5-for-named-entity-recognition/2167>\
**Category:** 🤗Transformers\
**Created:** [November 22, 2020, 9:15pm UTC](https://discuss.huggingface.co/t/t5-for-named-entity-recognition/2167 "2020-11-22T21:15:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DoMkRaT25](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@DoMkRaT25](https://discuss.huggingface.co/u/DoMkRaT25)\
**Post date:** [November 22, 2020, 9:15pm UTC](https://discuss.huggingface.co/t/t5-for-named-entity-recognition/2167/1 "2020-11-22T21:15:54Z")

</div>

The task is as follows: need to write the code for NamedEntityRecognition(Token classification), using the T5 model. Who knows how to do this, please write explicitly on the example.

This should work as for example:

```
from transformers import AutoModelForTokenClassification, AutoTokenizer
import torch
model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english", return_dict=True)
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
label_list = [
    "O", # Outside of a named entity
    "B-MISC", # Beginning of a miscellaneous entity right after another miscellaneous entity
    "I-MISC", # Miscellaneous entity
    "B-PER", # Beginning of a person's name right after another person's name
    "I-PER", # Person's name
    "B-ORG", # Beginning of an organisation right after another organisation
    "I-ORG", # Organisation
    "B-LOC", # Beginning of a location right after another location
    "I-LOC" # Location
]
sequence = "Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, therefore very" \
           "close to the Manhattan Bridge."
# Bit of a hack to get the tokens with the special tokens
tokens = tokenizer.tokenize(tokenizer.decode(tokenizer.encode(sequence)))
inputs = tokenizer.encode(sequence, return_tensors="pt")
outputs = model(inputs).logits
predictions = torch.argmax(outputs, dim=2)
print([(token, label_list[prediction]) for token, prediction in zip(tokens, predictions[0].numpy())])
[('[CLS]', 'O'), ('Hu', 'I-ORG'), ('##gging', 'I-ORG'), ('Face', 'I-ORG'), ('Inc', 'I-ORG'), ('.', 'O'), ......]

```

Please, write a concrete example with using T5Model and T5Tokenizer

---

<div class="post-metadata">

**Author:** ![amiya](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@amiya](https://discuss.huggingface.co/u/amiya)\
**Post date:** [November 23, 2020, 4:19pm UTC](https://discuss.huggingface.co/t/t5-for-named-entity-recognition/2167/2 "2020-11-23T16:19:42Z")

</div>

> <https://github.com/google-research/text-to-text-transfer-transformer/issues/27>
>
> Hi there, just as the title says. I wonder, does T5 support sequence labeling (l…ike NER) tasks?
> 
> I don't see report of such tasks in the paper. (correct me If I am wrong).
> 
> Thank you very much!

---

<div class="post-metadata">

**Author:** ![DoMkRaT25](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@DoMkRaT25](https://discuss.huggingface.co/u/DoMkRaT25)\
**Post date:** [November 24, 2020, 12:23pm UTC](https://discuss.huggingface.co/t/t5-for-named-entity-recognition/2167/3 "2020-11-24T12:23:48Z")

</div>

Yes, I exactly know that T5 support NER Tasks. It’s difficult to build, but need.
