<s> from itertools import islice <EOL> import numpy as np <EOL> def grouper ( iterable , n ) : <EOL> """<STR_LIT>""" <EOL> it = iter ( iterable ) <EOL> while True : <EOL> chunk = tuple ( islice ( it , n ) ) <EOL> if not chunk : <EOL> return <EOL> yield chunk <EOL> def gen_minibatch_idx ( n , n_per_mb ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( n , int ) <EOL> all_idx = np . arange ( n ) <EOL> np . random . shuffle ( all_idx ) <EOL> for idx in grouper ( all_idx , n_per_mb ) : <EOL> yield idx <EOL> def gen_minibatch ( arr1 , arr2 = None , n_per_mb = None ) : <EOL> """<STR_LIT>""" <EOL> if arr2 is not None and n_per_mb is None and isinstance ( arr2 , int ) : <EOL> n_per_mb , arr2 = arr2 , n_per_mb <EOL> n = len ( arr1 ) <EOL> if arr2 is None : <EOL> for idx in gen_minibatch_idx ( n , n_per_mb ) : <EOL> yield arr1 [ idx , : ] <EOL> else : <EOL> assert arr1 . shape [ <NUM_LIT:0> ] == arr2 . shape [ <NUM_LIT:0> ] <EOL> for idx in gen_minibatch_idx ( n , n_per_mb ) : <EOL> yield arr1 [ idx , : ] , arr2 [ idx , : ] </s>
