Update README.md
Browse files
README.md
CHANGED
|
@@ -87,9 +87,9 @@ The following language tags can be used for prefixing the model input:
|
|
| 87 |
The tag must be prepended to the prompt as a prefix using the format `<{tag}>: ` (e.g., `<pt-br>: `).
|
| 88 |
**Note:** a space between the prefix colon (`:`) and the beginning of the text is mandatory.
|
| 89 |
|
| 90 |
-
## Example
|
| 91 |
|
| 92 |
-
For batched inference & training it is
|
| 93 |
|
| 94 |
```python
|
| 95 |
from transformers import T5ForConditionalGeneration, AutoTokenizer
|
|
@@ -104,3 +104,24 @@ print(phones)
|
|
| 104 |
# ['laɪf ɪz laɪk ʌ bɑks ʌv t̠ʃɑkləts']
|
| 105 |
```
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
The tag must be prepended to the prompt as a prefix using the format `<{tag}>: ` (e.g., `<pt-br>: `).
|
| 88 |
**Note:** a space between the prefix colon (`:`) and the beginning of the text is mandatory.
|
| 89 |
|
| 90 |
+
## Example 1: inference with tokenizer
|
| 91 |
|
| 92 |
+
For batched inference & training it is recommended using a tokenizer class for handling padding, truncation and additional tokens:
|
| 93 |
|
| 94 |
```python
|
| 95 |
from transformers import T5ForConditionalGeneration, AutoTokenizer
|
|
|
|
| 104 |
# ['laɪf ɪz laɪk ʌ bɑks ʌv t̠ʃɑkləts']
|
| 105 |
```
|
| 106 |
|
| 107 |
+
## Example 2: inference without tokenizer
|
| 108 |
+
|
| 109 |
+
For standalone inference, the decoding without the tokenizer reads as
|
| 110 |
+
|
| 111 |
+
```python
|
| 112 |
+
import torch
|
| 113 |
+
import json
|
| 114 |
+
from transformers import T5ForConditionalGeneration
|
| 115 |
+
model = T5ForConditionalGeneration.from_pretrained('fdemelo/g2p-multilingual-byt5-tiny-8l-ipa-childes')
|
| 116 |
+
input_ids = torch.tensor([list("<en-na>: Life is like a box of chocolates.".encode("utf-8"))]) + 3 # add shift to account for special tokens <pad>, </s>, <unk>
|
| 117 |
+
preds = model.generate(input_ids=input_ids, num_beams=1, max_length=512)
|
| 118 |
+
# Simplified version of the decoding process (discarding special/added tokens)
|
| 119 |
+
with open("tokenizer_config.json", "r") as f:
|
| 120 |
+
added_tokens = json.load(f).get("added_tokens_decoder", {})
|
| 121 |
+
phone_bytes = [
|
| 122 |
+
bytes([token - 3]) for token in preds[0].tolist() if str(token) not in added_tokens
|
| 123 |
+
]
|
| 124 |
+
phones = b''.join(phone_bytes).decode("utf-8", errors="ignore")
|
| 125 |
+
print(phones)
|
| 126 |
+
# 'laɪf ɪz laɪk ʌ bɑks ʌv t̠ʃɑkləts'
|
| 127 |
+
```
|