Upload T5/showtokensT5.py with huggingface_hub
Browse files- T5/showtokensT5.py +37 -0
T5/showtokensT5.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/env python
|
2 |
+
|
3 |
+
"""
|
4 |
+
It turns out that T5 does some odd token padding of its text input.
|
5 |
+
This program shows the padding, in human readable form
|
6 |
+
"""
|
7 |
+
|
8 |
+
|
9 |
+
from transformers import T5Tokenizer,T5EncoderModel
|
10 |
+
import torch
|
11 |
+
|
12 |
+
T="mcmonkey/google_t5-v1_1-xxl_encoderonly"
|
13 |
+
|
14 |
+
tokenizer = T5Tokenizer.from_pretrained(T)
|
15 |
+
print("loded tokenzier")
|
16 |
+
|
17 |
+
def get_tokens(word):
|
18 |
+
tokens = tokenizer(word, return_tensors="pt")
|
19 |
+
input_ids = tokens.input_ids
|
20 |
+
|
21 |
+
print("Bare input_ids:",input_ids)
|
22 |
+
decoded_tokens = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0])
|
23 |
+
print("Tokenized input:", decoded_tokens)
|
24 |
+
|
25 |
+
|
26 |
+
# id should be a numeral
|
27 |
+
def get_token_from_id(id):
|
28 |
+
decoded_tokens = tokenizer.convert_ids_to_tokens(id)
|
29 |
+
print("Tokenized id:", decoded_tokens)
|
30 |
+
|
31 |
+
|
32 |
+
get_tokens("cat")
|
33 |
+
get_tokens("dogs and cats living together")
|
34 |
+
get_token_from_id(1712)
|
35 |
+
|
36 |
+
|
37 |
+
|