Decorator to make a converter.

This decorator turns a converter function into a chainer.dataset.Converter class instance, which also is a callable. This is required to use the converter function from an old module that does not support chainer.backend.Device instances (See the Device argument conversion section below).

Requirements of the target function

The target converter function must accept two positional arguments: a batch and a device, and return a converted batch.

The type of the device argument is chainer.backend.Device.

The types and values of the batches (the first argument and the return value) are not specified: they depend on how the converter is used (e.g. by updaters).


>>> @chainer.dataset.converter()
... def custom_converter(batch, device):
...     assert isinstance(device, chainer.backend.Device)
...     # do something with batch...
...     return device.send(batch)

Device argument conversion

For backward compatibility, the decorator wraps the function so that if the converter is called with the device argument with int type, it is converted to a chainer.backend.Device instance before calling the original function. The int value indicates the CUDA device of the cupy backend.

Without the decorator, the converter cannot support ChainerX devices. If the batch were requested to be converted to ChainerX with such converters, RuntimeError will be raised.


Converters using this decorator can’t be pickled causing to fail when the multiprocessing start mode is set to 'spawn' or 'forkserver'. Should you need to use such feature, please rely on class style converters.