@@ -269,156 +269,3 @@ def ptb_iterator(raw_data, batch_size, num_steps):
269269 x = data [:, i * num_steps :(i + 1 ) * num_steps ]
270270 y = data [:, i * num_steps + 1 :(i + 1 ) * num_steps + 1 ]
271271 yield (x , y )
272-
273-
274- # def minibatches_for_sequence2D(inputs, targets, batch_size, sequence_length, stride=1):
275- # """
276- # Input a group of example in 2D numpy.array and their labels.
277- # Return the examples and labels by the given batchsize, sequence_length.
278- # Use for RNN.
279- #
280- # Parameters
281- # ----------
282- # inputs : numpy.array
283- # (X) The input features, every row is a example.
284- # targets : numpy.array
285- # (y) The labels of inputs, every row is a example.
286- # batchsize : int
287- # The batch size must be a multiple of sequence_length: int(batch_size % sequence_length) == 0
288- # sequence_length : int
289- # The sequence length
290- # stride : int
291- # The stride step
292- #
293- # Examples
294- # --------
295- # >>> sequence_length = 2
296- # >>> batch_size = 4
297- # >>> stride = 1
298- # >>> X_train = np.asarray([[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18],[19,20,21],[22,23,24]])
299- # >>> y_train = np.asarray(['0','1','2','3','4','5','6','7'])
300- # >>> print('X_train = %s' % X_train)
301- # >>> print('y_train = %s' % y_train)
302- # >>> for batch in minibatches_for_sequence2D(X_train, y_train, batch_size=batch_size, sequence_length=sequence_length, stride=stride):
303- # >>> inputs, targets = batch
304- # >>> print(inputs)
305- # >>> print(targets)
306- # ... [[ 1. 2. 3.]
307- # ... [ 4. 5. 6.]
308- # ... [ 4. 5. 6.]
309- # ... [ 7. 8. 9.]]
310- # ... [1 2]
311- # ... [[ 4. 5. 6.]
312- # ... [ 7. 8. 9.]
313- # ... [ 7. 8. 9.]
314- # ... [ 10. 11. 12.]]
315- # ... [2 3]
316- # ... ...
317- # ... [[ 16. 17. 18.]
318- # ... [ 19. 20. 21.]
319- # ... [ 19. 20. 21.]
320- # ... [ 22. 23. 24.]]
321- # ... [6 7]
322- # """
323- # print('len(targets)=%d batch_size=%d sequence_length=%d stride=%d' % (len(targets), batch_size, sequence_length, stride))
324- # assert len(inputs) == len(targets), '1 feature vector have 1 target vector/value' #* sequence_length
325- # # assert int(batch_size % sequence_length) == 0, 'batch_size % sequence_length must == 0\
326- # # batch_size is number of examples rather than number of targets'
327- #
328- # # print(inputs.shape, len(inputs), len(inputs[0]))
329- #
330- # n_targets = int(batch_size/sequence_length)
331- # # n_targets = int(np.ceil(batch_size/sequence_length))
332- # X = np.empty(shape=(0,len(inputs[0])), dtype=np.float32)
333- # y = np.zeros(shape=(1, n_targets), dtype=np.int32)
334- #
335- # for idx in range(sequence_length, len(inputs), stride): # go through all example during 1 epoch
336- # for n in range(n_targets): # for num of target
337- # X = np.concatenate((X, inputs[idx-sequence_length+n:idx+n]))
338- # y[0][n] = targets[idx-1+n]
339- # # y = np.vstack((y, targets[idx-1+n]))
340- # yield X, y[0]
341- # X = np.empty(shape=(0,len(inputs[0])))
342- # # y = np.empty(shape=(1,0))
343- #
344- #
345- # def minibatches_for_sequence4D(inputs, targets, batch_size, sequence_length, stride=1): #
346- # """
347- # Input a group of example in 4D numpy.array and their labels.
348- # Return the examples and labels by the given batchsize, sequence_length.
349- # Use for RNN.
350- #
351- # Parameters
352- # ----------
353- # inputs : numpy.array
354- # (X) The input features, every row is a example.
355- # targets : numpy.array
356- # (y) The labels of inputs, every row is a example.
357- # batchsize : int
358- # The batch size must be a multiple of sequence_length: int(batch_size % sequence_length) == 0
359- # sequence_length : int
360- # The sequence length
361- # stride : int
362- # The stride step
363- #
364- # Examples
365- # --------
366- # >>> sequence_length = 2
367- # >>> batch_size = 2
368- # >>> stride = 1
369- # >>> X_train = np.asarray([[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18],[19,20,21],[22,23,24]])
370- # >>> y_train = np.asarray(['0','1','2','3','4','5','6','7'])
371- # >>> X_train = np.expand_dims(X_train, axis=1)
372- # >>> X_train = np.expand_dims(X_train, axis=3)
373- # >>> for batch in minibatches_for_sequence4D(X_train, y_train, batch_size=batch_size, sequence_length=sequence_length, stride=stride):
374- # >>> inputs, targets = batch
375- # >>> print(inputs)
376- # >>> print(targets)
377- # ... [[[[ 1.]
378- # ... [ 2.]
379- # ... [ 3.]]]
380- # ... [[[ 4.]
381- # ... [ 5.]
382- # ... [ 6.]]]]
383- # ... [1]
384- # ... [[[[ 4.]
385- # ... [ 5.]
386- # ... [ 6.]]]
387- # ... [[[ 7.]
388- # ... [ 8.]
389- # ... [ 9.]]]]
390- # ... [2]
391- # ... ...
392- # ... [[[[ 19.]
393- # ... [ 20.]
394- # ... [ 21.]]]
395- # ... [[[ 22.]
396- # ... [ 23.]
397- # ... [ 24.]]]]
398- # ... [7]
399- # """
400- # print('len(targets)=%d batch_size=%d sequence_length=%d stride=%d' % (len(targets), batch_size, sequence_length, stride))
401- # assert len(inputs) == len(targets), '1 feature vector have 1 target vector/value' #* sequence_length
402- # # assert int(batch_size % sequence_length) == 0, 'in LSTM, batch_size % sequence_length must == 0\
403- # # batch_size is number of X_train rather than number of targets'
404- # assert stride >= 1, 'stride must be >=1, at least move 1 step for each iternation'
405- #
406- # n_example, n_channels, width, height = inputs.shape
407- # print('n_example=%d n_channels=%d width=%d height=%d' % (n_example, n_channels, width, height))
408- #
409- # n_targets = int(np.ceil(batch_size/sequence_length)) # 实际为 batchsize/sequence_length + 1
410- # print(n_targets)
411- # X = np.zeros(shape=(batch_size, n_channels, width, height), dtype=np.float32)
412- # # X = np.zeros(shape=(n_targets, sequence_length, n_channels, width, height), dtype=np.float32)
413- # y = np.zeros(shape=(1,n_targets), dtype=np.int32)
414- # # y = np.empty(shape=(0,1), dtype=np.float32)
415- # # time.sleep(2)
416- # for idx in range(sequence_length, n_example-n_targets+2, stride): # go through all example during 1 epoch
417- # for n in range(n_targets): # for num of target
418- # # print(idx+n, inputs[idx-sequence_length+n : idx+n].shape)
419- # X[n*sequence_length : (n+1)*sequence_length] = inputs[idx+n-sequence_length : idx+n]
420- # # X[n] = inputs[idx-sequence_length+n:idx+n]
421- # y[0][n] = targets[idx+n-1]
422- # # y = np.vstack((y, targets[idx-1+n]))
423- # # y = targets[idx: idx+n_targets]
424- # yield X, y[0]
0 commit comments