Downloads · 30 days
0
ldilov/llama2-bg-tokenizer
llama2-bg-tokenizer is a machine learning model from ldilov. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as apache-2.0.
The tokenizer has been crafted with a specific focus on complementing the capabilities of Llama2 and Llama2-based models. Here's a detailed overview of its design philosophy and linguistic proficiency:
Downloads · 30 days
0
Access
Public
Updated Feb 15, 2024
Repo size
—
Likes
1
Public
Click a slice to open those files.
.json1.8 MB · 99%
From the Hugging Face model README
The tokenizer has been crafted with a specific focus on complementing the capabilities of Llama2 and Llama2-based models. Here's a detailed overview of its design philosophy and linguistic proficiency:
from transformers import LlamaTokenizerFast
# Initialize the tokenizer
tokenizer = LlamaTokenizerFast.from_pretrained("ldilov/llama2-bg-tokenizer")
# Example text in Bulgarian
bg_text = "Това е примерен текст на български език."
# Example text in English
en_text = "This is a sample text in English."
# Tokenize the Bulgarian text
tokens_bg = tokenizer.encode(bg_text, return_tensors="pt")
print("Tokenized Bulgarian Text:", tokens_bg)
# Tokenize the English text
tokens_en = tokenizer.encode(en_text, return_tensors="pt")
print("Tokenized English Text:", tokens_en)
Running the above code will produce output similar to the following, which shows the tokenized representation of both Bulgarian and English texts:
Tokenized Bulgarian Text: {'input_ids': [...], 'attention_mask': [...]}
Tokenized English Text: {'input_ids': [...], 'attention_mask': [...]}
The tokenizer training approach showcases a sophisticated and advanced methodology tailored for optimizing tokenizer performance specifically for Llama2 compatibility. This approach stands out due to its comprehensive customization capabilities, dynamic adjustments based on dataset analysis, and the integration of advanced tokenization techniques. Here are the key components and advantages:
<unk>, <s>, </s>), enhancing the tokenizer's ability to handle unknown tokens and start/end of sequence markers effectively.decoders.Replace: Replaces specified characters (e.g., the replacement character "▁") with another character (e.g., a space), aiding in the reconstruction of the original text from tokenized sequences.decoders.ByteFallback: Provides a fallback mechanism for handling bytes directly, useful for dealing with unknown or out-of-vocabulary tokens.decoders.Fuse(): Fuses consecutive tokens when possible to reduce tokenization granularity, potentially improving model performance by reducing sparsity.decoders.Strip: Removes leading or trailing characters (e.g., spaces), cleaning up the tokenized output for further processing.Prepend("▁"): Adds a specific character (e.g., the replacement character "▁") to the beginning of the text, marking the start of processing.Replace(r" ", "▁"): Replaces spaces with a specified character, aiding in distinguishing between spaces as part of the text and as token separators.NFKC(): Applies Unicode normalization (NFKC), standardizing characters and reducing the complexity of text encoding.pre_tokenizers.Sequence([Punctuation()]): Applies a sequence of pre-tokenizers, such as identifying and separating punctuation, which helps in parsing the text more accurately before the main tokenization step.TemplateProcessing(single, pair, special_tokens): Defines templates for processing single inputs and pairs of inputs, incorporating special tokens at specified positions.dynamic_tokens) for inclusion, improving model performance on specific domains or datasets.dropout but instead calculates on the fly specifically tailored value, based on the current training dataset. This ensures that tokenizer model can generalize better by putting more weight on context rather than specifics. This would be beneficial at later stage when finetuning LLM with this tokenizer.min_frequency) on dataset analysis ensures that the tokenizer remains effective across different text domains.<url>. Replacing varied URLs with a single token prevents the model from overfitting to specific web addresses, which are usually not relevant to understanding the text's general context.URLs can introduce a vast number of unique tokens into the vocabulary. Replacing them with a single token significantly simplifies the model's vocabulary. By abstracting away the specifics of URLs, models can focus more on the actual textual content.The evaluation of the tokenizer is crucial to ensure its effectiveness and accuracy. The approach used for evaluation relies on assessing the tokenizer's ability to accurately encode and decode textual data, aiming to measure how well the tokenizer can reproduce the original text after a round-trip of tokenization and detokenization. Here's a detailed explanation of how the loss function works and the significance of the evaluation scores:
Tokenization: Each sentence in the dataset is encoded to token IDs using tokenizer.encode(example).ids. This step converts text into a sequence of tokens that the model can understand.
Detokenization: The token IDs are then decoded back into text using tokenizer.decode(tokenizer.encode(original).ids). This step attempts to reconstruct the original text from the token IDs.
Distance Calculation: For texts that do not match, the Levenshtein distance (a measure of the difference between two sequences) is calculated between the original and detokenized text, normalized by the original text length. This distance provides a quantitative measure of how much the texts differ.
Loss: The overall loss is computed as the average of these distances (distance / round_trip_errors), providing a single metric that reflects the tokenizer's accuracy in reproducing the original text.
The evaluation results after training and testing the tokenizer with 5,000 random sentences not included in the training corpus are summarized in the table below:
| Version | Vocab Size | Loss | Training Time (seconds) |
|---|---|---|---|
| v1.1 | 32,000 | 0.00791045752872809 | 9188.8694 |
Vocab Size: Indicates the tokenizer's vocabulary size. A larger vocabulary can potentially capture more nuances in the text but might also increase the risk of overfitting or inefficiency. Current vocab size is compatible with existing Llama2 based models
Loss: The average normalized Levenshtein distance across all errors. Evaluating on sentences not included in the training corpus and achieving such a low loss value highlights the tokenizer's strong generalization capability.
From Levenshtein distance definition => On average, the necessary edits to recover the original text from the detokenized output account for 0.79% of the length of the original texts.
The loss value of 0.00791045752872809 suggests that the tokenizer performs well in maintaining the integrity of the text through the tokenization process and sustains a high level of fidelity. Mathematically, this low loss score signifies a high degree of similarity between the original and detokenized texts, demonstrating the tokenizer's effectiveness. The process of detokenization, converting tokenized representations back into their original text form, does not always guarantee a 1:1 exact match to the original text. While the goal of detokenization is to reconstruct the original text as closely as possible, minor differences can occur. These variances are generally acceptable and sometimes inevitable.
Most NLP models and applications can tolerate some level of discrepancy between original and processed texts.
This approach represents a significant advancement over regular tokenization methods, offering a more adaptable, efficient, and accurate solution for preparing text data for machine learning models, especially those compatible with Llama2.
When utilizing datasets from the Hugging Face 🤗 Datasets library for training models, it's crucial to acknowledge the contributions of the authors and organizations that have made these resources available. Below is a formatted credits section recognizing the datasets used:
OSCAR Dataset:
Bulgarian Poems Dataset:
BG OPUS100 Processed Dataset:
Reasoning BG Dataset:
Clickbait News BG Dataset:
700,000 bulgarian sentences~21,000,000 tokens5000 bulgarian sentences~15,000 tokensAcknowledging the sources of datasets and estimating the volume of training data are crucial steps in ensuring transparency and reproducibility in machine learning projects. These acknowledgments not only give credit where it's due but also provide insights into the scale and nature of the data used for model training.