GeneTok: Genetic Algorithm-based Tokenizer
GeneTok is a Python library that employs genetic algorithms to craft a tokenizer. This method stands out by using the principles of genetic evolution, the concepts of individuals (in this case, index ranges from the text), mutation, and crossover—to dynamically generate and refine token sets based on the input text(s). This approach is especially beneficial for natural language processing (NLP) tasks, offering a novel solution where traditional tokenization methods might be slow.
Features
- Genetic Algorithm Foundation: Built on the Finch library, GeneTok excels in speed and efficiency, utilizing genetic algorithms for token evolution.
- Customizable Tokenization: Users can define token size ranges and control the token evolution process, allowing for tailored tokenization strategies.
- Fitness Function Optimization: Utilizes a fitness function to assess and select the most effective tokens, considering their frequency and relevance in the source text.
- Serialization Support: Enables saving and loading the tokenizer's state, facilitating easy reuse and distribution.
- Resumable Training: Training sessions can be paused and resumed with entirely different texts, offering flexibility in model development.
Colab notebooks:
- simple example: genetok. Quick overview of the library, train a tokenizer on a few GBs of text rather quickly,
Installation
GeneTok requires Python 3.6 or later. You can install GeneTok directly from the source code:
git clone https://github.com/yourusername/genetok.git
cd genetok
pip install .
Quick Start
Here's a quick example to get you started with Genetok:
from genetok.tokenizer import GeneticTokenizer
# Initialize the GeneticTokenizer
tokenizer = GeneticTokenizer(step_epochs=4)
#Sample text
text = "This is a sample text for the GeneticTokenizer."
# Evolve the tokenizer based on the sample text
# Pass a list of texts, keep each under like 10,000 chars for best speed
tokenizer.evolve([text])
#Tokenize the text
tokens = tokenizer.tokenize(text)
print("Tokens:", tokens)
# Detokenize the tokens back to text
original_text = tokenizer.detokenize(tokens)
print("Original Text:", original_text)
How It Works
Genetok uses a genetic algorithm to evolve a set of tokens that are most effective for tokenizing a given text. It starts with a random set of tokens and iteratively applies genetic operations such as mutation and crossover to evolve these tokens. Each token is represented simply by it's start and end index in a source text. Mutation causes these ranges to change. Every time a good token is found it is added to the list. The fitness of each token is determined based on its frequency and utility in the source text, guiding the selection process towards more effective tokenization strategies.
Drawbacks:
- Speed has it's costs, the tokens may not be the absolute global "best", but the training is much faster than typical tokenizers.
- Far from complete, lots more features to add and bugs to weed out.
Example Implementation
For a detailed example of how to use Genetok on a larger dataset, refer to implimentation.py in the repository. This example demonstrates loading a
dataset, processing the text, evolving the tokenizer, and then using it to tokenize new texts.
Metadata
Release files for genetok 0.1.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| genetok-0.1.12.tar.gz | 6.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| genetok-0.1.12-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / genetok-0.1.12.tar.gz
| Download URL | genetok-0.1.12.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e0256ac8cd72905f4862d11188625250cbe922d8b4e8d2f216133aa1e10fe4b6
|
|
BLAKE2b-256 checksum How to use checksums |
26e032bd8d86b06020d13ca4660f8a32aa459a472d51551a470a5e9d045071cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.13
|
Release files / genetok-0.1.12-py3-none-any.whl
| Download URL | genetok-0.1.12-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ecacf735d5001a64ae5b9602517f43c763e9fa0642bc14905a42f341459b6650
|
|
BLAKE2b-256 checksum How to use checksums |
1aa8c89a2e7b472232dfebbed2b1b90116b31d7c6ff3bf6e06e29922311c400d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.13
|
Security for genetok 0.1.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.