pydataprovider2_en.rst 11.0 KB
Newer Older
1 2
PyDataProvider2
=================
Y
yuyang18 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

We highly recommand users to use PyDataProvider2 to provide training or testing
data to PaddlePaddle. The user only needs to focus on how to read a single
sample from the original data file by using PyDataProvider2, leaving all of the
trivial work, including, transfering data into cpu/gpu memory, shuffle, binary
serialization to PyDataProvider2. PyDataProvider2 uses multithreading and a
fanscinating but simple cache strategy to optimize the efficiency of the data
providing process.

DataProvider for the non-sequential model
-----------------------------------------

Here we use the MNIST handwriting recognition data as an example to illustrate
how to write a simple PyDataProvider.

MNIST is a handwriting classification data set. It contains 70,000 digital
grayscale images. Labels of the training sample range from 0 to 9. All the
D
dangqingqing 已提交
20
images have been size-normalized and centered into images with the same size
Y
yuyang18 已提交
21 22
of 28 x 28 pixels.

D
dangqingqing 已提交
23
A small part of the original data as an example is shown as below:
Y
yuyang18 已提交
24 25 26

.. literalinclude:: ../../../doc_cn/ui/data_provider/mnist_train.txt

27
Each line of the data contains two parts, separated by :code:`;`. The first part is
Y
yuyang18 已提交
28 29 30 31 32 33
label of an image. The second part contains 28x28 pixel float values.

Just write path of the above data into train.list. It looks like this:

.. literalinclude:: ../../../doc_cn/ui/data_provider/train.list

D
dangqingqing 已提交
34
The corresponding dataprovider is shown as below:
Y
yuyang18 已提交
35 36 37 38 39 40 41 42 43 44 45 46

.. literalinclude:: ../../../doc_cn/ui/data_provider/mnist_provider.py

The first line imports PyDataProvider2 package.
The main function is the process function, that has two parameters.
The first parameter is the settings, which is not used in this example.
The second parameter is the filename, that is exactly each line of train.list.
This parameter is passed to the process function by PaddlePaddle.

:code:`@provider` is a Python
`Decorator <http://www.learnpython.org/en/Decorators>`_ .
It sets some properties to DataProvider, and constructs a real PaddlePaddle
D
dangqingqing 已提交
47 48
DataProvider from a very simple user implemented python function. It does not
matter if you are not familiar with `Decorator`_. You can keep it simple by
Y
yuyang18 已提交
49 50 51 52 53 54 55 56 57 58 59 60
just taking :code:`@provider` as a fixed mark above the provider function you
implemented.

`input_types`_ defines the data format that a DataProvider returns.
In this example, it is set to a 28x28-dimensional dense vector and an integer
scalar, whose value ranges from 0 to 9.
`input_types`_ can be set to several kinds of input formats, please refer to the
document of `input_types`_ for more details.


The process method is the core part to construct a real DataProvider in
PaddlePaddle. It implements how to open the text file, how to read one sample
D
dangqingqing 已提交
61
from the original text file, convert them into `input_types`_, and give them
Y
yuyang18 已提交
62
back to PaddlePaddle process at line 23.
D
dangqingqing 已提交
63
Note that data yielded by the process function must follow the same order that
Y
yuyang18 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
`input_types`_ are defined.


With the help of PyDataProvider2, user can focus on how to generate ONE traning
sample by using keywords :code:`yield`.
:code:`yield` is a python keyword, and a concept related to it includes
:code:`generator`.

Only a few lines of codes need to be added into the training configuration file,
you can take this as an example.

.. literalinclude:: ../../../doc_cn/ui/data_provider/mnist_config.py

77 78 79 80 81 82 83 84 85 86 87 88 89 90
Here we specify training data by :code:`train.list`, and no testing data is specified.
The method which actually provide data is :code:`process`.

User also can use another style to provide data, which defines the
:code:`data_layer`'s name explicitly when `yield`. For example,
the :code:`dataprovider` is shown as below.

.. literalinclude:: ../../../doc_cn/ui/data_provider/mnist_provider.dict.py
   :linenos:

If user did't give the :code:`data_layer`'s name, PaddlePaddle will use
the order of :code:`data_layer` definition roughly to determine which feature to
which :code:`data_layer`. This order may be not correct, so TO DEFINE THE
:code:`data_layer`'s NAMES EXPLICITLY IS THE RECOMMANDED WAY TO PROVIDER DATA.
Y
yuyang18 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

Now, this simple example of using PyDataProvider is finished.
The only thing that the user should know is how to generte **one sample** from
**one data file**.
And PaddlePadle will do all of the rest things\:

* Form a training batch
* Shuffle the training data
* Read data with multithreading
* Cache the training data (Optional)
* CPU-> GPU double buffering.

Is this cool?

DataProvider for the sequential model
-------------------------------------
A sequence model takes sequences as its input. A sequence is made up of several
timesteps. The so-called timestep, is not necessary to have something to do
109
with time. It can also be explained to that the order of data are taken into
Y
yuyang18 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
consideration into model design and training.
For example, the sentence can be interpreted as a kind of sequence data in NLP
tasks.

Here is an example on data proivider for English sentiment classification data.
The original input data are simple English text, labeled into positive or
negative sentiment (marked by 0 and 1 respectively).

A small part of the original data as an example can be found in the path below:

.. literalinclude:: ../../../doc_cn/ui/data_provider/sentimental_train.txt

The corresponding data provider can be found in the path below:

.. literalinclude:: ../../../doc_cn/ui/data_provider/sentimental_provider.py

