Commit
·
d59d2fe
1
Parent(s):
8cbd6e9
Upload 5 files
Browse files
data/shakespeare_char/input.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
data/shakespeare_char/meta.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6ee5a37533af83b67fcbe6b93705fde9e15e78bafe895f54b2cb2cb32534526c
|
| 3 |
+
size 703
|
data/shakespeare_char/prepare.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Prepare the Shakespeare dataset for character-level language modeling.
|
| 3 |
+
So instead of encoding with GPT-2 BPE tokens, we just map characters to ints.
|
| 4 |
+
Will save train.bin, val.bin containing the ids, and meta.pkl containing the
|
| 5 |
+
encoder and decoder and some other related info.
|
| 6 |
+
"""
|
| 7 |
+
import os
|
| 8 |
+
import pickle
|
| 9 |
+
import requests
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
# download the tiny shakespeare dataset
|
| 13 |
+
input_file_path = os.path.join(os.path.dirname(__file__), 'input.txt')
|
| 14 |
+
if not os.path.exists(input_file_path):
|
| 15 |
+
data_url = 'https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt'
|
| 16 |
+
with open(input_file_path, 'w') as f:
|
| 17 |
+
f.write(requests.get(data_url).text)
|
| 18 |
+
|
| 19 |
+
with open(input_file_path, 'r') as f:
|
| 20 |
+
data = f.read()
|
| 21 |
+
print(f"length of dataset in characters: {len(data):,}")
|
| 22 |
+
|
| 23 |
+
# get all the unique characters that occur in this text
|
| 24 |
+
chars = sorted(list(set(data)))
|
| 25 |
+
vocab_size = len(chars)
|
| 26 |
+
print("all the unique characters:", ''.join(chars))
|
| 27 |
+
print(f"vocab size: {vocab_size:,}")
|
| 28 |
+
|
| 29 |
+
# create a mapping from characters to integers
|
| 30 |
+
stoi = { ch:i for i,ch in enumerate(chars) }
|
| 31 |
+
itos = { i:ch for i,ch in enumerate(chars) }
|
| 32 |
+
def encode(s):
|
| 33 |
+
return [stoi[c] for c in s] # encoder: take a string, output a list of integers
|
| 34 |
+
def decode(l):
|
| 35 |
+
return ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string
|
| 36 |
+
|
| 37 |
+
# create the train and test splits
|
| 38 |
+
n = len(data)
|
| 39 |
+
train_data = data[:int(n*0.9)]
|
| 40 |
+
val_data = data[int(n*0.9):]
|
| 41 |
+
|
| 42 |
+
# encode both to integers
|
| 43 |
+
train_ids = encode(train_data)
|
| 44 |
+
val_ids = encode(val_data)
|
| 45 |
+
print(f"train has {len(train_ids):,} tokens")
|
| 46 |
+
print(f"val has {len(val_ids):,} tokens")
|
| 47 |
+
|
| 48 |
+
# export to bin files
|
| 49 |
+
train_ids = np.array(train_ids, dtype=np.uint16)
|
| 50 |
+
val_ids = np.array(val_ids, dtype=np.uint16)
|
| 51 |
+
train_ids.tofile(os.path.join(os.path.dirname(__file__), 'train.bin'))
|
| 52 |
+
val_ids.tofile(os.path.join(os.path.dirname(__file__), 'val.bin'))
|
| 53 |
+
|
| 54 |
+
# save the meta information as well, to help us encode/decode later
|
| 55 |
+
meta = {
|
| 56 |
+
'vocab_size': vocab_size,
|
| 57 |
+
'itos': itos,
|
| 58 |
+
'stoi': stoi,
|
| 59 |
+
}
|
| 60 |
+
with open(os.path.join(os.path.dirname(__file__), 'meta.pkl'), 'wb') as f:
|
| 61 |
+
pickle.dump(meta, f)
|
| 62 |
+
|
| 63 |
+
# length of dataset in characters: 1115394
|
| 64 |
+
# all the unique characters:
|
| 65 |
+
# !$&',-.3:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
|
| 66 |
+
# vocab size: 65
|
| 67 |
+
# train has 1003854 tokens
|
| 68 |
+
# val has 111540 tokens
|
data/shakespeare_char/train.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6ec305602a99ac2802745a134e1f5e33e2231b4855525b00b9aebb730ac2626f
|
| 3 |
+
size 2007708
|
data/shakespeare_char/val.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d37d30cc0c8327c270d493299c3dca54135f6d5f1c9ef60cda78076e311204b1
|
| 3 |
+
size 223080
|