forked from mlcommons/training
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselect_example.py
More file actions
170 lines (149 loc) · 4.49 KB
/
select_example.py
File metadata and controls
170 lines (149 loc) · 4.49 KB
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
"""Script to randomly pick certain number of text from C4 dataset.
"""
import argparse
import collections
import hashlib
import io
import logging
import tensorflow as tf
import tensorflow_datasets as tfds
import time
parser = argparse.ArgumentParser(
description="Randomly pick examples from C4 dataset.")
parser.add_argument(
"--data_dir",
type=str,
default="",
help="Path to tfds directory, which contains C4/../x.y.z.")
parser.add_argument(
"--language",
type=str,
default="en",
help="Language of dataset.")
parser.add_argument(
"--version",
type=str,
default="3.0.1",
help="Version of dataset.")
parser.add_argument(
"--split",
type=str,
default="validation",
help="Split of dataset.")
parser.add_argument(
"--num_examples_to_pick",
type=int,
default=24576,
help="Number of examples to pick from dataset.")
parser.add_argument(
"--output_filepath",
type=str,
default="",
help="Path for output tfrecord and hash files.")
args = parser.parse_args()
def create_str_feature(value):
bytes_list = tf.train.BytesList(value=value)
f = tf.train.Feature(bytes_list=bytes_list)
return f
if __name__ == '__main__':
tic = time.time()
ds_name = "c4/" + args.language + ":" + args.version
ds = tfds.load(
ds_name,
split=args.split,
shuffle_files=True,
data_dir=args.data_dir)
num_examples = 0
min_text_length = 2^20
max_text_length = 0
total_text_length = 0
num_lines = 0
min_line_length = 2^20
max_line_length = 0
total_line_length = 0
examples = []
for example in ds:
examples.append(example)
text = example["text"].numpy()
length = len(text)
if length < min_text_length:
min_text_length = length
if length > max_text_length:
max_text_length = length
total_text_length += length
lines = text.split(b"\n")
for line in lines:
line_length = len(line)
if line_length < min_line_length:
min_line_length = line_length
if line_length > max_line_length:
max_line_length = line_length
total_line_length += line_length
num_lines += 1
num_examples += 1
if (num_examples % 10000) == 0:
print(num_examples)
print("Input:")
print(
" num_examples = ", num_examples,
" min_length = ", min_text_length,
" avg_length = ", total_text_length / num_examples,
" max_length = ", max_text_length)
print(
" num_lines = ", num_lines,
" min_length = ", min_line_length,
" avg_length = ", total_line_length / num_lines,
" max_length = ", max_line_length)
min_text_length = 2^20
max_text_length = 0
total_text_length = 0
num_lines = 0
min_line_length = 2^20
max_line_length = 0
total_line_length = 0
writer = tf.io.TFRecordWriter(args.output_filepath + ".tfrecord")
hashout = io.open(args.output_filepath + ".txt", "w", encoding="utf-8", newline="\n")
pick_ratio = num_examples / args.num_examples_to_pick
num_examples_picked = 0
i = 0
feature_names = ["content-length", "content-type", "text", "timestamp", "url"]
for i in range(args.num_examples_to_pick):
example = examples[int(i * pick_ratio)]
text = example["text"].numpy()
length = len(text)
if length < min_text_length:
min_text_length = length
if length > max_text_length:
max_text_length = length
total_text_length += length
text_hash = hashlib.md5(text).hexdigest()
hashout.write(text_hash)
hashout.write('\n')
lines = text.split(b"\n")
for line in lines:
line_length = len(line)
if line_length < min_line_length:
min_line_length = line_length
if line_length > max_line_length:
max_line_length = line_length
total_line_length += line_length
num_lines += 1
features = collections.OrderedDict()
for f in feature_names:
features[f] = create_str_feature([example[f].numpy()])
tf_example = tf.train.Example(features=tf.train.Features(feature=features))
writer.write(tf_example.SerializeToString())
num_examples_picked += 1
writer.close()
hashout.close()
print("Selected:")
print(
" num_examples = ", num_examples_picked,
" min_length = ", min_text_length,
" avg_length = ", total_text_length / num_examples_picked,
" max_length = ", max_text_length)
print(
" num_lines = ", num_lines,
" min_length = ", min_line_length,
" avg_length = ", total_line_length / num_lines,
" max_length = ", max_line_length)