forked from akanyaani/gpt-2-tensorflow2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_pipeline.py
65 lines (48 loc) · 1.72 KB
/
data_pipeline.py
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
import collections
import tensorflow as tf
PAD_ID = 0
UNKNOWN_ID = 1
START_ID = 3
END_ID = 4
def load_vocab(vocab_path):
vocab = collections.OrderedDict()
index = 0
for line in open(vocab_path, 'r').read().splitlines():
vocab[line.split()[0]] = index
index += 1
inv_vocab = {v: k for k, v in vocab.items()}
return vocab, inv_vocab
def convert_by_vocab(vocab, items):
output = []
for item in items:
output.append(vocab[item])
return output
def convert_tokens_to_ids(vocab, tokens):
return convert_by_vocab(vocab, tokens)
def convert_ids_to_tokens(inv_vocab, ids):
return convert_by_vocab(inv_vocab, ids)
def parse_example(serialized_example):
data_fields = {
"inputs": tf.io.VarLenFeature(tf.int64),
"targets": tf.io.VarLenFeature(tf.int64)
}
parsed = tf.io.parse_single_example(serialized_example, data_fields)
inputs = tf.sparse.to_dense(parsed["inputs"])
targets = tf.sparse.to_dense(parsed["targets"])
inputs = tf.cast(inputs, tf.int32)
targets = tf.cast(targets, tf.int32)
return inputs, targets
def input_fn(tf_records,
batch_size=32,
padded_shapes=([-1], [-1]),
epoch=10,
buffer_size=10000):
if type(tf_records) is str:
tf_records = [tf_records]
dataset = tf.data.TFRecordDataset(tf_records, buffer_size=10000)
dataset = dataset.shuffle(buffer_size=buffer_size)
dataset = dataset.map(parse_example,num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.padded_batch(batch_size, padded_shapes=padded_shapes)
dataset = dataset.repeat(epoch)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return dataset