How to use 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 a same size of 28 x 28 pixels.

A small part of the original data as an example can be found in the path 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 can be found in the path below:

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 sample user implemented python function. It does not matter if you are not familiar with Decorator. You can keep it sample 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, converted them into input_types, and give them back to PaddlePaddle process at line 23. Note that data yields by the process function must follow a 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.

from paddle.trainer_config_helpers import *

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

Here we specify training data by ‘train.list’, and no testing data is specified.

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:

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.
        integer_value(len(dictionary), seq_type=SequenceType),  # text input

        # label positive/negative
        integer_value(2)
    ]

    # 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 bit 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.

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

'@provider‘ is a Python Decorator, it can construct a PyDataProvider in PaddlePaddle from a user defined function. Its parameters are:

  • input_types defines format of the data input.
  • should_shuffle defines whether to shuffle data or not. By default, it is set true during training, and false during testing.
  • pool_size is the memory pool size (in sample number) in DataProvider. -1 means no limit.
  • can_over_batch_size defines whether PaddlePaddle can store little more samples than pool_size. It is better to set True to avoid some deadlocks.
  • calc_batch_size is a function define how to calculate batch size. This is usefull in sequential model, that defines batch size is counted upon sequence or token. By default, each sample or sequence counts to 1 when calculating batch size.
  • cache is a data cache strategy, see cache
  • Init_hook function is invoked once the data provider is initialized, see init_hook

input_types

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

  • dense_vector represents 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 that can be any float value. They are given by the user.
  • integer represents 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 :code:’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 defines 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 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.