Data provider provides data for training. It will be passed into `paddle.train` as a parameter.
Paddle reads data from data reader during training. It will be passed into `paddle.train` as a parameter.
## Data Provider Interface
## Data Reader Interface
Data provider is a function with no parameter that creates a iterable (anything can be used in `for x in iterable`):
Data reader is a function with no parameter that creates a iterable (anything can be used in `for x in iterable`):
```
iterable = data_provider()
iterable = data_reader()
```
Element produced for the iterable should be a **single** entry of data, in format `[column_0_item, column_1_item, ...]`. Each element of the list needs to be supported data type (e.g., numpy 1d array of float32, list of int).
Element produced for the iterable should be a **single** entry of data, **not** a mini batch. That entry of data could be a single item, or a tuple of items. Item should be of [supported type](http://www.paddlepaddle.org/doc/ui/data_provider/pydataprovider2.html?highlight=dense_vector#input-types)(e.g., numpy 1d array of float32, int, list of int)
For example, `column_0_item` could be image pixels of format numpy 1d array of float32, and `column_1_item` could be image label of format single int value:
An example implementation for single item data reader:
```python
defdata_reader_fake_image():
whileTrue:
yieldnumpy.random.uniform(-1,1,size=20*20)
```
An example implementation for multiple item data reader:
```python
defdata_reader_fake_image_and_label():
whileTrue:
yieldnumpy.random.uniform(-1,1,size=20*20),False
```
for single_entry in iterable:
pixel = entry[0]
label = entry[1]
## Data Reader Decorators
Data Reader Decorators takes a single or multiple data reader, returns a new data reader. It is similar to a [python decorator](https://wiki.python.org/moin/PythonDecorators), but it does not use `@` syntax.
Since we have a strict interface for data readers (no parameter, return a single data item). Data reader can be used flexiable via data reader decorators. Following a few examples:
### Prefetch Data
Since reading data may take time and training can not proceed without data. It is generally a good idea to prefetch data.
`buffered_reader` will try to buffer (prefetch) `100` data entries.
### Compose Multiple Data Readers
For example, we want to use a source of real images (reusing mnist dataset), and a source of fake images as input for [Generative Adversarial Networks](https://arxiv.org/abs/1406.2661).
Given shuffle buffer size `n`, `paddle.reader.shuffle` will return a data reader decorater that buffers `n` data entries and shuffle them before a data entry is read.
### Why return only a single entry, but not a mini batch?
If return a mini batch, data provider need to take care of batch size. But batch size is a concept for training, it makes more sense for user to specify batch size as a parameter for `train`.
If return a mini batch, data reader need to take care of batch size. But batch size is a concept for training, it makes more sense for user to specify batch size as a parameter for `train`.
Concretely, always return a single entry make reusing existing data providers much easier (e.g., if existing data provider return not a single entry but 3 entries, training code will be more complex because it need to handle cases like batch size 2).
Practically, always return a single entry make reusing existing data reader much easier (e.g., if existing data reader return not a single entry but 3 entries, training code will be more complex because it need to handle cases like batch size 2).
### How to create custom data provider
### Why use a dictionary but not a list to provide mapping?
We decided to use dictionary (`{"image":0, "label":1}`) instead of list (`["image", "label"]`) is because that user can easily resue item (e.g., using `{"image_a":0, "image_b":0, "label":1}`) or skip item (e.g., using `{"image_a":0, "label":2}`).