chainer.dataset.converter

chainer.dataset.converter()[source]

Decorator to make a converter function.

The target converter must be a callable that accepts two positional arguments: a batch and a device, and returns 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).

Example

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

This decorator puts a mark on the target converter function so that Chainer can recognize that it accepts chainer.backend.Device as the device argument. For backward compatibility, the decorator also 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.