Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide mini-batch iterator #69

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions nolearn/lasagne.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ def __iter__(self):
def transform(self, Xb, yb):
return Xb, yb

class MiniBatchIterator(BatchIterator):
def __init__(self, batch_size = 128, iterations = 32):
BatchIterator.__init__(self, batch_size)
self.iterations = iterations
self.X = None
self.y = None
self.cidx = 0
self.midx = 0

def __call__(self, X, y = None):
# if data set is reset
if not (self.X is X and self.y is y):
self.cidx = 0
n_samples = X.shape[0]
bs = self.batch_size
self.midx = (n_samples + bs - 1) // bs
self.X, self.y = X, y
return self

def __iter__(self):
bs = self.batch_size
for i in range(0, self.iterations):
sl = slice(self.cidx * bs , (self.cidx + 1) * bs)
self.cidx += 1
# wrap up.
if self.cidx >= self.midx: self.cidx = 0
Xb = self.X[sl]
if self.y is not None:
yb = self.y[sl]
else:
yb = None
yield self.transform(Xb, yb)

class NeuralNet(BaseEstimator):
"""A scikit-learn estimator based on Lasagne.
Expand Down