/
data_utils.py
309 lines (257 loc) · 11 KB
/
data_utils.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import csv
import collections
import os
import time
from pathlib import Path
import numpy as np
import pandas as pd
import scipy as sp
import tensorflow as tf
# We only use TF for data loading; ensure TF does not see GPU and grab all GPU memory.
tf.config.set_visible_devices([], device_type='GPU')
import tensorflow_datasets as tfds
#############################
### Dataset loading utils ###
#############################
def imdb_loader(batch_size,
split,
data_dir='data/imdb',
is_eval=False,
seed=None,
drop_remainder=True,
public_split=0.0):
"""IMDB logistic regression dataset. x_train/x_test: (25000, 10000).
Args:
batch_size: batch size for the loader
split: 'train' or 'test' sets
data_dir: directory to load the dataset; contains `.npy` files
is_eval: whether this loader is for evaluation
seed: random seed for shuffling
drop_remainder: whether to drop the last batch if it is smaller than `batch_size`
public_split: fraction of data to use as "public data"; if >0, the public split
is returned along the data loader
Returns:
The data loader, number of examples, and (if `public_split` > 0) the public data.
"""
# IMDB is not too large so we load all into memory directly for speed.
# We may want to allow a `mmap` mode for instances with limited RAM.
assert split in ('train', 'test'), f'{split=} not supported!'
fname_x, fname_y = f'x_{split}.npy', f'y_{split}.npy'
dir_path = Path(data_dir)
data_x = np.load(dir_path / fname_x)
data_y = np.load(dir_path / fname_y)
assert len(data_x) == len(data_y)
num_examples = len(data_x)
if (not is_eval) or public_split:
rng = np.random.default_rng(seed)
train_perm = rng.permutation(len(data_y))
data_x, data_y = data_x[train_perm], data_y[train_perm]
if public_split:
num_public_samples = int(public_split * num_examples)
num_examples -= num_public_samples
public_x, public_y = data_x[:num_public_samples], data_y[:num_public_samples]
data_x, data_y = data_x[num_public_samples:], data_y[num_public_samples:]
print(f'[INFO] IMDB binary classification {split=} {num_examples=} ({public_split=})')
if is_eval:
loader = _create_tf_loader(data_x, data_y, batch_size, shuffle_size=None, drop_remainder=False)
else:
loader = _create_tf_loader(
data_x,
data_y,
batch_size,
shuffle_size=num_examples, # Full training set
seed=seed,
drop_remainder=drop_remainder)
if public_split:
return loader, num_examples, (public_x, public_y)
return loader, num_examples
def stackoverflow_tag_loader(batch_size,
split,
data_dir='data/stackoverflow_tag',
is_eval=False,
seed=None,
drop_remainder=True,
public_split=0.0):
"""Loads (centralized, subsampled) StackOverflow tag prediction dataset with generators.
See `imdb_loader` for parameter descriptions.
"""
assert split in ('train', 'test')
fname_x, fname_y = f'x_{split}.npy', f'y_{split}.npy'
dir_path = Path(data_dir)
rng = np.random.default_rng(seed)
# Use mmap & generator to prevent memory overload since stackoverflow is large
data_x = np.load(dir_path / fname_x, mmap_mode='r')
data_y = np.load(dir_path / fname_y)
assert len(data_x) == len(data_y)
num_examples = len(data_x)
indices = rng.permutation(num_examples)
if public_split:
num_public_samples = int(public_split * num_examples)
num_examples -= num_public_samples
indices, public_indices = indices[num_public_samples:], indices[:num_public_samples]
# NOTE: below assumes `public_indices` is a small fraction since `data_x` is memmap'ed
public_x, public_y = data_x[public_indices], data_y[public_indices]
print(f'[INFO] StackOverflow Tag Prediction ({split}) {num_examples} samples ({public_split=})')
def data_gen():
# Shuffle for non-public indices
nonlocal indices
indices = rng.permutation(indices)
for i in indices:
yield (data_x[i], data_y[i])
loader = tf.data.Dataset.from_generator(data_gen,
output_types=(tf.float32, tf.int64),
output_shapes=((10000, ), ()))
# Manually set the length of the dataset
loader = loader.apply(tf.data.experimental.assert_cardinality(num_examples))
if is_eval:
loader = _config_tf_loader(loader, batch_size, shuffle_size=None, drop_remainder=False)
else:
# NOTE: the shuffle size determines memory usage
loader = _config_tf_loader(
loader,
batch_size,
shuffle_size=10000, # around 4% of training set (total 246092 examples)
seed=seed,
drop_remainder=drop_remainder)
if public_split:
return loader, num_examples, (public_x, public_y)
return loader, num_examples
def stackoverflow_tag_loader_cached(batch_size,
split,
data_dir='data/stackoverflow_tag',
is_eval=False,
seed=None,
drop_remainder=True,
public_split=0.0):
"""The same SO tag loader, but load the dataset in memory (typically >30GB)!"""
assert split in ('train', 'test'), f'{split=} not supported!'
fname_x, fname_y = f'x_{split}.npy', f'y_{split}.npy'
dir_path = Path(data_dir)
data_x = np.load(dir_path / fname_x)
data_y = np.load(dir_path / fname_y)
assert len(data_x) == len(data_y)
num_examples = len(data_x)
if (not is_eval) or public_split:
rng = np.random.default_rng(seed)
train_perm = rng.permutation(len(data_y))
data_x, data_y = data_x[train_perm], data_y[train_perm]
if public_split:
num_public_samples = int(public_split * num_examples)
num_examples -= num_public_samples
public_x, public_y = data_x[:num_public_samples], data_y[:num_public_samples]
data_x, data_y = data_x[num_public_samples:], data_y[num_public_samples:]
print(f'[INFO] StackOverflow Tag Prediction ({split}) {num_examples} samples ({public_split=})')
if is_eval:
loader = _create_tf_loader(data_x,
data_y,
batch_size,
shuffle_size=None,
drop_remainder=False,
cache=True)
else:
loader = _create_tf_loader(
data_x,
data_y,
batch_size,
shuffle_size=10000, # around 4% of training set (total 246092 examples)
seed=seed,
drop_remainder=drop_remainder,
cache=True)
if public_split:
return loader, num_examples, (public_x, public_y)
return loader, num_examples
def _load_movielens_100k(data_dir):
names = ['user_id', 'item_id', 'rating', 'timestamp']
ratings_df = pd.read_csv(Path(data_dir) / 'u.data', sep='\t', names=names)
X = ratings_df[['user_id', 'item_id']].values
y = ratings_df['rating'].values
row_indices = X[:, 0]
col_indices = X[:, 1]
values = y.astype(np.float32)
n_users = len(ratings_df['user_id'].unique())
n_items = len(ratings_df['item_id'].unique())
shape = (n_users + 1, n_items + 1)
# print(shape)
# print(len(row_indices), len(col_indices), len(y))
return shape, row_indices, col_indices, values
def movielens_loader(batch_size,
matfac_dim_1: int,
matfac_dim_2: int,
density: float,
seed=None,
split=None,
data_dir='data/movielens_100k',
is_eval=False,
drop_remainder=True,
public_split=0.0):
"""Loader for MovieLens matrix factorization task."""
if public_split:
raise NotImplementedError('`public_split` not implemented for MovieLens')
assert split in ('train', 'test'), f'{split=} not supported!'
shape, rows, cols, vals = _load_movielens_100k(data_dir)
# Create matrix that can be indexed by (row, col) pairs
mat = sp.sparse.coo_matrix((vals, (rows, cols)), shape=shape).tocsr()
# Create loader that randomly samples the non-zero coordinates.
# This implies that the "dataset" is the set of non-zero coordinates onto
# which we apply example-level DP.
data_x = np.array([rows, cols]).T # (n_nonzero, 2)
data_y = vals # (n_nonzero,)
num_examples = len(data_x)
assert len(data_x) == len(data_y)
density = len(vals) / (shape[0] * shape[1])
rng = np.random.default_rng(seed)
perm = rng.permutation(num_examples)
data_x, data_y = data_x[perm], data_y[perm]
train_num_examples = int(num_examples * 0.8)
if split == 'test':
data_x, data_y = data_x[train_num_examples:], data_y[train_num_examples:]
else:
data_x, data_y = data_x[:train_num_examples], data_y[:train_num_examples],
split_examples = len(data_x)
print(f'[INFO] MovieLens_100k has {num_examples} non-zero coords in total '
f'with {split_examples} in {split=} in a {shape} matrix of {density=} ({public_split=})')
if is_eval:
loader = _create_tf_loader(data_x, data_y, batch_size, shuffle_size=None, drop_remainder=False)
else:
loader = _create_tf_loader(
data_x,
data_y,
batch_size,
shuffle_size=split_examples, # Shuffle all data
seed=seed,
drop_remainder=drop_remainder)
return loader, split_examples
###################################
### TensorFlow dataloading for JAX
### From https://jax.readthedocs.io/en/latest/notebooks/neural_network_with_tfds_data.html
###################################
def _config_tf_loader(loader, batch_size, shuffle_size=50000, seed=None, drop_remainder=True):
if shuffle_size is not None:
# A fixed `seed` still allows every new epoch to have different shuffle.
loader = loader.shuffle(shuffle_size, seed=seed, reshuffle_each_iteration=True)
loader = loader.batch(batch_size, drop_remainder=drop_remainder)
loader = loader.prefetch(tf.data.AUTOTUNE)
return loader
def _create_tf_loader(x_data: np.ndarray,
y_data: np.ndarray,
batch_size,
shuffle_size=50000,
seed=None,
drop_remainder=True,
cache=False):
"""Constructs an iterable of numpy batches with tf.data (more CPU efficient)."""
# See also https://www.tensorflow.org/datasets/performances.
loader = tf.data.Dataset.from_tensor_slices((x_data, y_data))
if batch_size == -1:
batch_size = len(x_data) # Full batch.
if cache:
loader = loader.cache()
return _config_tf_loader(loader, batch_size, shuffle_size, seed, drop_remainder)
def benchmark(dataset, num_epochs=2):
start_time = time.perf_counter()
for epoch_num in range(num_epochs):
for i, sample in enumerate(dataset.as_numpy_iterator()):
# time.sleep(0.001) # Performing a training step
if i == 0:
print(sample[1])
print('Execution time:', time.perf_counter() - start_time)