Skip to content

Instantly share code, notes, and snippets.

@sdcubber
Last active September 15, 2018 12:44
Show Gist options
  • Save sdcubber/9acac44f634a89a123486966758d3b67 to your computer and use it in GitHub Desktop.
Save sdcubber/9acac44f634a89a123486966758d3b67 to your computer and use it in GitHub Desktop.
from keras.utils import Sequence
import numpy as np
class MySequence(Sequence):
"""Custom Sequence"""
def __init__(self, x, y, batch_size):
self.x, self.y = x, y
self.batch_size = batch_size
def __len__(self):
return int(np.ceil(len(self.x) / float(self.batch_size)))
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
return batch_x, batch_y
training_generator = MySequence(train_images, train_labels, batch_size=128)
validation_generator = MySequence(val_images, val_labels, batch_size=128)
network.fit_generator(generator=training_generator, epochs=5, validation_data=validation_generator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment