PyDataProvider2

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 images have been size-normalized and centered into images with the same size of 28 x 28 pixels.

A small part of the original data as an example is shown as below:

5;0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.215686 0.533333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.67451 0.992157 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.070588 0.886275 0.992157 0 0 0 0 0 0 0 0 0 0 0.192157 0.070588 0 0 0 0 0 0 0 0 0 0 0 0 0 0.670588 0.992157 0.992157 0 0 0 0 0 0 0 0 0 0.117647 0.933333 0.858824 0.313725 0 0 0 0 0 0 0 0 0 0 0 0.090196 0.858824 0.992157 0.831373 0 0 0 0 0 0 0 0 0 0.141176 0.992157 0.992157 0.611765 0.054902 0 0 0 0 0 0 0 0 0 0 0.258824 0.992157 0.992157 0.529412 0 0 0 0 0 0 0 0 0 0.368627 0.992157 0.992157 0.419608 0.003922 0 0 0 0 0 0 0 0 0 0.094118 0.835294 0.992157 0.992157 0.517647 0 0 0 0 0 0 0 0 0 0.603922 0.992157 0.992157 0.992157 0.603922 0.545098 0.043137 0 0 0 0 0 0 0 0.447059 0.992157 0.992157 0.956863 0.062745 0 0 0 0 0 0 0 0 0.011765 0.666667 0.992157 0.992157 0.992157 0.992157 0.992157 0.745098 0.137255 0 0 0 0 0 0.152941 0.866667 0.992157 0.992157 0.521569 0 0 0 0 0 0 0 0 0 0.070588 0.992157 0.992157 0.992157 0.803922 0.352941 0.745098 0.992157 0.945098 0.317647 0 0 0 0 0.580392 0.992157 0.992157 0.764706 0.043137 0 0 0 0 0 0 0 0 0 0.070588 0.992157 0.992157 0.776471 0.043137 0 0.007843 0.27451 0.882353 0.941176 0.176471 0 0 0.180392 0.898039 0.992157 0.992157 0.313725 0 0 0 0 0 0 0 0 0 0 0.070588 0.992157 0.992157 0.713725 0 0 0 0 0.627451 0.992157 0.729412 0.062745 0 0.509804 0.992157 0.992157 0.776471 0.035294 0 0 0 0 0 0 0 0 0 0 0.494118 0.992157 0.992157 0.968627 0.168627 0 0 0 0.423529 0.992157 0.992157 0.364706 0 0.717647 0.992157 0.992157 0.317647 0 0 0 0 0 0 0 0 0 0 0 0.533333 0.992157 0.984314 0.945098 0.603922 0 0 0 0.003922 0.466667 0.992157 0.988235 0.976471 0.992157 0.992157 0.788235 0.007843 0 0 0 0 0 0 0 0 0 0 0 0.686275 0.882353 0.364706 0 0 0 0 0 0 0.098039 0.588235 0.992157 0.992157 0.992157 0.980392 0.305882 0 0 0 0 0 0 0 0 0 0 0 0 0.101961 0.67451 0.321569 0 0 0 0 0 0 0 0.105882 0.733333 0.976471 0.811765 0.713725 0 0 0 0 0 0 0 0 0 0 0 0 0 0.65098 0.992157 0.321569 0 0 0 0 0 0 0 0 0 0.25098 0.007843 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0.94902 0.219608 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.968627 0.764706 0.152941 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.498039 0.25098 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0;0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.298039 0.333333 0.333333 0.333333 0.337255 0.333333 0.333333 0.109804 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.027451 0.223529 0.776471 0.964706 0.988235 0.988235 0.988235 0.992157 0.988235 0.988235 0.780392 0.098039 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.14902 0.698039 0.988235 0.992157 0.988235 0.901961 0.87451 0.568627 0.882353 0.976471 0.988235 0.988235 0.501961 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.188235 0.647059 0.988235 0.988235 0.745098 0.439216 0.098039 0 0 0 0.572549 0.988235 0.988235 0.988235 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2 0.933333 0.992157 0.941176 0.247059 0 0 0 0 0 0 0.188235 0.898039 0.992157 0.992157 0 0 0 0 0 0 0 0 0 0 0 0.039216 0.639216 0.933333 0.988235 0.913725 0.278431 0 0 0 0 0 0 0 0.113725 0.843137 0.988235 0.988235 0 0 0 0 0 0 0 0 0 0 0 0.235294 0.988235 0.992157 0.988235 0.815686 0.07451 0 0 0 0 0 0 0 0.333333 0.988235 0.988235 0.552941 0 0 0 0 0 0 0 0 0 0 0.211765 0.878431 0.988235 0.992157 0.701961 0.329412 0.109804 0 0 0 0 0 0 0 0.698039 0.988235 0.913725 0.145098 0 0 0 0 0 0 0 0 0 0.188235 0.890196 0.988235 0.988235 0.745098 0.047059 0 0 0 0 0 0 0 0 0 0.882353 0.988235 0.568627 0 0 0 0 0 0 0 0 0 0.2 0.933333 0.992157 0.992157 0.992157 0.447059 0.294118 0 0 0 0 0 0 0 0 0.447059 0.992157 0.768627 0 0 0 0 0 0 0 0 0 0 0.623529 0.988235 0.988235 0.988235 0.988235 0.992157 0.47451 0 0 0 0 0 0 0 0.188235 0.933333 0.87451 0.509804 0 0 0 0 0 0 0 0 0 0 0.992157 0.988235 0.937255 0.792157 0.988235 0.894118 0.082353 0 0 0 0 0 0 0.027451 0.647059 0.992157 0.654902 0 0 0 0 0 0 0 0 0 0 0 0.623529 0.988235 0.913725 0.329412 0.376471 0.184314 0 0 0 0 0 0 0.027451 0.513725 0.988235 0.635294 0.219608 0 0 0 0 0 0 0 0 0 0 0 0.196078 0.929412 0.988235 0.988235 0.741176 0.309804 0 0 0 0 0 0 0.529412 0.988235 0.678431 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.223529 0.992157 0.992157 1 0.992157 0.992157 0.992157 0.992157 1 0.992157 0.992157 0.882353 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.023529 0.478431 0.654902 0.658824 0.952941 0.988235 0.988235 0.988235 0.992157 0.988235 0.729412 0.278431 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.196078 0.647059 0.764706 0.764706 0.768627 0.580392 0.047059 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
4;0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.180392 0.470588 0.623529 0.623529 0.623529 0.588235 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.243137 0.494118 0.862745 0.870588 0.960784 0.996078 0.996078 0.996078 0.996078 0.992157 0.466667 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.317647 0.639216 0.639216 0.639216 0.639216 0.639216 0.470588 0.262745 0.333333 0.929412 0.694118 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.811765 0.694118 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.811765 0.694118 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.811765 0.694118 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.184314 0.992157 0.694118 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.192157 0.996078 0.384314 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.454902 0.980392 0.219608 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.564706 0.941176 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.588235 0.776471 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.945098 0.560784 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.054902 0.952941 0.356863 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.337255 0.917647 0.109804 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.698039 0.701961 0.019608 0.4 0.662745 0.662745 0.662745 0.662745 0.662745 0.662745 0.662745 0.376471 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.090196 0.639216 0.972549 0.945098 0.913725 0.996078 0.996078 0.996078 0.996078 1 0.996078 0.996078 1 0.996078 0 0 0 0 0 0 0 0 0 0 0.007843 0.105882 0.717647 0.776471 0.905882 0.996078 0.996078 0.988235 0.980392 0.862745 0.537255 0.223529 0.223529 0.368627 0.376471 0.6 0.6 0.6 0 0 0 0 0 0 0 0 0.262745 0.470588 0.6 0.996078 0.996078 0.996078 0.996078 0.847059 0.356863 0.156863 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.909804 0.705882 0.823529 0.635294 0.490196 0.219608 0.113725 0.062745 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.152941 0.152941 0.156863 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;

