forked from probml/pyprobml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batcher.py
265 lines (217 loc) · 7.51 KB
/
batcher.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
# Sometimes (eg when using JAX) we want to have an infinite stream
# of numpy minibatches. Here are some handy functions for this.
import numpy as np
USE_TORCH = True
USE_TF = True
if USE_TORCH:
import torch
print("torch version {}".format(torch.__version__))
if torch.cuda.is_available():
print(torch.cuda.get_device_name(0))
print("current device {}".format(torch.cuda.current_device()))
else:
print("Torch cannot find GPU")
if USE_TF:
import tensorflow as tf
print("tf version {}".format(tf.__version__))
if tf.test.is_gpu_available():
print(tf.test.gpu_device_name())
else:
print("TF cannot find GPU")
class NumpyBatcher():
def __init__(self, X, y, batch_size, shuffle=False):
self.num_data = X.shape[0]
num_complete_batches, leftover = divmod(self.num_data, batch_size)
self.num_batches = num_complete_batches + bool(leftover)
self.X = X
self.y = y
self.batch_size = batch_size
self.shuffle = shuffle
self.generator = self._make_data_stream()
def _make_data_stream(self):
while True:
if self.shuffle:
perm = np.random.permutation(self.num_data)
else:
perm = range(self.num_data)
for i in range(self.num_batches):
batch_idx = perm[i * self.batch_size:(i + 1) * self.batch_size]
yield self.X[batch_idx], self.y[batch_idx]
# Test the Batcher
N_train = 5
D = 4
np.random.seed(0)
X_train = np.random.randn(N_train, D)
y_train = np.random.randn(N_train, 1)
batch_size = 2
# If we know how much of the stream we want
train_iterator = NumpyBatcher(X_train, y_train, batch_size).generator
num_minibatches = 4
print("read fixed number")
for step in range(num_minibatches):
batch = next(train_iterator)
x, y = batch
print(y)
# If we want to keep reading the stream until we meet a stopping criterion
train_iterator = NumpyBatcher(X_train, y_train, batch_size).generator
step = 0
print("read till had enough")
for batch in train_iterator:
x, y = batch
print(y)
step = step + 1
if step >= num_minibatches:
break
##########
# Pytorch version
# https://stanford.edu/~shervine/blog/pytorch-how-to-generate-data-parallel
from torch.utils.data import DataLoader, TensorDataset
np.random.seed(0)
train_set = TensorDataset(torch.Tensor(X_train), torch.Tensor(y_train))
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False)
print("One epoch")
for step, (x,y) in enumerate(train_loader):
print(y)
# If we try to read past the end of the dataset, the loop just terminates.
step = 0
for batch in train_loader:
x, y = batch
print(y)
step = step + 1
if step >= num_minibatches:
break
# DataLoader is not an iterator, and does not support next()
try:
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False)
for step in range(num_minibatches):
batch = next(train_loader)
x, y = batch
print(y)
except Exception as e:
print(e)
# It can converted to an iterator. But if we try to read past ened of
# the dataset, we get a stopIteration error.
try:
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False)
train_iterator = iter(train_loader)
for step in range(num_minibatches):
batch = next(train_iterator)
x, y = batch
print(y)
except Exception as e:
print(e)
############
# Use pre-canned dataset
class FlattenAndCast(object):
def __call__(self, pic):
return np.ravel(np.array(pic, dtype=np.float32))
import torchvision.datasets as datasets
mnist_dataset = datasets.MNIST('/tmp/mnist/', transform=FlattenAndCast())
mnist_dataset = datasets.MNIST('/tmp/mnist/')
training_generator = DataLoader(mnist_dataset, batch_size=batch_size, num_workers=0)
print("MNIST labels")
step = 0
for batch in training_generator:
if step >= num_minibatches:
break
x, y = batch
y = y.numpy()
print(y)
step = step + 1
training_iterator = iter(training_generator)
print("MNIST labels")
for step in range(num_minibatches):
batch = next(training_iterator)
x, y = batch
print(y)
##############
# Lets make an Infinite stream
#https://gist.github.com/MFreidank/821cc87b012c53fade03b0c7aba13958
class InfiniteDataLoader(DataLoader):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Initialize an iterator over the dataset.
self.dataset_iterator = super().__iter__()
def __iter__(self):
return self
def __next__(self):
try:
batch = next(self.dataset_iterator)
except StopIteration:
# Dataset exhausted, use a new fresh iterator.
self.dataset_iterator = super().__iter__()
batch = next(self.dataset_iterator)
return batch
train_loader_infinite = InfiniteDataLoader(train_set, batch_size=batch_size, shuffle=False)
step = 0
print("read till done")
for batch in train_loader_infinite:
x, y = batch
print(y)
step = step + 1
if step >= num_minibatches:
break
print("read fixed number")
train_loader_infinite = InfiniteDataLoader(train_set, batch_size=batch_size, shuffle=False)
for step in range(num_minibatches):
batch = next(train_loader_infinite)
x, y = batch
print(y)
############
# Make Torch DataLoader return numpy arrays instead of Tensors.
# https://github.com/google/jax/blob/master/notebooks/neural_network_and_data_loading.ipynb
def numpy_collate(batch):
print('collate')
print(batch)
if isinstance(batch[0], np.ndarray):
return np.stack(batch)
elif isinstance(batch[0], (tuple,list)):
transposed = zip(*batch)
return [numpy_collate(samples) for samples in transposed]
else:
return np.array(batch)
class NumpyLoader(DataLoader):
def __init__(self, dataset, batch_size=1,
shuffle=False, sampler=None,
batch_sampler=None, num_workers=0,
pin_memory=False, drop_last=False,
timeout=0, worker_init_fn=None):
super(self.__class__, self).__init__(dataset,
batch_size=batch_size,
shuffle=shuffle,
sampler=sampler,
batch_sampler=batch_sampler,
num_workers=num_workers,
collate_fn=numpy_collate,
pin_memory=pin_memory,
drop_last=drop_last,
timeout=timeout,
worker_init_fn=worker_init_fn)
N = 5; D = 2;
X = np.random.randn(N,D)
y = np.random.rand(N)
data_set = TensorDataset(torch.Tensor(X), torch.Tensor(y))
data_loader = DataLoader(data_set, batch_size=2, shuffle=False)
for step, (x,y) in enumerate(data_loader):
print(y)
numpy_loader = NumpyLoader(data_set, batch_size=2, shuffle=False)
for step, (x,y) in enumerate(numpy_loader):
print(y)
train_loader_numpy = NumpyLoader(train_set, batch_size=batch_size, shuffle=False)
print("One epoch")
for step, (x,y) in enumerate(train_loader_numpy):
print(y)
#################
# TF gives us an infinite stream of minibatches, all with the same size.
dataset = tf.data.Dataset.from_tensor_slices({"X": X_train, "y": y_train})
batches = dataset.repeat().batch(batch_size)
step = 0
for batch in batches:
if step >= num_minibatches:
break
x, y = batch["X"].numpy(), batch["y"].numpy()
print(y)
step = step + 1
# Pre-canned datasets.
import tensorflow_datasets as tfds
dataset = tfds.load(name="mnist", split=tfds.Split.TRAIN)