huggingface / huggingface/course
Chapter 6 - missing item from the code
- Dominant language
- MDX
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 13m
- Merged PRs (30d)
- 1
Description
In [chapter 6](https://huggingface.co/course/chapter6/3?fw=pt#inside-the-token-classification-pipeline), the index is missing from the code.
Current code:
```py
results = []
tokens = inputs.tokens()
for idx, pred in enumerate(predictions):
label = model.config.id2label[pred]
if label != "O":
results.append(
{"entity": label, "score": probabilities[idx][pred], "word": tokens[idx]}
)
print(results)
```
Current output based on the code from the course:
```
[{'entity': 'I-PER', 'score': 0.9993828535079956, 'word': 'S'},
{'entity': 'I-PER', 'score': 0.9981549382209778, 'word': '##yl'},
{'entity': 'I-PER', 'score': 0.995907187461853, 'word': '##va'},
{'entity': 'I-PER', 'score': 0.9992327690124512, 'word': '##in'},
{'entity': 'I-ORG', 'score': 0.9738932251930237, 'word': 'Hu'},
{'entity': 'I-ORG', 'score': 0.9761150479316711, 'word': '##gging'},
{'entity': 'I-ORG', 'score': 0.9887976050376892, 'word': 'Face'},
{'entity': 'I-LOC', 'score': 0.9932106137275696, 'word': 'Brooklyn'}]
```
So, as we see that 'index' is missing from the code but exists on the output. So, we need to add it as the following:
```py
results = []
tokens = inputs.tokens()
print(tokens)
for idx, pred in enumerate(predictions):
label = model.config.id2label[pred]
if label != "O":
results.append(
{
"entity": label,
"score": probabilities[idx][pred],
"index": idx,
"word": tokens[idx],
}
)
print(results)
```
Then, the current results will be shown as the course:
```
[{'entity': 'I-PER', 'score': 0.9993828, 'index': 4, 'word': 'S', 'start': 11, 'end': 12},
{'entity': 'I-PER', 'score': 0.99815476, 'index': 5, 'word': '##yl', 'start': 12, 'end': 14},
{'entity': 'I-PER', 'score': 0.99590725, 'index': 6, 'word': '##va', 'start': 14, 'end': 16},
{'entity': 'I-PER', 'score': 0.9992327, 'index': 7, 'word': '##in', 'start': 16, 'end': 18},
{'entity': 'I-ORG', 'score': 0.97389334, 'index': 12, 'word': 'Hu', 'start': 33, 'end': 35},
{'entity': 'I-ORG', 'score': 0.976115, 'index': 13, 'word': '##gging', 'start': 35, 'end': 40},
{'entity': 'I-ORG', 'score': 0.98879766, 'index': 14, 'word': 'Face', 'start': 41, 'end': 45},
{'entity': 'I-LOC', 'score': 0.99321055, 'index': 16, 'word': 'Brooklyn', 'start': 49, 'end': 57}]
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.