# HG changeset patch # User bengioy@bengiomac.local # Date 1206326683 14400 # Node ID 8039918516fe6bb27abd2199b24d27f724df53c3 # Parent f7dcfb5f9d5b6e602e227b7a1f6fc4df851639da Added MinibatchIterator diff -r f7dcfb5f9d5b -r 8039918516fe dataset.py --- a/dataset.py Sun Mar 23 22:14:10 2008 -0400 +++ b/dataset.py Sun Mar 23 22:44:43 2008 -0400 @@ -56,6 +56,29 @@ """dataset[i:j] returns the subdataset with examples i,i+1,...,j-1.""" raise NotImplementedError + def minibatches(self,minibatch_size): + """Return an iterator for the dataset that goes through minibatches of the given size.""" + return MinibatchIterator(self,minibatch_size) + +class MinibatchIterator(object): + """ + Iterator class for FiniteDataSet that can iterate by minibatches + (sub-dataset of consecutive examples). + """ + def __init__(self,dataset,minibatch_size): + assert minibatch_size>0 and minibatch_size=len(self.dataset): + self.current=-self.minibatchsize + raise StopIteration + return self.dataset[self.current:self.current+self.minibatchsize] + # we may want ArrayDataSet defined in another python file import numpy @@ -197,4 +220,4 @@ c+=slice_width return result return self.data - +