[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to train from memory #544

Merged
merged 8 commits into from
Nov 28, 2020
Merged
Prev Previous commit
Next Next commit
Add example for training with datasets
  • Loading branch information
n1t0 committed Nov 28, 2020
commit 723ee7c9ee3b1619463d411784e358166098c691
19 changes: 19 additions & 0 deletions bindings/python/examples/train_with_datasets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import datasets
from tokenizers import normalizers, pre_tokenizers, Tokenizer, models, trainers

# Build a tokenizer
bpe_tokenizer = Tokenizer(models.BPE())
bpe_tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()
bpe_tokenizer.normalizer = normalizers.Lowercase()

# Initialize a dataset
dataset = datasets.load_dataset("wikitext", "wikitext-103-raw-v1")

# Build an iterator over this dataset
def batch_iterator():
batch_length = 1000
for i in range(0, len(dataset["train"]), batch_length):
yield dataset["train"][i : i + batch_length]["text"]

# And finally train
bpe_tokenizer.train_from_iterator(batch_iterator(), length=len(dataset["train"]))