Search code examples
chainer

chainer StandardUpdater iterator Parameters


in the chainer doc, it shows in https://docs.chainer.org/en/stable/reference/core/generated/chainer.training.StandardUpdater.html#chainer.training.StandardUpdater

Parameters: iterator – Dataset iterator for the training dataset. It can also be a dictionary that maps strings to iterators. If this is just an iterator, then the iterator is registered by the name 'main'.

but actually in the chainer's code, i hava found

def update_core(self):
    batch = self._iterators['main'].next()

it means that it only use the iterator dict by the name 'main'?


Solution

  • Yes, as default StandardUpdater only uses 'main' iterator.

    I think the functionality of multiple iterator can be useful only when you are defining your own Updater class which is subclass of StandardUpdater.

    For example:

    import chainer
    from chainer import training
    
    class MyUpdater(training.updaters.StandardUpdater):
    
        # override `update_core`
        def update_core(self):
            # You can use several iterators here!
            batch0 = self.get_iterator('0').next()
            batch1 = self.get_iterator('1').next()
    
            # do what ever you want with `batch0` and `batch1` etc.
            ...
    
    
    ...
    
    train_iter0 = chainer.iterators.SerialIterator(train0, args.batchsize)
    train_iter1 = chainer.iterators.SerialIterator(train1, args.batchsize)
    
    # You can pass several iterators in dict format to your own Updater class.
    updater = MyUpdater(
        {'0': train_iter0, '1': train_iter1}, optimizer, device=args.gpu)
    

    Note that I have not tested that above code works or not.

    For other reference, DCGAN example code shows another example of overriding update_core to defining your own update scheme (but it is also not using multiple iterator). https://github.com/chainer/chainer/blob/master/examples/dcgan/updater.py#L30