Chiquitin commited on
Commit ·
41fb647
1
Parent(s): 242bd10
upload tokenizer script and configs
Browse files- bin/config.py +22 -0
- bin/train_bpe.py +57 -0
bin/config.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 2 |
+
# #
|
| 3 |
+
# This file was created by: Alberto Palomo Alonso #
|
| 4 |
+
# Universidad de Alcalá - Escuela Politécnica Superior #
|
| 5 |
+
# #
|
| 6 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 7 |
+
# Import statements:
|
| 8 |
+
from dataclasses import dataclass
|
| 9 |
+
|
| 10 |
+
@dataclass
|
| 11 |
+
class TokenizerConfig:
|
| 12 |
+
vocab_size: int = 32_768
|
| 13 |
+
min_frequency: int = 2
|
| 14 |
+
max_length: int = 1024 ** 6
|
| 15 |
+
dataset_portion: float = 1.0
|
| 16 |
+
max_sentences: int = 384
|
| 17 |
+
train_batch_size: int = 1024
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 21 |
+
# END OF FILE #
|
| 22 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
bin/train_bpe.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 2 |
+
# #
|
| 3 |
+
# This file was created by: Alberto Palomo Alonso #
|
| 4 |
+
# Universidad de Alcalá - Escuela Politécnica Superior #
|
| 5 |
+
# #
|
| 6 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 7 |
+
# Import statements:
|
| 8 |
+
from src import SegmentationTokenizer, SentenceSegmenter, SegmentationDataset
|
| 9 |
+
from bin.config import TokenizerConfig
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 13 |
+
# MAIN FUNC #
|
| 14 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 15 |
+
def main():
|
| 16 |
+
# Get configs:
|
| 17 |
+
wikipedia_dataset_path: str = input('Enter path to wikipedia dataset: ')
|
| 18 |
+
config = TokenizerConfig()
|
| 19 |
+
|
| 20 |
+
# Build tokenizer:
|
| 21 |
+
tokenizer = SegmentationTokenizer(
|
| 22 |
+
vocab_size=config.vocab_size,
|
| 23 |
+
min_frequency=config.min_frequency,
|
| 24 |
+
max_length=config.max_length
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Build segmenter:
|
| 28 |
+
segmenter = SentenceSegmenter(max_sentences=config.max_sentences)
|
| 29 |
+
|
| 30 |
+
# Build dataset:
|
| 31 |
+
dataset = SegmentationDataset(
|
| 32 |
+
wikipedia_dataset_path,
|
| 33 |
+
tokenizer=tokenizer,
|
| 34 |
+
segmenter=segmenter,
|
| 35 |
+
percentage=config.dataset_portion
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Train tokenizer:
|
| 39 |
+
tokenizer.train_from_iterator(
|
| 40 |
+
tokenizer.build_iterator(dataset.huggingface_dataset, batch_size=config.train_batch_size)
|
| 41 |
+
)
|
| 42 |
+
tokenizer.save('tokenizer_32768.json')
|
| 43 |
+
|
| 44 |
+
print('Tokenizer saved to tokenizer_32768.json')
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 48 |
+
# MAIN #
|
| 49 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 50 |
+
if __name__ == '__main__':
|
| 51 |
+
main()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 55 |
+
# END OF FILE #
|
| 56 |
+
# - x - x - x - x - x - x - x - x - x - x - x - x - x - x - #
|
| 57 |
+
|