D
dangqingqing 已提交
126
This data provider for sequential model is a little more complex than that
Y
yuyang18 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
for MINST dataset.
A new initialization method is introduced here.
The method :code:`on_init` is configured to DataProvider by :code:`@provider`'s
:code:`init_hook` parameter, and it will be invoked once DataProvider is
initialized. The :code:`on_init` function has the following parameters:

* The first parameter is the settings object.
* The rest parameters are passed by key word arguments. Some of them are passed
  by PaddlePaddle, see reference for `init_hook`_.
  The :code:`dictionary` object is a python dict object passed from the trainer
  configuration file, and it maps word string to word id.

To pass these parameters into DataProvider, the following lines should be added
into trainer configuration file.

.. literalinclude:: ../../../doc_cn/ui/data_provider/sentimental_config.py

The definition is basically same as MNIST example, except:
* Load dictionary in this configuration
* Pass it as a parameter to the DataProvider

The `input_types` is configured in method :code:`on_init`. It has the same
effect to configure them by :code:`@provider`'s :code:`input_types` parameter.
However, the :code:`input_types` is set at runtime, so we can set it to
different types according to the input data. Input of the neural network is a
sequence of word id, so set :code:`seq_type` to :code:`integer_value_sequence`.

Durning :code:`on_init`, we save :code:`dictionary` variable to
:code:`settings`, and it will be used in :code:`process`. Note the settings
parameter for the process function and for the on_init's function are a same
object.

The basic processing logic is the same as MNIST's :code:`process` method. Each
sample in the data file is given back to PaddlePaddle process.

Thus, the basic usage of PyDataProvider is here.
Please refer to the following section reference for details.

Reference
---------

@provider
+++++++++

171
.. autofunction:: paddle.trainer.PyDataProvider2.provider
Y
yuyang18 已提交
172 173 174 175 176 177 178

input_types
+++++++++++

PaddlePaddle has four data types, and three sequence types.
The four data types are: 

179 180
* :code:`dense_vector`: dense float vector.
* :code:`sparse_binary_vector`: sparse binary vector, most of the value is 0, and
Y
yuyang18 已提交
181
  the non zero elements are fixed to 1.
182 183 184
* :code:`sparse_float_vector`: sparse float vector, most of the value is 0, and some
  non zero elements can be any float value. They are given by the user.
* :code:`integer`: an integer scalar, that is especially used for label or word index.
Y
yuyang18 已提交
185

186
The three sequence types are:
Y
yuyang18 已提交
187

188 189 190
* :code:`SequenceType.NO_SEQUENCE` means the sample is not a sequence.
* :code:`SequenceType.SEQUENCE` means the sample is a sequence.
* :code:`SequenceType.SUB_SEQUENCE` means it is a nested sequence, that each timestep of
Y
yuyang18 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  the input sequence is also a sequence.

Different input type has a defferenct input format. Their formats are shown
in the above table.

+----------------------+---------------------+-----------------------------------+------------------------------------------------+
|                      | NO_SEQUENCE         | SEQUENCE                          |  SUB_SEQUENCE                                  |
+======================+=====================+===================================+================================================+
| dense_vector         | [f, f, ...]         | [[f, ...], [f, ...], ...]         | [[[f, ...], ...], [[f, ...], ...],...]         |
+----------------------+---------------------+-----------------------------------+------------------------------------------------+
| sparse_binary_vector | [i, i, ...]         | [[i, ...], [i, ...], ...]         | [[[i, ...], ...], [[i, ...], ...],...]         |
+----------------------+---------------------+-----------------------------------+------------------------------------------------+
| sparse_float_vector  | [(i,f), (i,f), ...] | [[(i,f), ...], [(i,f), ...], ...] | [[[(i,f), ...], ...], [[(i,f), ...], ...],...] |
+----------------------+---------------------+-----------------------------------+------------------------------------------------+
| integer_value        |  i                  | [i, i, ...]                       | [[i, ...], [i, ...], ...]                      |
+----------------------+---------------------+-----------------------------------+------------------------------------------------+

where f represents a float value, i represents an integer value.

init_hook
+++++++++

init_hook is a function that is invoked once the data provoder is initialized.
Its parameters lists as follows:

216 217 218 219 220 221
* The first parameter is a settings object, which is the same to :code:`settings`
  in :code:`process` method. The object contains several attributes, including:

  * :code:`settings.input_types`: the input types. Reference `input_types`_.
  * :code:`settings.logger`: a logging object.

Y
yuyang18 已提交
222 223
* The rest parameters are the key word arguments. It is made up of PaddpePaddle
  pre-defined parameters and user defined parameters.
224 225 226 227 228 229 230

  * PaddlePaddle-defined parameters including:

    * :code:`is_train` is a bool parameter that indicates the DataProvider is used in
      training or testing.
    * :code:`file_list` is the list of all files.
      
Y
yuyang18 已提交
231 232 233 234 235 236 237 238
  * User-defined parameters args can be set in training configuration.

Note, PaddlePaddle reserves the right to add pre-defined parameter, so please
use :code:`**kwargs` in init_hook to ensure compatibility by accepting the
parameters which your init_hook does not use.

cache
+++++
239 240 241
DataProvider provides two simple cache strategy. They are:

* :code:`CacheType.NO_CACHE` means do not cache any data, then data is read at runtime by
Y
yuyang18 已提交
242
  the user implemented python module every pass.
243
* :code:`CacheType.CACHE_PASS_IN_MEM` means the first pass reads data by the user
Y
yuyang18 已提交
244 245
  implemented python module, and the rest passes will directly read data from
  memory.