data_feed_desc.py 9.3 KB
Newer Older
W
Wang Guibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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.fluid.proto import data_feed_pb2
from google.protobuf import text_format

__all__ = ['DataFeedDesc']


class DataFeedDesc(object):
    """
23 24
    :api_attr: Static Graph
    
W
Wang Guibao 已提交
25 26 27 28
    Datafeed descriptor, describing input training data format. This class is
    currently only used for AsyncExecutor (See comments for class AsyncExecutor
    for a brief introduction)

29
    DataFeedDesc shall be initialized from a valid protobuf message from disk.
W
Wang Guibao 已提交
30 31 32 33

    See :code:`paddle/fluid/framework/data_feed.proto` for message definition.
    A typical message might look like:

34 35
    .. code-block:: python

36
      import paddle.fluid as fluid
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
      f = open("data.proto", "w")
      print >> f, 'name: "MultiSlotDataFeed"'
      print >> f, 'batch_size: 2'
      print >> f, 'multi_slot_desc {'
      print >> f, '    slots {'
      print >> f, '         name: "words"'
      print >> f, '         type: "uint64"'
      print >> f, '         is_dense: false'
      print >> f, '         is_used: true'
      print >> f, '     }'
      print >> f, '     slots {'
      print >> f, '         name: "label"'
      print >> f, '         type: "uint64"'
      print >> f, '         is_dense: false'
      print >> f, '         is_used: true'
      print >> f, '    }'
      print >> f, '}'
      f.close()
      data_feed = fluid.DataFeedDesc('data.proto')
W
Wang Guibao 已提交
56 57

    However, users usually shouldn't care about the message format; instead,
T
tianshuo78520a 已提交
58
    they are encouraged to use :code:`Data Generator` as a tool to generate a
W
Wang Guibao 已提交
59 60 61 62 63
    valid data description, in the process of converting their raw log files to
    training files acceptable to AsyncExecutor.

    DataFeedDesc can also be changed during runtime. Once you got familiar with
    what each field mean, you can modify it to better suit your need. E.g.:
64 65 66

    .. code-block:: python

67
      import paddle.fluid as fluid
68 69 70 71
      data_feed = fluid.DataFeedDesc('data.proto')
      data_feed.set_batch_size(128)
      data_feed.set_dense_slots('wd')  # The slot named 'wd' will be dense
      data_feed.set_use_slots('wd')    # The slot named 'wd' will be used
W
Wang Guibao 已提交
72 73

    Finally, the content can be dumped out for debugging purpose:
74 75 76 77

    .. code-block:: python

      print(data_feed.desc())
W
Wang Guibao 已提交
78 79 80

    Args:
        proto_file(string): Disk file containing a data feed description.
81

W
Wang Guibao 已提交
82 83 84 85
    """

    def __init__(self, proto_file):
        self.proto_desc = data_feed_pb2.DataFeedDesc()
D
dongdaxiang 已提交
86
        self.proto_desc.pipe_command = "cat"
