You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2017/12/19 15:27:50 UTC
[GitHub] firstdeathmaker commented on issue #8749: [FIX ATTACHED] Not working example on Iterators tutorial - SimpleIter
firstdeathmaker commented on issue #8749: [FIX ATTACHED] Not working example on Iterators tutorial - SimpleIter
URL: https://github.com/apache/incubator-mxnet/issues/8749#issuecomment-352791952
I found the error (if it only appears when you use Python 3.x instead of Python 2): The behaviour of the zip function of python was changed. In python 3, you get a one-time iterator. Therefore, you cannot reuse it. You can change the example code of SimpleIter this way to get it to work in Python 3 (it will create a new zip-iterator when calling the properties and uses a call to that property in next() intead of calling the internal one:
`class SimpleIter(mx.io.DataIter):
def __init__(self, data_names, data_shapes, data_gen,
label_names, label_shapes, label_gen, num_batches=10):
self._data_names = data_names
self._data_shapes = data_shapes
self._label_names = label_names
self._label_shapes = label_shapes
self.num_batches = num_batches
self.data_gen = data_gen
self.label_gen = label_gen
self.cur_batch = 0
def __iter__(self):
return self
def reset(self):
self.cur_batch = 0
def __next__(self):
return self.next()
@property
def provide_data(self):
return zip(self._data_names, self._data_shapes)
@property
def provide_label(self):
return zip(self._label_names, self._label_shapes)
def next(self):
if self.cur_batch < self.num_batches:
self.cur_batch += 1
data = [mx.nd.array(g(d[1])) for d,g in zip(self.provide_data, self.data_gen)]
label = [mx.nd.array(g(d[1])) for d,g in zip(self.provide_label, self.label_gen)]
return mx.io.DataBatch(data, label)
else:
raise StopIteration`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services