Each line of the data contains two parts, separated by ;. The first part is 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:

mnist_train.txt

The corresponding dataprovider is shown as below:

#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
from paddle.trainer.PyDataProvider2 import *


# Define a py data provider
@provider(
    input_types={'pixel': dense_vector(28 * 28),
                 'label': integer_value(10)})
def process(settings, filename):  # settings is not used currently.
    f = open(filename, 'r')  # open one of training file

    for line in f:  # read each line
        label, pixel = line.split(';')

        # get features and label
        pixels_str = pixel.split(' ')

        pixels_float = []
        for each_pixel_str in pixels_str:
            pixels_float.append(float(each_pixel_str))

        # give data to paddle.
        yield {"pixel": pixels_float, 'label': int(label)}

    f.close()  # close file

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.

@provider is a Python Decorator . It sets some properties to DataProvider, and constructs a real PaddlePaddle 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 just taking @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 from the original text file, convert them into input_types, and give them back to PaddlePaddle process at line 23. Note that data yielded by the process function must follow the same order that input_types are defined.

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

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

#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
from paddle.trainer_config_helpers import *

define_py_data_sources2(
    train_list='train.list',
    test_list=None,
    module='mnist_provider',
    obj='process')

img = data_layer(name='pixel', size=784)
label = data_layer(name='label', size=10)

