Skip to content

Instantly share code, notes, and snippets.

@alxndrkalinin
Last active April 13, 2018 19:32
Show Gist options
  • Save alxndrkalinin/6cc4228e9178ec4af7b2696a0d1ad5a1 to your computer and use it in GitHub Desktop.
Save alxndrkalinin/6cc4228e9178ec4af7b2696a0d1ad5a1 to your computer and use it in GitHub Desktop.
[bug] keras.fit_generator(Sequence)
import numpy as np
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Activation
from keras.utils.data_utils import Sequence
from keras.losses import categorical_crossentropy
from keras.optimizers import SGD
class CustomSequence(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.X,self.y = x_set,y_set
self.batch_size = batch_size
self.counter = 0
self.epoch_counter = 0
def __len__(self):
return len(self.X) // self.batch_size
def __getitem__(self,idx):
print '\nBatch #: ' + str(idx)
print 'From ' + str(idx*self.batch_size) + ' to ' + str((idx+1)*self.batch_size)
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]
print 'Returned batch size: ' + str(len(batch_x))
self.counter += 1
return np.array(batch_x), np.array(batch_y)
def on_epoch_end(self):
"""Method called at the end of every epoch.
"""
print('\nEpoch end: ' + str(self.epoch_counter) + ' counter: ' + str(self.counter))
self.epoch_counter +=1
model = Sequential()
model.add(Dense(units=5, input_dim=3))
model.add(Activation('relu'))
model.add(Dense(units=2))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
x_train = np.random.rand(20,3)
y_train = np.random.randint(2, size=20)
y_train = np.vstack((y_train, 1-y_train)).T
print x_train.shape
print y_train.shape
batch_size = 4
epochs = 3
train_generator = CustomSequence(x_train, y_train, batch_size)
model.fit_generator(
train_generator,
steps_per_epoch = 5,
epochs = epochs,
use_multiprocessing=False,
workers=1,
verbose=0
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment