Skip to content

Instantly share code, notes, and snippets.

@marta-sd
Created November 29, 2017 08:18
Show Gist options
  • Save marta-sd/55a7b8860d440ba1113c8f81810e40a0 to your computer and use it in GitHub Desktop.
Save marta-sd/55a7b8860d440ba1113c8f81810e40a0 to your computer and use it in GitHub Desktop.
Data generators
from random import shuffle
def example_generator(*lists, random_order=True):
"""Create infinite generator of examples.
example_generator([3, 6, 8], [0, 0, 1]) -> (6,0), (3,0), (8,1), (3,0)...
"""
assert len(lists) > 0, 'no lists provided'
length = len(lists[0])
assert length > 0, 'empty lists provided'
for i, l in enumerate(lists):
assert len(l) == length, ('list %s has different length'
' (%s instead of %s)' % (i, len(l), length))
examples = list(zip(*lists))
while True:
if random_order:
shuffle(examples)
for example in examples:
yield example
def batch_generator(examples, batch_size=5, num_batches=200000):
"""Group examples into batches (expects generator)"""
for _ in range(num_batches):
batch = []
for _ in range(batch_size):
batch.append(next(examples))
yield list(zip(*batch))
import generators
X1 = range(10)
X2 = range(50, 60)
Y = [0]*5 + [1]*5
print('original order')
examples = generators.example_generator(X1, X2, Y, random_order=False)
for _ in range(15):
print(next(examples))
print()
print('random order')
examples = generators.example_generator(X1, X2, Y)
for _ in range(15):
print(next(examples))
print()
print('in batches')
batches = generators.batch_generator(examples, batch_size=4, num_batches=10)
for b in batches:
print(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment