File size: 7,190 Bytes
f5eb6b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
r"""
- basic bpe-tokenizer that doesn't uses byte pairing, insted uses set of initial unique characters
to train the new vocab
- set of initial characters = ["\n", "A", "C", "G", "T", " "] that can be present in a file or are
needed for the tokenizer
- save and load functions, saves two files, '.model' and 'vocab.json' and only '.model' file is loaded
'vocab.json' is just for human interpretation
"""
from tqdm import tqdm
import json
import os
current_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(current_dir)
class DNAtokenizer:
def __init__(self):
"""
inital variables:
- chars = set of unique characters that could be present in the file, that are needed
- merges, vocab = empty dictonaries to store future merges and final vocab
- vocab_size = initially it's equal to 6 or len(chars), updated later
- str_to_idx, idx_to_str = functions enumerate chars to idx and idx to chars
"""
super().__init__()
self.chars = ["\n", "A", "C", "G", "T", " "]
self.vocab_size = len(self.chars)
self.merges = {}
self.vocab = {}
self.string_to_index = {char: idx for idx, char in enumerate(self.chars)}
self.index_to_string = {idx: char for idx, char in enumerate(self.chars)}
def _encode(self, string):
"""
encoder: takes a string, returns a list of integers
eg. AATGC --> ['2', '2', '5', '4', '3']
"""
encoded = [self.string_to_index[char] for char in string]
return encoded
def _decode(self, integer):
"""
decoder: takes a list of integers, returns a string
eg. ['2', '2', '5', '4', '3'] --> AATGC
"""
decoded = ''.join([self.index_to_string[i] for i in integer])
return decoded
def _get_stats(self, ids, counts=None):
"""
takes list of integers and returns dictionary of counts of pairs(consecutive ones)
eg: [1, 2, 3, 1, 2] -> {(1, 2): 2, (2, 3): 1, (3, 1): 1}
allows to update an existing dictionary of counts
"""
counts = {} if counts is None else counts
for pair in zip(ids, ids[1:]):
counts[pair] = counts.get(pair, 0) + 1
return counts
def _merge(self, ids, pair, idx):
"""
in the list of integers, replaces all consecutive pair with the new integer token idx
eg: ids=[1, 2, 3, 1, 2], pair=(1, 2), idx=4 -> [4, 3, 4]
"""
new_ids = []
i = 0
while i < len(ids):
if i+1 < len(ids) and ids[i] == pair[0] and ids[i+1] == pair[1]:
new_ids.append(idx)
i += 2
else:
new_ids.append(ids[i])
i += 1
return new_ids
def _build_vocab(self):
"""
it was causing some bugs, if not used, so I had to use it
"""
return {i: ids for i, ids in enumerate(self.chars)}
def train(self, train_data, target_vocab):
"""
- takes in the data, encodes it using _encode() function, converts each unique char to index
eg. AATGC --> ['2', '2', '5', '4', '3']
- performs iteration till n_merges i.e. target_vocab - self.vocab_size
- each iteration, makes dictonary of 2 consecutive pairs and then merges the max occuring
pair together
- at the end uses merges to build final vocab
Args:
train_data (str): a big file containing lots of dna sequence
target_vocab (integer): name tells you fucking idiot
"""
vocab = self._build_vocab()
tokens = self._encode(train_data)
ids = list(tokens)
merges = {}
n_merges = target_vocab - self.vocab_size + 1
for i in tqdm(range(n_merges), desc='Training the tokenizer\t'):
stats = self._get_stats(ids)
pair = max(stats, key=stats.get)
idx = self.vocab_size + i
ids = self._merge(ids, pair, idx)
merges[pair] = idx
for (p0, p1), idx in merges.items():
vocab[idx] = vocab[p0] + vocab[p1]
self.vocab = vocab
self.merges = merges
self.vocab_size = len(vocab)
def continue_train(self, train_data, n_merges):
"""
- takes in the data, performs iteration till n_merges
- continues from the last index of the loaded merges
- each iteration, makes dictonary of 2 consecutive pairs and then merges the max occuring
pair together (same as train())
- at the end uses merges to build final vocab
Args:
train_data (str): a big file containing lots of dna sequence
n_merges (integer): no of merges
** this function has some problems
"""
tokens = self._encode(train_data)
ids = list(tokens)
for i in tqdm(range(n_merges), desc='Training continue'):
stats = self._get_stats(ids)
pair = max(stats, key=stats.get)
idx = self.vocab_size + i
ids = self._merge(ids, pair, idx)
self.merges[pair] = idx
for (p0, p1), idx in self.merges.items():
self.vocab[idx] = self.vocab[p0] + self.vocab[p1]
self.vocab_size = len(self.vocab)
def encode(self, text):
"""
- takes in the input string, encodes it using initial vocab '_encode()' function
- fetches merges from saved or loaded merges
Args:
train_data (str): string of dna sequence
self.merges (dictonary): contains merges
"""
tokens = self._encode(text)
ids = list(tokens)
while len(ids) >= 2:
stats = self._get_stats(ids)
pair = min(stats, key=lambda p: self.merges.get(p, float('inf')))
if pair not in self.merges:
break
idx = self.merges[pair]
ids = self._merge(ids, pair, idx)
return ids
def decode(self, de_text):
tokens = [self.vocab[idx] for idx in de_text]
text = ''.join(tokens)
return text
def save_model(self, model_prefix):
"""
- basic save_model() funtion, saves two files, '.model' & 'vocab.json'
- '.model' contians all the final merges, each on next line
- 'vocab.json' contians the final vocab, for human interpretation
Args:
model_prefix (str): prefix along with the path
self.merges (dict): contains final merges
self.vocab (dict): contains final vocab
"""
model_file = model_prefix + '.model'
with open(model_file, 'w', encoding='utf-8') as fwrite:
for ids1, ids2 in self.merges:
fwrite.write(f"{ids1} {ids2}\n")
vocab_file = model_prefix + '_vocab.json'
with open(vocab_file, 'w') as f:
json.dump(self.vocab, f)
print('model file saved successfully!')
def load_model(self, model_path):
"""
- loads the '.model' file
- re-writes the merges in the new merges dict
- builds the vocab again for further use
Args:
model_path (str): path to the '.model' file
"""
assert model_path.endswith('.model')
merges = {}
idx = self.vocab_size
with open(model_path, 'r', encoding='utf-8') as fread:
for line in fread:
idx1, idx2 = map(int, line.split())
merges[(idx1, idx2)] = idx
idx += 1
vocab = self._build_vocab()
for (p0, p1), idx in merges.items():
vocab[idx] = vocab[p0] + vocab[p1]
self.merges = merges
self.vocab = vocab
self.vocab_size = len(self.vocab) |