At training and testing time, PaddlePaddle programs need to read data. To ease the users' work to write data reading code, we define that
During the training and testing phases, PaddlePaddle programs need to read data. To help the users write code that performs reading input data, we define the following:
- A *reader* is a function that reads data (from file, network, random number generator, etc) and yields data items.
- A *reader creator* is a function that returns a reader function.
- A *reader decorator* is a function, which accepts one or more readers, and returns a reader.
- A *batch reader* is a function that reads data (from *reader*, file, network, random number generator, etc) and yields a batch of data items.
- A *reader*: A function that reads data (from file, network, random number generator, etc) and yields the data items.
- A *reader creator*: A function that returns a reader function.
- A *reader decorator*: A function, which takes in one or more readers, and returns a reader.
- A *batch reader*: A function that reads data (from *reader*, file, network, random number generator, etc) and yields a batch of data items.
and provide function which converts reader to batch reader, frequently used reader creators and reader decorators.
and also provide a function which can convert a reader to a batch reader, frequently used reader creators and reader decorators.
## Data Reader Interface
Indeed, *data reader* doesn't have to be a function that reads and yields data items. It can be any function with no parameter that creates a iterable (anything can be used in `for x in iterable`):
*Data reader* doesn't have to be a function that reads and yields data items. It can just be any function without any parameters that creates an iterable (anything can be used in `for x in iterable`) as follows:
```
iterable = data_reader()
```
Element produced from 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)
The item produced from the iterable should be a **single** entry of data and **not** a mini batch. The entry of data could be a single item or a tuple of items. Item should be of one of the [supported types](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 etc.)
An example implementation for single item data reader creator:
An example implementation for single item data reader creator is as follows:
*batch reader* can be any function with no parameter that creates a iterable (anything can be used in `for x in iterable`). The output of the iterable should be a batch (list) of data items. Each item inside the list must be a tuple.
*Batch reader* can be any function without any parameters that creates an iterable (anything can be used in `for x in iterable`). The output of the iterable should be a batch (list) of data items. Each item inside the list should be a tuple.
Here are some valid outputs:
Here are valid outputs:
```python
# a mini batch of three data items. Each data item consist three columns of data, each of which is 1.
[(1, 1, 1),
...
...
@@ -58,20 +59,22 @@ Here are valid outputs:
Please note that each item inside the list must be a tuple, below is an invalid output:
```python
# wrong, [1,1,1] needs to be inside a tuple: ([1,1,1],).
# Otherwise it's ambiguous whether [1,1,1] means a single column of data [1, 1, 1],
# or three column of datas, each of which is 1.
# Otherwise it is ambiguous whether [1,1,1] means a single column of data [1, 1, 1],
# or three columns of data, each of which is 1.
[[1,1,1],
[2,2,2],
[3,3,3]]
```
It's easy to convert from reader to batch reader:
It is easy to convert from a reader to a batch reader:
*Data reader decorator* 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.
The *Data reader decorator* takes in a single reader or multiple data readers and returns a new data reader. It is similar to a [python decorator](https://wiki.python.org/moin/PythonDecorators), but it does not use `@` in the 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 are a few examples:
Since we have a strict interface for data readers (no parameters and return a single data item), a data reader can be used in a flexible way using data reader decorators. Following are 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.
Since reading data may take some time and training can not proceed without data, it is generally a good idea to prefetch the data.
For example, we want to use a source of real images (reusing mnist dataset), and a source of random images as input for [Generative Adversarial Networks](https://arxiv.org/abs/1406.2661).
For example, if we want to use a source of real images (say reusing mnist dataset), and a source of random 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 that buffers `n` data entries and shuffle them before a data entry is read.
Given the shuffle buffer size `n`, `paddle.reader.shuffle` returns a data reader that buffers `n` data entries and shuffles them before a data entry is read.
### Why reader return only a single entry, but not a mini batch?
### Why does a reader return only a single entry, and not a mini batch?
Always returning a single entry make reusing existing data readers much easier (e.g., if existing 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).
Returning a single entry makes reusing existing data readers much easier (for example, if an existing reader returns 3 entries instead if a single entry, the training code will be more complicated because it need to handle cases like a batch size 2).
We provide function `paddle.batch` to turn (single entry) reader into batch reader.
We provide a function: `paddle.batch` to turn (a single entry) reader into a batch reader.
### Why do we need batch reader, isn't train take reader and batch_size as arguments sufficient?
### Why do we need a batch reader, isn't is sufficient to give the reader and batch_size as arguments during training ?
In most of the case, train taking reader and batch_size as arguments would be sufficent. However sometimes user want to customize order of data entries inside a mini batch. Or even change batch size dynamically.
In most of the cases, it would be sufficient to give the reader and batch_size as arguments to the train method. However sometimes the user wants to customize the order of data entries inside a mini batch, or even change the batch size dynamically. For these cases using a batch reader is very efficient and helpful.
### Why use a dictionary but not a list to provide mapping?
### Why use a dictionary instead of 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}`).
Using a dictionary (`{"image":0, "label":1}`) instead of a list (`["image", "label"]`) gives the advantage that the user can easily reuse the items (e.g., using `{"image_a":0, "image_b":0, "label":1}`) or even skip an item (e.g., using `{"image_a":0, "label":2}`).
<spanid="python-data-reader-design-doc"></span><h1>Python Data Reader Design Doc<aclass="headerlink"href="#python-data-reader-design-doc"title="Permalink to this headline">¶</a></h1>
<p>At training and testing time, PaddlePaddle programs need to read data. To ease the users’ work to write data reading code, we define that</p>
<p>During the training and testing phases, PaddlePaddle programs need to read data. To help the users write code that performs reading input data, we define the following:</p>
<ulclass="simple">
<li>A <em>reader</em> is a function that reads data (from file, network, random number generator, etc) and yields data items.</li>
<li>A <em>reader creator</em> is a function that returns a reader function.</li>
<li>A <em>reader decorator</em> is a function, which accepts one or more readers, and returns a reader.</li>
<li>A <em>batch reader</em> is a function that reads data (from <em>reader</em>, file, network, random number generator, etc) and yields a batch of data items.</li>
<li>A <em>reader</em>: A function that reads data (from file, network, random number generator, etc) and yields the data items.</li>
<li>A <em>reader creator</em>: A function that returns a reader function.</li>
<li>A <em>reader decorator</em>: A function, which takes in one or more readers, and returns a reader.</li>
<li>A <em>batch reader</em>: A function that reads data (from <em>reader</em>, file, network, random number generator, etc) and yields a batch of data items.</li>
</ul>
<p>and provide function which converts reader to batch reader, frequently used reader creators and reader decorators.</p>
<p>and also provide a function which can convert a reader to a batch reader, frequently used reader creators and reader decorators.</p>
<divclass="section"id="data-reader-interface">
<spanid="data-reader-interface"></span><h2>Data Reader Interface<aclass="headerlink"href="#data-reader-interface"title="Permalink to this headline">¶</a></h2>
<p>Indeed, <em>data reader</em> doesn’t have to be a function that reads and yields data items. It can be any function with no parameter that creates a iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>):</p>
<p><em>Data reader</em> doesn’t have to be a function that reads and yields data items. It can just be any function without any parameters that creates an iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>) as follows:</p>
<p>Element produced from the iterable should be a <strong>single</strong> entry of data, <strong>not</strong> a mini batch. That entry of data could be a single item, or a tuple of items. Item should be of <aclass="reference external"href="http://www.paddlepaddle.org/doc/ui/data_provider/pydataprovider2.html?highlight=dense_vector#input-types">supported type</a> (e.g., numpy 1d array of float32, int, list of int)</p>
<p>An example implementation for single item data reader creator:</p>
<p>The item produced from the iterable should be a <strong>single</strong> entry of data and <strong>not</strong> a mini batch. The entry of data could be a single item or a tuple of items. Item should be of one of the <aclass="reference external"href="http://www.paddlepaddle.org/doc/ui/data_provider/pydataprovider2.html?highlight=dense_vector#input-types">supported types</a> (e.g., numpy 1d array of float32, int, list of int etc.)</p>
<p>An example implementation for single item data reader creator is as follows:</p>
<spanid="batch-reader-interface"></span><h2>Batch Reader Interface<aclass="headerlink"href="#batch-reader-interface"title="Permalink to this headline">¶</a></h2>
<p><em>batch reader</em> can be any function with no parameter that creates a iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>). The output of the iterable should be a batch (list) of data items. Each item inside the list must be a tuple.</p>
<p>Here are valid outputs:</p>
<p><em>Batch reader</em> can be any function without any parameters that creates an iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>). The output of the iterable should be a batch (list) of data items. Each item inside the list should be a tuple.</p>
<p>Here are some valid outputs:</p>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># a mini batch of three data items. Each data item consist three columns of data, each of which is 1.</span>
<spanid="usage"></span><h2>Usage<aclass="headerlink"href="#usage"title="Permalink to this headline">¶</a></h2>
<p>batch reader, mapping from item(s) read to data layer, batch size and number of total pass will be passed into <codeclass="docutils literal"><spanclass="pre">paddle.train</span></code>:</p>
<p>Following is how we can use the reader with PaddlePaddle:
The batch reader, a mapping from item(s) to data layer, the batch size and the number of total passes will be passed into <codeclass="docutils literal"><spanclass="pre">paddle.train</span></code> as follows:</p>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># two data layer is created:</span>
<spanid="data-reader-decorator"></span><h2>Data Reader Decorator<aclass="headerlink"href="#data-reader-decorator"title="Permalink to this headline">¶</a></h2>
<p><em>Data reader decorator</em> takes a single or multiple data reader, returns a new data reader. It is similar to a <aclass="reference external"href="https://wiki.python.org/moin/PythonDecorators">python decorator</a>, but it does not use <codeclass="docutils literal"><spanclass="pre">@</span></code> syntax.</p>
<p>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 are a few examples:</p>
<p>The <em>Data reader decorator</em> takes in a single reader or multiple data readers and returns a new data reader. It is similar to a <aclass="reference external"href="https://wiki.python.org/moin/PythonDecorators">python decorator</a>, but it does not use <codeclass="docutils literal"><spanclass="pre">@</span></code> in the syntax.</p>
<p>Since we have a strict interface for data readers (no parameters and return a single data item), a data reader can be used in a flexible way using data reader decorators. Following are a few examples:</p>
<divclass="section"id="prefetch-data">
<spanid="prefetch-data"></span><h3>Prefetch Data<aclass="headerlink"href="#prefetch-data"title="Permalink to this headline">¶</a></h3>
<p>Since reading data may take time and training can not proceed without data. It is generally a good idea to prefetch data.</p>
<p>Since reading data may take some time and training can not proceed without data, it is generally a good idea to prefetch the data.</p>
<p>Use <codeclass="docutils literal"><spanclass="pre">paddle.reader.buffered</span></code> to prefetch data:</p>
<spanid="compose-multiple-data-readers"></span><h3>Compose Multiple Data Readers<aclass="headerlink"href="#compose-multiple-data-readers"title="Permalink to this headline">¶</a></h3>
<p>For example, we want to use a source of real images (reusing mnist dataset), and a source of random images as input for <aclass="reference external"href="https://arxiv.org/abs/1406.2661">Generative Adversarial Networks</a>.</p>
<p>We can do:</p>
<p>For example, if we want to use a source of real images (say reusing mnist dataset), and a source of random images as input for <aclass="reference external"href="https://arxiv.org/abs/1406.2661">Generative Adversarial Networks</a>.</p>
<spanid="shuffle"></span><h3>Shuffle<aclass="headerlink"href="#shuffle"title="Permalink to this headline">¶</a></h3>
<p>Given shuffle buffer size <codeclass="docutils literal"><spanclass="pre">n</span></code>, <codeclass="docutils literal"><spanclass="pre">paddle.reader.shuffle</span></code> will return a data reader that buffers <codeclass="docutils literal"><spanclass="pre">n</span></code> data entries and shuffle them before a data entry is read.</p>
<p>Given the shuffle buffer size <codeclass="docutils literal"><spanclass="pre">n</span></code>, <codeclass="docutils literal"><spanclass="pre">paddle.reader.shuffle</span></code> returns a data reader that buffers <codeclass="docutils literal"><spanclass="pre">n</span></code> data entries and shuffles them before a data entry is read.</p>
<spanid="why-reader-return-only-a-single-entry-but-not-a-mini-batch"></span><h3>Why reader return only a single entry, but not a mini batch?<aclass="headerlink"href="#why-reader-return-only-a-single-entry-but-not-a-mini-batch"title="Permalink to this headline">¶</a></h3>
<p>Always returning a single entry make reusing existing data readers much easier (e.g., if existing 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).</p>
<p>We provide function <codeclass="docutils literal"><spanclass="pre">paddle.batch</span></code> to turn (single entry) reader into batch reader.</p>
<spanid="why-does-a-reader-return-only-a-single-entry-and-not-a-mini-batch"></span><h3>Why does a reader return only a single entry, and not a mini batch?<aclass="headerlink"href="#why-does-a-reader-return-only-a-single-entry-and-not-a-mini-batch"title="Permalink to this headline">¶</a></h3>
<p>Returning a single entry makes reusing existing data readers much easier (for example, if an existing reader returns 3 entries instead if a single entry, the training code will be more complicated because it need to handle cases like a batch size 2).</p>
<p>We provide a function: <codeclass="docutils literal"><spanclass="pre">paddle.batch</span></code> to turn (a single entry) reader into a batch reader.</p>
<spanid="why-do-we-need-batch-reader-isn-t-train-take-reader-and-batch-size-as-arguments-sufficient"></span><h3>Why do we need batch reader, isn’t train take reader and batch_size as arguments sufficient?<aclass="headerlink"href="#why-do-we-need-batch-reader-isn-t-train-take-reader-and-batch-size-as-arguments-sufficient"title="Permalink to this headline">¶</a></h3>
<p>In most of the case, train taking reader and batch_size as arguments would be sufficent. However sometimes user want to customize order of data entries inside a mini batch. Or even change batch size dynamically.</p>
<spanid="why-do-we-need-a-batch-reader-isn-t-is-sufficient-to-give-the-reader-and-batch-size-as-arguments-during-training"></span><h3>Why do we need a batch reader, isn’t is sufficient to give the reader and batch_size as arguments during training ?<aclass="headerlink"href="#why-do-we-need-a-batch-reader-isn-t-is-sufficient-to-give-the-reader-and-batch-size-as-arguments-during-training"title="Permalink to this headline">¶</a></h3>
<p>In most of the cases, it would be sufficient to give the reader and batch_size as arguments to the train method. However sometimes the user wants to customize the order of data entries inside a mini batch, or even change the batch size dynamically. For these cases using a batch reader is very efficient and helpful.</p>
<spanid="why-use-a-dictionary-but-not-a-list-to-provide-mapping"></span><h3>Why use a dictionary but not a list to provide mapping?<aclass="headerlink"href="#why-use-a-dictionary-but-not-a-list-to-provide-mapping"title="Permalink to this headline">¶</a></h3>
<p>We decided to use dictionary (<codeclass="docutils literal"><spanclass="pre">{"image":0,</span><spanclass="pre">"label":1}</span></code>) instead of list (<codeclass="docutils literal"><spanclass="pre">["image",</span><spanclass="pre">"label"]</span></code>) is because that user can easily resue item (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"image_b":0,</span><spanclass="pre">"label":1}</span></code>) or skip item (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"label":2}</span></code>).</p>
<spanid="why-use-a-dictionary-instead-of-a-list-to-provide-mapping"></span><h3>Why use a dictionary instead of a list to provide mapping?<aclass="headerlink"href="#why-use-a-dictionary-instead-of-a-list-to-provide-mapping"title="Permalink to this headline">¶</a></h3>
<p>Using a dictionary (<codeclass="docutils literal"><spanclass="pre">{"image":0,</span><spanclass="pre">"label":1}</span></code>) instead of a list (<codeclass="docutils literal"><spanclass="pre">["image",</span><spanclass="pre">"label"]</span></code>) gives the advantage that the user can easily reuse the items (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"image_b":0,</span><spanclass="pre">"label":1}</span></code>) or even skip an item (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"label":2}</span></code>).</p>
<spanid="how-to-create-custom-data-reader-creator"></span><h3>How to create custom data reader creator<aclass="headerlink"href="#how-to-create-custom-data-reader-creator"title="Permalink to this headline">¶</a></h3>
<spanid="how-to-create-a-custom-data-reader-creator"></span><h3>How to create a custom data reader creator ?<aclass="headerlink"href="#how-to-create-a-custom-data-reader-creator"title="Permalink to this headline">¶</a></h3>
<spanid="how-is-paddle-train-implemented"></span><h3>How is <codeclass="docutils literal"><spanclass="pre">paddle.train</span></code> implemented<aclass="headerlink"href="#how-is-paddle-train-implemented"title="Permalink to this headline">¶</a></h3>
<p>An example implementation of paddle.train could be:</p>
<p>An example implementation of paddle.train is:</p>
<spanclass="k">for</span><spanclass="n">mini_batch</span><spanclass="ow">in</span><spanclass="n">batch_reader</span><spanclass="p">():</span><spanclass="c1"># this loop will never end in online learning.</span>
At training and testing time, PaddlePaddle programs need to read data. To ease the users' work to write data reading code, we define that
During the training and testing phases, PaddlePaddle programs need to read data. To help the users write code that performs reading input data, we define the following:
- A *reader* is a function that reads data (from file, network, random number generator, etc) and yields data items.
- A *reader creator* is a function that returns a reader function.
- A *reader decorator* is a function, which accepts one or more readers, and returns a reader.
- A *batch reader* is a function that reads data (from *reader*, file, network, random number generator, etc) and yields a batch of data items.
- A *reader*: A function that reads data (from file, network, random number generator, etc) and yields the data items.
- A *reader creator*: A function that returns a reader function.
- A *reader decorator*: A function, which takes in one or more readers, and returns a reader.
- A *batch reader*: A function that reads data (from *reader*, file, network, random number generator, etc) and yields a batch of data items.
and provide function which converts reader to batch reader, frequently used reader creators and reader decorators.
and also provide a function which can convert a reader to a batch reader, frequently used reader creators and reader decorators.
## Data Reader Interface
Indeed, *data reader* doesn't have to be a function that reads and yields data items. It can be any function with no parameter that creates a iterable (anything can be used in `for x in iterable`):
*Data reader* doesn't have to be a function that reads and yields data items. It can just be any function without any parameters that creates an iterable (anything can be used in `for x in iterable`) as follows:
```
iterable = data_reader()
```
Element produced from 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)
The item produced from the iterable should be a **single** entry of data and **not** a mini batch. The entry of data could be a single item or a tuple of items. Item should be of one of the [supported types](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 etc.)
An example implementation for single item data reader creator:
An example implementation for single item data reader creator is as follows:
*batch reader* can be any function with no parameter that creates a iterable (anything can be used in `for x in iterable`). The output of the iterable should be a batch (list) of data items. Each item inside the list must be a tuple.
*Batch reader* can be any function without any parameters that creates an iterable (anything can be used in `for x in iterable`). The output of the iterable should be a batch (list) of data items. Each item inside the list should be a tuple.
Here are some valid outputs:
Here are valid outputs:
```python
# a mini batch of three data items. Each data item consist three columns of data, each of which is 1.
[(1, 1, 1),
...
...
@@ -58,20 +59,22 @@ Here are valid outputs:
Please note that each item inside the list must be a tuple, below is an invalid output:
```python
# wrong, [1,1,1] needs to be inside a tuple: ([1,1,1],).
# Otherwise it's ambiguous whether [1,1,1] means a single column of data [1, 1, 1],
# or three column of datas, each of which is 1.
# Otherwise it is ambiguous whether [1,1,1] means a single column of data [1, 1, 1],
# or three columns of data, each of which is 1.
[[1,1,1],
[2,2,2],
[3,3,3]]
```
It's easy to convert from reader to batch reader:
It is easy to convert from a reader to a batch reader:
*Data reader decorator* 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.
The *Data reader decorator* takes in a single reader or multiple data readers and returns a new data reader. It is similar to a [python decorator](https://wiki.python.org/moin/PythonDecorators), but it does not use `@` in the 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 are a few examples:
Since we have a strict interface for data readers (no parameters and return a single data item), a data reader can be used in a flexible way using data reader decorators. Following are 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.
Since reading data may take some time and training can not proceed without data, it is generally a good idea to prefetch the data.
For example, we want to use a source of real images (reusing mnist dataset), and a source of random images as input for [Generative Adversarial Networks](https://arxiv.org/abs/1406.2661).
For example, if we want to use a source of real images (say reusing mnist dataset), and a source of random 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 that buffers `n` data entries and shuffle them before a data entry is read.
Given the shuffle buffer size `n`, `paddle.reader.shuffle` returns a data reader that buffers `n` data entries and shuffles them before a data entry is read.
### Why reader return only a single entry, but not a mini batch?
### Why does a reader return only a single entry, and not a mini batch?
Always returning a single entry make reusing existing data readers much easier (e.g., if existing 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).
Returning a single entry makes reusing existing data readers much easier (for example, if an existing reader returns 3 entries instead if a single entry, the training code will be more complicated because it need to handle cases like a batch size 2).
We provide function `paddle.batch` to turn (single entry) reader into batch reader.
We provide a function: `paddle.batch` to turn (a single entry) reader into a batch reader.
### Why do we need batch reader, isn't train take reader and batch_size as arguments sufficient?
### Why do we need a batch reader, isn't is sufficient to give the reader and batch_size as arguments during training ?
In most of the case, train taking reader and batch_size as arguments would be sufficent. However sometimes user want to customize order of data entries inside a mini batch. Or even change batch size dynamically.
In most of the cases, it would be sufficient to give the reader and batch_size as arguments to the train method. However sometimes the user wants to customize the order of data entries inside a mini batch, or even change the batch size dynamically. For these cases using a batch reader is very efficient and helpful.
### Why use a dictionary but not a list to provide mapping?
### Why use a dictionary instead of 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}`).
Using a dictionary (`{"image":0, "label":1}`) instead of a list (`["image", "label"]`) gives the advantage that the user can easily reuse the items (e.g., using `{"image_a":0, "image_b":0, "label":1}`) or even skip an item (e.g., using `{"image_a":0, "label":2}`).
<spanid="python-data-reader-design-doc"></span><h1>Python Data Reader Design Doc<aclass="headerlink"href="#python-data-reader-design-doc"title="永久链接至标题">¶</a></h1>
<p>At training and testing time, PaddlePaddle programs need to read data. To ease the users’ work to write data reading code, we define that</p>
<p>During the training and testing phases, PaddlePaddle programs need to read data. To help the users write code that performs reading input data, we define the following:</p>
<ulclass="simple">
<li>A <em>reader</em> is a function that reads data (from file, network, random number generator, etc) and yields data items.</li>
<li>A <em>reader creator</em> is a function that returns a reader function.</li>
<li>A <em>reader decorator</em> is a function, which accepts one or more readers, and returns a reader.</li>
<li>A <em>batch reader</em> is a function that reads data (from <em>reader</em>, file, network, random number generator, etc) and yields a batch of data items.</li>
<li>A <em>reader</em>: A function that reads data (from file, network, random number generator, etc) and yields the data items.</li>
<li>A <em>reader creator</em>: A function that returns a reader function.</li>
<li>A <em>reader decorator</em>: A function, which takes in one or more readers, and returns a reader.</li>
<li>A <em>batch reader</em>: A function that reads data (from <em>reader</em>, file, network, random number generator, etc) and yields a batch of data items.</li>
</ul>
<p>and provide function which converts reader to batch reader, frequently used reader creators and reader decorators.</p>
<p>and also provide a function which can convert a reader to a batch reader, frequently used reader creators and reader decorators.</p>
<p>Indeed, <em>data reader</em> doesn’t have to be a function that reads and yields data items. It can be any function with no parameter that creates a iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>):</p>
<p><em>Data reader</em> doesn’t have to be a function that reads and yields data items. It can just be any function without any parameters that creates an iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>) as follows:</p>
<p>Element produced from the iterable should be a <strong>single</strong> entry of data, <strong>not</strong> a mini batch. That entry of data could be a single item, or a tuple of items. Item should be of <aclass="reference external"href="http://www.paddlepaddle.org/doc/ui/data_provider/pydataprovider2.html?highlight=dense_vector#input-types">supported type</a> (e.g., numpy 1d array of float32, int, list of int)</p>
<p>An example implementation for single item data reader creator:</p>
<p>The item produced from the iterable should be a <strong>single</strong> entry of data and <strong>not</strong> a mini batch. The entry of data could be a single item or a tuple of items. Item should be of one of the <aclass="reference external"href="http://www.paddlepaddle.org/doc/ui/data_provider/pydataprovider2.html?highlight=dense_vector#input-types">supported types</a> (e.g., numpy 1d array of float32, int, list of int etc.)</p>
<p>An example implementation for single item data reader creator is as follows:</p>
<p><em>batch reader</em> can be any function with no parameter that creates a iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>). The output of the iterable should be a batch (list) of data items. Each item inside the list must be a tuple.</p>
<p>Here are valid outputs:</p>
<p><em>Batch reader</em> can be any function without any parameters that creates an iterable (anything can be used in <codeclass="docutils literal"><spanclass="pre">for</span><spanclass="pre">x</span><spanclass="pre">in</span><spanclass="pre">iterable</span></code>). The output of the iterable should be a batch (list) of data items. Each item inside the list should be a tuple.</p>
<p>Here are some valid outputs:</p>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># a mini batch of three data items. Each data item consist three columns of data, each of which is 1.</span>
<p>batch reader, mapping from item(s) read to data layer, batch size and number of total pass will be passed into <codeclass="docutils literal"><spanclass="pre">paddle.train</span></code>:</p>
<p>Following is how we can use the reader with PaddlePaddle:
The batch reader, a mapping from item(s) to data layer, the batch size and the number of total passes will be passed into <codeclass="docutils literal"><spanclass="pre">paddle.train</span></code> as follows:</p>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># two data layer is created:</span>
<p><em>Data reader decorator</em> takes a single or multiple data reader, returns a new data reader. It is similar to a <aclass="reference external"href="https://wiki.python.org/moin/PythonDecorators">python decorator</a>, but it does not use <codeclass="docutils literal"><spanclass="pre">@</span></code> syntax.</p>
<p>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 are a few examples:</p>
<p>The <em>Data reader decorator</em> takes in a single reader or multiple data readers and returns a new data reader. It is similar to a <aclass="reference external"href="https://wiki.python.org/moin/PythonDecorators">python decorator</a>, but it does not use <codeclass="docutils literal"><spanclass="pre">@</span></code> in the syntax.</p>
<p>Since we have a strict interface for data readers (no parameters and return a single data item), a data reader can be used in a flexible way using data reader decorators. Following are a few examples:</p>
<spanid="compose-multiple-data-readers"></span><h3>Compose Multiple Data Readers<aclass="headerlink"href="#compose-multiple-data-readers"title="永久链接至标题">¶</a></h3>
<p>For example, we want to use a source of real images (reusing mnist dataset), and a source of random images as input for <aclass="reference external"href="https://arxiv.org/abs/1406.2661">Generative Adversarial Networks</a>.</p>
<p>We can do:</p>
<p>For example, if we want to use a source of real images (say reusing mnist dataset), and a source of random images as input for <aclass="reference external"href="https://arxiv.org/abs/1406.2661">Generative Adversarial Networks</a>.</p>
<p>Given shuffle buffer size <codeclass="docutils literal"><spanclass="pre">n</span></code>, <codeclass="docutils literal"><spanclass="pre">paddle.reader.shuffle</span></code> will return a data reader that buffers <codeclass="docutils literal"><spanclass="pre">n</span></code> data entries and shuffle them before a data entry is read.</p>
<p>Given the shuffle buffer size <codeclass="docutils literal"><spanclass="pre">n</span></code>, <codeclass="docutils literal"><spanclass="pre">paddle.reader.shuffle</span></code> returns a data reader that buffers <codeclass="docutils literal"><spanclass="pre">n</span></code> data entries and shuffles them before a data entry is read.</p>
<spanid="why-reader-return-only-a-single-entry-but-not-a-mini-batch"></span><h3>Why reader return only a single entry, but not a mini batch?<aclass="headerlink"href="#why-reader-return-only-a-single-entry-but-not-a-mini-batch"title="永久链接至标题">¶</a></h3>
<p>Always returning a single entry make reusing existing data readers much easier (e.g., if existing 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).</p>
<p>We provide function <codeclass="docutils literal"><spanclass="pre">paddle.batch</span></code> to turn (single entry) reader into batch reader.</p>
<spanid="why-does-a-reader-return-only-a-single-entry-and-not-a-mini-batch"></span><h3>Why does a reader return only a single entry, and not a mini batch?<aclass="headerlink"href="#why-does-a-reader-return-only-a-single-entry-and-not-a-mini-batch"title="永久链接至标题">¶</a></h3>
<p>Returning a single entry makes reusing existing data readers much easier (for example, if an existing reader returns 3 entries instead if a single entry, the training code will be more complicated because it need to handle cases like a batch size 2).</p>
<p>We provide a function: <codeclass="docutils literal"><spanclass="pre">paddle.batch</span></code> to turn (a single entry) reader into a batch reader.</p>
<spanid="why-do-we-need-batch-reader-isn-t-train-take-reader-and-batch-size-as-arguments-sufficient"></span><h3>Why do we need batch reader, isn’t train take reader and batch_size as arguments sufficient?<aclass="headerlink"href="#why-do-we-need-batch-reader-isn-t-train-take-reader-and-batch-size-as-arguments-sufficient"title="永久链接至标题">¶</a></h3>
<p>In most of the case, train taking reader and batch_size as arguments would be sufficent. However sometimes user want to customize order of data entries inside a mini batch. Or even change batch size dynamically.</p>
<spanid="why-do-we-need-a-batch-reader-isn-t-is-sufficient-to-give-the-reader-and-batch-size-as-arguments-during-training"></span><h3>Why do we need a batch reader, isn’t is sufficient to give the reader and batch_size as arguments during training ?<aclass="headerlink"href="#why-do-we-need-a-batch-reader-isn-t-is-sufficient-to-give-the-reader-and-batch-size-as-arguments-during-training"title="永久链接至标题">¶</a></h3>
<p>In most of the cases, it would be sufficient to give the reader and batch_size as arguments to the train method. However sometimes the user wants to customize the order of data entries inside a mini batch, or even change the batch size dynamically. For these cases using a batch reader is very efficient and helpful.</p>
<spanid="why-use-a-dictionary-but-not-a-list-to-provide-mapping"></span><h3>Why use a dictionary but not a list to provide mapping?<aclass="headerlink"href="#why-use-a-dictionary-but-not-a-list-to-provide-mapping"title="永久链接至标题">¶</a></h3>
<p>We decided to use dictionary (<codeclass="docutils literal"><spanclass="pre">{"image":0,</span><spanclass="pre">"label":1}</span></code>) instead of list (<codeclass="docutils literal"><spanclass="pre">["image",</span><spanclass="pre">"label"]</span></code>) is because that user can easily resue item (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"image_b":0,</span><spanclass="pre">"label":1}</span></code>) or skip item (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"label":2}</span></code>).</p>
<spanid="why-use-a-dictionary-instead-of-a-list-to-provide-mapping"></span><h3>Why use a dictionary instead of a list to provide mapping?<aclass="headerlink"href="#why-use-a-dictionary-instead-of-a-list-to-provide-mapping"title="永久链接至标题">¶</a></h3>
<p>Using a dictionary (<codeclass="docutils literal"><spanclass="pre">{"image":0,</span><spanclass="pre">"label":1}</span></code>) instead of a list (<codeclass="docutils literal"><spanclass="pre">["image",</span><spanclass="pre">"label"]</span></code>) gives the advantage that the user can easily reuse the items (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"image_b":0,</span><spanclass="pre">"label":1}</span></code>) or even skip an item (e.g., using <codeclass="docutils literal"><spanclass="pre">{"image_a":0,</span><spanclass="pre">"label":2}</span></code>).</p>
<spanid="how-to-create-custom-data-reader-creator"></span><h3>How to create custom data reader creator<aclass="headerlink"href="#how-to-create-custom-data-reader-creator"title="永久链接至标题">¶</a></h3>
<spanid="how-to-create-a-custom-data-reader-creator"></span><h3>How to create a custom data reader creator ?<aclass="headerlink"href="#how-to-create-a-custom-data-reader-creator"title="永久链接至标题">¶</a></h3>
<spanid="how-is-paddle-train-implemented"></span><h3>How is <codeclass="docutils literal"><spanclass="pre">paddle.train</span></code> implemented<aclass="headerlink"href="#how-is-paddle-train-implemented"title="永久链接至标题">¶</a></h3>
<p>An example implementation of paddle.train could be:</p>
<p>An example implementation of paddle.train is:</p>
<spanclass="k">for</span><spanclass="n">mini_batch</span><spanclass="ow">in</span><spanclass="n">batch_reader</span><spanclass="p">():</span><spanclass="c1"># this loop will never end in online learning.</span>