Here we specify training data by train.list, and no testing data is specified. The method which actually provide data is process.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
from paddle.trainer.PyDataProvider2 import *


# Define a py data provider
@provider(
    input_types={'pixel': dense_vector(28 * 28),
                 'label': integer_value(10)})
def process(settings, filename):  # settings is not used currently.
    f = open(filename, 'r')  # open one of training file

    for line in f:  # read each line
        label, pixel = line.split(';')

        # get features and label
        pixels_str = pixel.split(' ')

        pixels_float = []
        for each_pixel_str in pixels_str:
            pixels_float.append(float(each_pixel_str))

        # give data to paddle.
        yield {"pixel": pixels_float, 'label': int(label)}

    f.close()  # close file

If user did’t give the data_layer‘s name, PaddlePaddle will use the order of data_layer definition roughly to determine which feature to which data_layer. This order may be not correct, so TO DEFINE THE data_layer‘s NAMES EXPLICITLY IS THE RECOMMANDED WAY TO PROVIDER DATA.

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 with time. It can also be explained to that the order of data are taken into 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:

0       I saw this movie at the AFI Dallas festival . It all takes place at a lake house and it looks wonderful .
1       This documentary makes you travel all around the globe . It contains rare and stunning sequels from the wilderness .
...

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

#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
from paddle.trainer.PyDataProvider2 import *


def on_init(settings, dictionary, **kwargs):
    # on_init will invoke when data provider is initialized. The dictionary
    # is passed from trainer_config, and is a dict object with type
    # (word string => word id).

    # set input types in runtime. It will do the same thing as
    # @provider(input_types) will do, but it is set dynamically during runtime.
    settings.input_types = {
        # The text is a sequence of integer values, and each value is a word id.
        # The whole sequence is the sentences that we want to predict its
        # sentimental.
        'data': integer_value_sequence(len(dictionary)),  # text input
        'label': integer_value(2)  # label positive/negative
    }

    # save dictionary as settings.dictionary. 
    # It will be used in process method.
    settings.dictionary = dictionary


@provider(init_hook=on_init)
def process(settings, filename):
    f = open(filename, 'r')

    for line in f:  # read each line of file
        label, sentence = line.split('\t')  # get label and sentence
        words = sentence.split(' ')  # get words

        # convert word string to word id
        # the word not in dictionary will be ignored.
        word_ids = []

        for each_word in words:
            if each_word in settings.dictionary:
                word_ids.append(settings.dictionary[each_word])

        # give data to paddle.
        yield word_ids, int(label)

    f.close()

This data provider for sequential model is a little more complex than that for MINST dataset. A new initialization method is introduced here. The method on_init is configured to DataProvider by @provider‘s init_hook parameter, and it will be invoked once DataProvider is initialized. The 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 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.

#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
from paddle.trainer_config_helpers import *

dictionary = dict()
...  #  read dictionary from outside

