Skip to content

Instantly share code, notes, and snippets.

@yurkor
Created July 13, 2016 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurkor/1816f025161a65ed54bd1803c7b7766c to your computer and use it in GitHub Desktop.
Save yurkor/1816f025161a65ed54bd1803c7b7766c to your computer and use it in GitHub Desktop.
def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
assert len(inputs) == len(targets)
while True:
if shuffle:
indices = np.arange(len(inputs))
np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
if shuffle:
excerpt = indices[start_idx:start_idx + batchsize]
else:
excerpt = slice(start_idx, start_idx + batchsize)
yield inputs[excerpt], targets[excerpt]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment