-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathutils.py
318 lines (278 loc) · 13.8 KB
/
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
310
311
312
313
314
315
316
317
318
import numpy as np
from fuel.datasets import IterableDataset
from fuel.streams import DataStream
import theano
from fuel.transformers import Transformer
import fuel
floatX = theano.config.floatX
#class SampleDrops(Transformer):
#def __init__(self, data_stream, drop_prob, hidden_dim,
#is_for_test, **kwargs):
#super(SampleDrops, self).__init__(
#data_stream, **kwargs)
#self.drop_prob = drop_prob
#self.hidden_dim = hidden_dim
#self.is_for_test = is_for_test
#self.produces_examples = False
#def get_data(self, request=None):
#data = next(self.child_epoch_iterator)
#transformed_data = []
#transformed_data.append(data[0])
#transformed_data.append(data[1])
#T, B, _ = data[1].shape
#if self.is_for_test:
#drops = np.ones((T, B, self.hidden_dim)) * self.drop_prob
#else:
#drops = np.random.binomial(n=1, p=self.drop_prob,
#size=(T, B, self.hidden_dim))
#transformed_data.append(drops.astype(floatX))
#return transformed_data
#def get_seq_mnist_streams(hidden_dim, batch_size=100, drop_prob=0.5):
#permutation = np.random.randint(0, 784, size=(784,))
#train_set, valid_set, test_set = load_data('mnist.pkl.gz')
#train_x = train_set[0].reshape((50000 / batch_size, batch_size, 784))
#train_x = np.swapaxes(train_x, 2, 1)
#train_x = train_x[:, :, :, np.newaxis]
## Now the dimension is num_batches x 784 x batch_size x 1
#train_y = (np.zeros(train_set[0].shape) - 1)
## label for each time-step is -1 and for the last one is the real label
#train_y[:, -1] = train_set[1]
#train_y = train_y.reshape((50000 / batch_size, batch_size, 784))
#train_y = np.swapaxes(train_y, 2, 1)
#train_y = train_y[:, :, :, np.newaxis]
## Now the dimension is num_batches x 784 x batch_size x 1
#valid_x = valid_set[0].reshape((10000 / batch_size, batch_size, 784))
#valid_x = np.swapaxes(valid_x, 2, 1)
#valid_x = valid_x[:, :, :, np.newaxis]
## Now the dimension is num_batches x 784 x batch_size x 1
#valid_y = (np.zeros(valid_set[0].shape) - 1)
## label for each time-step is -1 and for the last one is the real label
#valid_y[:, -1] = valid_set[1]
#valid_y = valid_y.reshape((10000 / batch_size, batch_size, 784))
#valid_y = np.swapaxes(valid_y, 2, 1)
#valid_y = valid_y[:, :, :, np.newaxis]
## Now the dimension is num_batches x 784 x batch_size x 1
#train_x = train_x[:, permutation]
#valid_x = valid_x[:, permutation]
#train = IterableDataset({'x': train_x.astype(floatX),
#'y': train_y[:, -1, :, 0].astype('int32')})
#train_stream = DataStream(train)
#train_stream = SampleDrops(train_stream, drop_prob, hidden_dim, False)
#train_stream.sources = ('y', 'x', 'drops')
#train_stream.get_epoch_iterator().next()
#valid = IterableDataset({'x': valid_x.astype(floatX),
#'y': valid_y[:, -1, :, 0].astype('int32')})
#valid_stream = DataStream(valid)
#valid_stream = SampleDrops(valid_stream, drop_prob, hidden_dim, True)
#valid_stream.sources = ('y', 'x', 'drops')
#return train_stream, valid_stream
#def get_dataset(which_set):
#MNIST = fuel.datasets.MNIST
## jump through hoops to instantiate only once and only if needed
#_datasets = dict(
#train=MNIST(which_sets=["train"], subset=slice(None, 50000)),
#valid=MNIST(which_sets=["train"], subset=slice(50000, None)),
#test=MNIST(which_sets=["test"]))
#return _datasets[which_set]
#def get_stream(which_set, batch_size, drop_prob,
#hidden_dim, for_evaluation, num_examples=None):
#np.random.seed(seed=1)
#permutation = np.random.randint(0, 784, size=(784,))
#dataset = get_dataset(which_set)
#if num_examples is None or num_examples > dataset.num_examples:
#num_examples = dataset.num_examples
#stream = fuel.streams.DataStream.default_stream(
#dataset,
#iteration_scheme=fuel.schemes.ShuffledScheme(num_examples, batch_size))
#ds = SampleDrops2(stream, drop_prob, hidden_dim,
#for_evaluation, permutation)
#ds.sources = ('x', 'y', 'drops')
#return ds
#class SampleDrops2(Transformer):
#def __init__(self, data_stream, drop_prob, hidden_dim,
#is_for_test, permutation, **kwargs):
#super(SampleDrops2, self).__init__(
#data_stream, **kwargs)
#self.drop_prob = drop_prob
#self.hidden_dim = hidden_dim
#self.is_for_test = is_for_test
#self.produces_examples = False
#self.permutation = permutation
#def get_data(self, request=None):
#data = next(self.child_epoch_iterator)
#transformed_data = []
#transformed_data.append(
#np.swapaxes(data[0].reshape(data[0].shape[0], -1),
#0, 1)[self.permutation, :, np.newaxis])
#transformed_data.append(data[1][:, 0])
#T, B, _ = transformed_data[0].shape
#if self.is_for_test:
#drops = np.ones((T, B, self.hidden_dim)) * self.drop_prob
#else:
#drops = np.random.binomial(n=1, p=self.drop_prob,
#size=(T, B, self.hidden_dim))
#transformed_data.append(drops.astype(floatX))
#return transformed_data
# PTB
_data_cache = dict()
def get_data(which_set):
if which_set not in _data_cache:
path = '../wtfcptb/char_level_penntree.npz'
data = np.load(path)
# put the entire thing on GPU in one-hot (takes
# len(self.vocab) * len(self.data) * sizeof(floatX) bytes
# which is about 1G for the training set and less for the
# other sets)
#cudandarray = theano.sandbox.cuda.cuda_ndarray.cuda_ndarray.CudaNdarray
# (doing it in numpy first because cudandarray doesn't accept
# lists of indices)
one_hot_data = np.eye(len(data["vocab"]), dtype=theano.config.floatX)[data[which_set]]
#_data_cache[which_set] = cudandarray(one_hot_data)
return one_hot_data#_data_cache[which_set]
class PTB(fuel.datasets.Dataset):
provides_sources = ('features',)
example_iteration_scheme = None
def __init__(self, which_set, length, augment=True):
self.which_set = which_set
self.length = length
self.augment = augment
self.data = get_data(which_set)
self.num_examples = int(len(self.data) / self.length)
if self.augment:
# -1 so we have one self.length worth of room for augmentation
self.num_examples -= 1
super(PTB, self).__init__()
def open(self):
offset = 0
if self.augment:
# choose an offset to get some data augmentation by
# not always chopping the examples at the same point.
offset = np.random.randint(self.length)
# none of this should copy
data = self.data[offset:]
# reshape to nonoverlapping examples
data = (data[:self.num_examples * self.length]
.reshape((self.num_examples, self.length, self.data.shape[1])))
# return the data so we will get it as the "state" argument to get_data
return data
def get_data(self, state, request):
if isinstance(request, (tuple, list)):
request = np.array(request, dtype=np.int64)
return (state.take(request, 0),)
return (state[request],)
class SampleDropsPTB(Transformer):
def __init__(self, data_stream, drop_prob_states, drop_prob_cells, drop_prob_igates, hidden_dim,
is_for_test, **kwargs):
super(SampleDropsPTB, self).__init__(
data_stream, **kwargs)
self.drop_prob_states = drop_prob_states
self.drop_prob_cells = drop_prob_cells
self.drop_prob_igates = drop_prob_igates
self.hidden_dim = hidden_dim
self.is_for_test = is_for_test
self.produces_examples = False
def get_data(self, request=None):
data = next(self.child_epoch_iterator)
transformed_data = []
# Now it is: T x B x F
transformed_data.append(np.swapaxes(data[0], 0, 1))#[:-1])
#transformed_data.append(np.swapaxes(data[0], 0, 1)[1:])
T, B, _ = transformed_data[0].shape
if self.is_for_test:
drops_states = np.ones((T, B, self.hidden_dim)) * self.drop_prob_states
else:
drops_states = np.random.binomial(n=1, p=self.drop_prob_states,
size=(T, B, self.hidden_dim))
if self.is_for_test:
drops_cells = np.ones((T, B, self.hidden_dim)) * self.drop_prob_cells
else:
drops_cells = np.random.binomial(n=1, p=self.drop_prob_cells,
size=(T, B, self.hidden_dim))
if self.is_for_test:
drops_igates = np.ones((T, B, self.hidden_dim)) * self.drop_prob_igates
else:
drops_igates = np.random.binomial(n=1, p=self.drop_prob_igates,
size=(T, B, self.hidden_dim))
transformed_data.append(drops_states.astype(floatX))
transformed_data.append(drops_cells.astype(floatX))
transformed_data.append(drops_igates.astype(floatX))
return transformed_data
class Sample_static_mask_DropsPTB(Transformer):
def __init__(self, data_stream, drop_prob_states, drop_prob_cells, drop_prob_igates, hidden_dim,
is_for_test, seq_len, batch_size, **kwargs):
super(Sample_static_mask_DropsPTB, self).__init__(
data_stream, **kwargs)
self.drop_prob_states = drop_prob_states
self.drop_prob_cells = drop_prob_cells
self.drop_prob_igates = drop_prob_igates
self.hidden_dim = hidden_dim
self.T = seq_len
#self.B = batch_size
self.is_for_test = is_for_test
self.produces_examples = False
#self.drops_states_mask = np.tile(np.random.binomial(n=1, p=self.drop_prob_states,
#size=(self.T, 1, self.hidden_dim)), (1, self.B, 1))
#self.drops_cells_mask = np.tile(np.random.binomial(n=1, p=self.drop_prob_cells,
#size=(self.T, 1, self.hidden_dim)), (1, self.B, 1))
#self.drops_igates_mask = np.tile(np.random.binomial(n=1, p=self.drop_prob_igates,
#size=(self.T, 1, self.hidden_dim)), (1, self.B, 1))
self.drops_states_mask = np.random.binomial(n=1, p=self.drop_prob_states,
size=(self.T, 1, self.hidden_dim))
self.drops_cells_mask = np.random.binomial(n=1, p=self.drop_prob_cells,
size=(self.T, 1, self.hidden_dim))
self.drops_igates_mask = np.random.binomial(n=1, p=self.drop_prob_igates,
size=(self.T, 1, self.hidden_dim))
def get_data(self, request=None):
data = next(self.child_epoch_iterator)
transformed_data = []
# Now it is: T x B x F
transformed_data.append(np.swapaxes(data[0], 0, 1))#[:-1])
#transformed_data.append(np.swapaxes(data[0], 0, 1)[1:])
_T, _B, _ = transformed_data[0].shape
assert self.T == _T
#assert self.B == _B
drops_states = np.tile(self.drops_states_mask, (1, _B, 1))
drops_cells = np.tile(self.drops_cells_mask, (1, _B, 1))
drops_igates = np.tile(self.drops_igates_mask, (1, _B, 1))
#if self.is_for_test:
#drops_states = np.ones((self.T, _B, self.hidden_dim)) * np.tile(self.drop_prob_states, (1, _B, 1))
#else:
#drops_states = self.drops_states_mask
#if self.is_for_test:
#drops_cells = np.ones((self.T, _B, self.hidden_dim)) * np.tile(self.drop_prob_cells, (1, _B, 1))
#else:
#drops_cells = self.drops_cells_mask
#if self.is_for_test:
#drops_igates = np.ones((self.T, _B, self.hidden_dim)) * np.tile(self.drop_prob_igates, (1, _B, 1))
#else:
#drops_igates = self.drops_igates_mask
transformed_data.append(drops_states.astype(floatX))
transformed_data.append(drops_cells.astype(floatX))
transformed_data.append(drops_igates.astype(floatX))
return transformed_data
def get_ptb_stream(which_set, batch_size, length, drop_prob_states, drop_prob_cells, drop_prob_igates,
hidden_dim, for_evaluation, num_examples=None,
augment=True):
dataset = PTB(which_set, length=length, augment=augment)
if num_examples is None or num_examples > dataset.num_examples:
num_examples = dataset.num_examples
stream = fuel.streams.DataStream.default_stream(
dataset,
iteration_scheme=fuel.schemes.ShuffledScheme(num_examples, batch_size))
ds = SampleDropsPTB(stream, drop_prob_states, drop_prob_cells, drop_prob_igates, hidden_dim,
for_evaluation)
ds.sources = ('features', 'drops_states', 'drops_cells', 'drops_igates')#'outputs',
return ds
def get_static_mask_ptb_stream(which_set, batch_size, length, drop_prob_states, drop_prob_cells, drop_prob_igates,
hidden_dim, for_evaluation, num_examples=None,
augment=True):
dataset = PTB(which_set, length=length, augment=augment)
if num_examples is None or num_examples > dataset.num_examples:
num_examples = dataset.num_examples
stream = fuel.streams.DataStream.default_stream(
dataset,
iteration_scheme=fuel.schemes.ShuffledScheme(num_examples, batch_size))
ds = Sample_static_mask_DropsPTB(stream, drop_prob_states, drop_prob_cells, drop_prob_igates, hidden_dim,
for_evaluation, length, batch_size)
ds.sources = ('features', 'drops_states', 'drops_cells', 'drops_igates')#'outputs',
return ds