define_py_data_sources2(
    train_list='train.list',
    test_list=None,
    module='sentimental_provider',
    obj='process',
    # above codes same as mnist sample.
    args={  # pass to provider.
        'dictionary': dictionary
    })

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 on_init. It has the same effect to configure them by @provider‘s input_types parameter. However, the 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 seq_type to integer_value_sequence.

Durning on_init, we save dictionary variable to settings, and it will be used in 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 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

paddle.trainer.PyDataProvider2.provider(input_types=None, should_shuffle=None, pool_size=-1, min_pool_size=-1, can_over_batch_size=True, calc_batch_size=None, cache=0, check=False, check_fail_continue=False, init_hook=None, **outter_kwargs)

Provider decorator. Use it to make a function into PyDataProvider2 object. In this function, user only need to get each sample for some train/test file.

The basic usage is:

@provider(some data provider config here...)
def process(settings, file_name):
    while not at end of file_name:
        sample = readOneSampleFromFile(file_name)
        yield sample.

The configuration of data provider should be setup by:

Parameters:
  • input_types (list|tuple|dict) – Specify the input types, can also be set in init_hook. It could be a list of InputType object. For example, input_types=[dense_vector(9), integer_value(2)]. Or user can set a dict of InputType object, which key is data_layer’s name. For example, input_types= {‘img’: img_features, ‘label’: label}. when using dict of InputType, user could yield a dict of feature values, which key is also data_layer’s name.
  • should_shuffle (bool) – True if data should shuffle. Pass None means shuffle when is training and not to shuffle when is testing.
  • pool_size (int) – Max number of sample in data pool.
  • min_pool_size (int) – Set minimal sample in data pool. The PaddlePaddle will random pick sample in pool. So the min_pool_size effect the randomize of data.
  • can_over_batch_size (bool) –

    True if paddle can return a mini-batch larger than batch size in settings. It is useful when custom calculate one sample’s batch_size.

    It is very danger to set it to false and use calc_batch_size together. Default is true.

  • calc_batch_size (callable) – a method to calculate each sample’s batch size. Default each sample’s batch size is 1. But to you can customize each sample’s batch size.
  • cache (int) – Cache strategy of Data Provider. Default is CacheType.NO_CACHE
  • init_hook (callable) –

    Initialize hook. Useful when data provider need load some external data like dictionary. The parameter is (settings, file_list, **kwargs).

    • settings. It is the global settings object. User can set settings.input_types here.
    • file_list. All file names for passed to data provider.
    • is_train. Is this data provider used for training or not.
    • kwargs. Other keyword arguments passed from trainer_config’s args parameter.
  • check (bool) – Check the yield data format is as same as input_types. Enable this will make data provide process slow but it is very useful for debug. Default is disabled.
  • check_fail_continue (bool) – Continue train or not when check failed. Just drop the wrong format data when it is True. Has no effect when check set to False.

input_types

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

  • dense_vector: dense float vector.
  • sparse_binary_vector: sparse binary vector, most of the value is 0, and the non zero elements are fixed to 1.
  • 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.
  • integer: an integer scalar, that is especially used for label or word index.

The three sequence types are:

  • SequenceType.NO_SEQUENCE means the sample is not a sequence.
  • SequenceType.SEQUENCE means the sample is a sequence.
  • SequenceType.SUB_SEQUENCE means it is a nested sequence, that each timestep of 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:

  • The first parameter is a settings object, which is the same to settings in process method. The object contains several attributes, including:
    • settings.input_types: the input types. Reference input_types.
    • settings.logger: a logging object.
  • The rest parameters are the key word arguments. It is made up of PaddpePaddle pre-defined parameters and user defined parameters.
    • PaddlePaddle-defined parameters including:
      • is_train is a bool parameter that indicates the DataProvider is used in training or testing.
      • file_list is the list of all files.
    • User-defined parameters args can be set in training configuration.

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

cache

DataProvider provides two simple cache strategy. They are:

  • CacheType.NO_CACHE means do not cache any data, then data is read at runtime by the user implemented python module every pass.
  • CacheType.CACHE_PASS_IN_MEM means the first pass reads data by the user implemented python module, and the rest passes will directly read data from memory.