VinAIResearch/PhoBERT

Revert Tokenization nonsense!

HariWu1995 opened this issue · 2 comments

Dear,

Before apply model, I want to check how tokenization works.
Firstly, I run
import transformers
from transformers import AutoModel, AutoTokenizer
embedder = AutoModel.from_pretrained("vinai/phobert-base")
tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base", use_fast=False)
to download artifacts into C:\Users\ADMIN.cache\huggingface\transformers.

I find vocab file in this folder (already check with file in HuggingFace). I build 2 dictionaries as below
with open("C:/Users/ADMIN/.cache/huggingface/transformers/970c6224b2713c8b52a7bcfc4d5a951c9bb88302e4523388b50f28284e87ac44.26ba0c8945e559c68d0bc35d24fea16f5463a49fe8f134e0c32261d590b577fa", 'r', encoding='utf-8') as f:
vocab = f.readlines()
vocab = [p[:-1] for p in vocab]
vocab_forward = dict()
vocab_reverse = dict()
for pair in tqdm(vocab):
k, v = pair.split(' ')[:2]
if k in vocab_forward.keys():
vocab_forward[k].append(v)
else:
vocab_forward[k] = [v]
if v in vocab_reverse.keys():
vocab_reverse[v].append(k)
else:
vocab_reverse[v] = [k]

I used VNCoreNLP to segment sentence into words as below:
['Công_ty', 'cổ_phần', 'Tập_đoàn', 'FLC', '(', 'FLC', ',', 'mã', 'chứng_khoán', ':', 'FLC', ')', 'vừa', 'công_bố', 'một', 'loạt', 'giao_dịch', 'với', 'các', 'bên', 'liên_quan', 'trong', 'giai_đoạn', '2019-2021', 'theo', 'yêu_cầu', 'của', 'Uỷ_ban', 'Chứng_khoán', 'Nhà_nước', '.']
However, after running tokenization
tokens = tokenizer.encode(sentence)[1:-1]
I cannot receive the original tokens. Below is the code:
for ti, token in enumerate(tokens):
token = str(token)
print(sentence[ti], token, vocab_reverse[token] if token in vocab_reverse.keys() else None)
the result is:

Original token: Công_ty
token: 290 - reverted token: ['Yeah@@', 'ngứa_@@', 'ĩ_vãng', 'Lolli@@', 'enei', 'đẽ', 'ト', 'ốc_liệt', 'ng_tín', 'ành_viên', 'thành_niên', 'Ai_C@@']
Original token: cổ_phần
token: 1272 - reverted token: ['asha', 'ưa@@']
Original token: Tập_đoàn
token: 1183 - reverted token: ['khép
@@', 'NAM']
Original token: FLC
token: 6505 - reverted token: ['chất_chứa', 'Quốc_Hùng', 'Đoài', 'Liveshow']
Original token: (
token: 20 - reverted token: None
Original token: FLC
token: 6505 - reverted token: ['chất_chứa', 'Quốc_Hùng', 'Đoài', 'Liveshow']
Original token: ,
token: 4 - reverted token: None
Original token: mã
token: 1624 - reverted token: ['Gò_Đ@@', 'động_vật
@@']
Original token: chứng_khoán
token: 2476 - reverted token: ['7,6%', 'Hi_Lạp', 'shor@@', 'Tân_Hương', '21-3', 'Fantas@@', 'Lớp_@@', 'Allardyce', 'Y_Tế', 'Liên_Bộ', 'ính_phòng', 'văn_tự', 'isi@@', 'ay_tr@@', 'Hui', 'va_vấp']
Original token: :
token: 27 - reverted token: None
Original token: FLC
token: 6505 - reverted token: ['chất_chứa', 'Quốc_Hùng', 'Đoài', 'Liveshow']
Original token: )
token: 19 - reverted token: None
Original token: vừa
token: 164 - reverted token: ['水', 'Kêu_@@', 'át_nước', 'nh_diện', '真', 'Heyn@@', '府', '구@@', 'trao_@@', 'Tẻ@@', 'TNĐ', '色@@', '_South@@', 'イ', '靜@@', '紅@@', '會@@', '格@@']
Original token: công_bố
token: 576 - reverted token: ['Middles@@', '_trương', '花@@', 'nhộn', 'Dong_Gun', 'Lị@@', 'Don@@']
Original token: một
token: 16 - reverted token: None
Original token: loạt
token: 1406 - reverted token: ['Fin@@', 'ẩm_tra', 'Né', 'giải_th@@']
Original token: giao_dịch
token: 786 - reverted token: ['Wiscon@@', 'thù@@']
Original token: với
token: 15 - reverted token: None
Original token: các
token: 9 - reverted token: None
Original token: bên
token: 145 - reverted token: ['ż', 'ア', 'ghi_nợ', 'Tàm', 'tài_sản
@@', '汉@@', '義', 'n_đơn', 'ǒ@@']
Original token: liên_quan
token: 314 - reverted token: ['nhân_hoá', 'ž', 'triển@@', '拉@@', 'Hmeym@@', 'n_lút', 'π', 'ở_thành', 'hiếp', 'ôn_phu', 'phạm_pháp_luật']
Original token: trong
token: 12 - reverted token: None
Original token: giai_đoạn
token: 609 - reverted token: ['n_thuyết', 'Lò_G@@', 'nh_thiêng', 'tư_th@@']
Original token: 2019-2021
token: 3 - reverted token: None
Original token: theo
token: 63 - reverted token: None
Original token: yêu_cầu
token: 285 - reverted token: ['Cocker@@', 'Raphael
@@', 'bánh_tr@@', '良@@', 'súng_l@@', 'sổ@@', 'DAR@@']
Original token: của
token: 7 - reverted token: None
Original token: Uỷ_ban
token: 871 - reverted token: ['phương_hoá', 'acca', 'xiêu', 'yol']
Original token: Chứng_khoán
token: 5149 - reverted token: ['Bích_Hằng', 'ack
@@', 'Aleksand@@', 'hoang_dại']
Original token: Nhà_nước
token: 544 - reverted token: ['nh_thường_quân', 'Bảo_t@@', 'Cheon@@', 'ambon', 'ững_sờ']
Original token: .
token: 5 - reverted token: None

My question is: there is a problem with tokenizer or the vocab file is wrong?

I see in the vocab file that there are many tokens being mapped with a single id. Is there any similarity between these tokens so you can do this way or something?
Appreciate any reasonable explanation.

from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base")
line = " ".join(['Công_ty', 'cổ_phần', 'Tập_đoàn', 'FLC', '(', 'FLC', ',', 'mã', 'chứng_khoán', ':', 'FLC', ')', 'vừa', 'công_bố', 'một', 'loạt', 'giao_dịch', 'với', 'các', 'bên', 'liên_quan', 'trong', 'giai_đoạn', '2019-2021', 'theo', 'yêu_cầu', 'của', 'Uỷ_ban', 'Chứng_khoán', 'Nhà_nước', '.'])
print(tokenizer.decode(tokenizer.encode(line)))

# <s> Công_ty cổ_phần Tập_đoàn FLC ( FLC, mã chứng_khoán : FLC ) vừa công_bố một loạt giao_dịch với các bên liên_quan trong giai_đoạn 2019-2021 theo yêu_cầu của Uỷ_ban Chứng_khoán Nhà_nước. </s>

You should double-check your code (e.g. reading files). There is no problem with the tokenizer and the vocab.