W
Wang Guibao 已提交
87 88 89 90 91 92 93 94 95 96
        with open(proto_file, 'r') as f:
            text_format.Parse(f.read(), self.proto_desc)
        if self.proto_desc.name == "MultiSlotDataFeed":
            self.__name_to_index = {
                slot.name: i
                for i, slot in enumerate(self.proto_desc.multi_slot_desc.slots)
            }

    def set_batch_size(self, batch_size):
        """
Z
zhoushiyu 已提交
97
        Set :attr:`batch_size` in :ref:`api_fluid_DataFeedDesc` . :attr:`batch_size` can be changed during training.
W
Wang Guibao 已提交
98 99

        Example:
100 101
            .. code-block:: python

102
              import paddle.fluid as fluid
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
              f = open("data.proto", "w")
              print >> f, 'name: "MultiSlotDataFeed"'
              print >> f, 'batch_size: 2'
              print >> f, 'multi_slot_desc {'
              print >> f, '    slots {'
              print >> f, '         name: "words"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '     }'
              print >> f, '     slots {'
              print >> f, '         name: "label"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '    }'
              print >> f, '}'
              f.close()
              data_feed = fluid.DataFeedDesc('data.proto')
              data_feed.set_batch_size(128)
W
Wang Guibao 已提交
123 124

        Args:
Z
zhoushiyu 已提交
125 126 127 128
            batch_size (int): The number of batch size.

        Returns:
            None.
W
Wang Guibao 已提交
129 130 131 132 133 134

        """
        self.proto_desc.batch_size = batch_size

    def set_dense_slots(self, dense_slots_name):
        """
Z
zhoushiyu 已提交
135 136 137 138
        Set slots in :attr:`dense_slots_name` as dense slots. **Note: In default, all slots are sparse slots.**
 
        Features for a dense slot will be fed into a Tensor, while those for a
        sparse slot will be fed into a LoDTensor.
W
Wang Guibao 已提交
139 140

        Example:
141 142
            .. code-block:: python

143
              import paddle.fluid as fluid
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
              f = open("data.proto", "w")
              print >> f, 'name: "MultiSlotDataFeed"'
              print >> f, 'batch_size: 2'
              print >> f, 'multi_slot_desc {'
              print >> f, '    slots {'
              print >> f, '         name: "words"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '     }'
              print >> f, '     slots {'
              print >> f, '         name: "label"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '    }'
              print >> f, '}'
              f.close()
              data_feed = fluid.DataFeedDesc('data.proto')
              data_feed.set_dense_slots(['words'])
W
Wang Guibao 已提交
164 165

        Args:
Z
zhoushiyu 已提交
166 167 168 169
            dense_slots_name (list(str)): a list of slot names which will be set dense.

        Returns:
            None.
W
Wang Guibao 已提交
170 171 172 173

        """
        if self.proto_desc.name != "MultiSlotDataFeed":
            raise ValueError(
174
                "Only MultiSlotDataFeed needs set_dense_slots, please check your datafeed.proto"
W
Wang Guibao 已提交
175 176
            )
        for name in dense_slots_name:
177 178
            self.proto_desc.multi_slot_desc.slots[
                self.__name_to_index[name]].is_dense = True
W
Wang Guibao 已提交
179 180 181 182 183 184 185 186

    def set_use_slots(self, use_slots_name):
        """
        Set if a specific slot will be used for training. A dataset shall
        contain a lot of features, through this function one can select which
        ones will be used for a specific model.

        Example:
187 188
            .. code-block:: python

189
              import paddle.fluid as fluid
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
              f = open("data.proto", "w")
              print >> f, 'name: "MultiSlotDataFeed"'
              print >> f, 'batch_size: 2'
              print >> f, 'multi_slot_desc {'
              print >> f, '    slots {'
              print >> f, '         name: "words"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '     }'
              print >> f, '     slots {'
              print >> f, '         name: "label"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '    }'
              print >> f, '}'
              f.close()
              data_feed = fluid.DataFeedDesc('data.proto')
              data_feed.set_use_slots(['words'])
W
Wang Guibao 已提交
210 211 212 213 214 215 216 217 218

        Args:
            use_slots_name: a list of slot names which will be used in training

        Note:
            Default is not used for all slots
        """
        if self.proto_desc.name != "MultiSlotDataFeed":
            raise ValueError(
219
                "Only MultiSlotDataFeed needs set_use_slots, please check your datafeed.proto"
W
Wang Guibao 已提交
220 221
            )
        for name in use_slots_name:
222 223
            self.proto_desc.multi_slot_desc.slots[
                self.__name_to_index[name]].is_used = True
W
Wang Guibao 已提交
224 225 226 227 228 229

    def desc(self):
        """
        Returns a protobuf message for this DataFeedDesc

        Example:
230 231
            .. code-block:: python

232
              import paddle.fluid as fluid
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
              f = open("data.proto", "w")
              print >> f, 'name: "MultiSlotDataFeed"'
              print >> f, 'batch_size: 2'
              print >> f, 'multi_slot_desc {'
              print >> f, '    slots {'
              print >> f, '         name: "words"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '     }'
              print >> f, '     slots {'
              print >> f, '         name: "label"'
              print >> f, '         type: "uint64"'
              print >> f, '         is_dense: false'
              print >> f, '         is_used: true'
              print >> f, '    }'
              print >> f, '}'
              f.close()
              data_feed = fluid.DataFeedDesc('data.proto')
              print(data_feed.desc())
W
Wang Guibao 已提交
253 254 255 256 257

        Returns:
            A string message
        """
        return text_format.MessageToString(self.proto_desc)