data.py 3.6 KB
Newer Older
H
Huihuang Zheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   Copyright (c) 2019 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.

import numpy as np

from . import core
from .layer_helper import LayerHelper

__all__ = ['data']


23
def data(name, shape, dtype='float32', lod_level=0):
H
Huihuang Zheng 已提交
24 25 26
    """
    **Data Layer**

27 28 29 30
    This function creates a variable on the global block. The global variable
    can be accessed by all the following operators in the graph. The variable
    is a placeholder that could be feeded with input, such as Executor can feed
    input into the variable.
H
Huihuang Zheng 已提交
31 32 33 34 35

    Note: 
       `paddle.fluid.layers.data` is deprecated. It will be removed in a future
       version. Please use this `paddle.fluid.data`. 
       
36 37 38 39
       The `paddle.fluid.layers.data` set shape and dtype at compile time but
       does NOT check the shape or the dtype of feeded data, this
       `paddle.fluid.data` checks the shape and the dtype of data feeded by
       Executor or ParallelExecutor during run time.
H
Huihuang Zheng 已提交
40 41

    Args:
42 43
       name (str): The name/alias of the variable, see :ref:`api_guide_Name`
           for more details.
H
Huihuang Zheng 已提交
44
       shape (list|tuple): List|Tuple of integers declaring the shape.
45 46 47 48 49 50
       dtype (np.dtype|VarType|str, optional): The type of the data. Supported
           dtype: bool, float16, float32, float64, int8, int16, int32, int64,
           uint8. Default: float32
       lod_level (int, optional): The LoD level of the LoDTensor. Usually users
           don't have to set this value. For more details about when and how to
           use LoD level, see :ref:`user_guide_lod_tensor` . Default: 0
H
Huihuang Zheng 已提交
51 52 53 54 55 56 57 58

    Returns:
        Variable: The global variable that gives access to the data.

    Examples:
        .. code-block:: python

          import paddle.fluid as fluid
59
          import numpy as np
H
Huihuang Zheng 已提交
60

61
          # Creates a variable with fixed size [3, 2, 1]
H
Huihuang Zheng 已提交
62
          # User can only feed data of the same shape to x
63
          x = fluid.data(name='x', shape=[3, 2, 1], dtype='float32')
H
Huihuang Zheng 已提交
64 65

          # Creates a variable with changable batch size -1.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
          # Users can feed data of any batch size into y,
          # but size of each data sample has to be [2, 1]
          y = fluid.data(name='y', shape=[-1, 2, 1], dtype='float32')

          z = x + y

          # In this example, we will feed x and y with np-ndarry "1"
          # and fetch z, like implementing "1 + 1 = 2" in PaddlePaddle
          feed_data = np.ones(shape=[3, 2, 1], dtype=np.float32)

          exe = fluid.Executor(fluid.CPUPlace())
          out = exe.run(fluid.default_main_program(),
                        feed={
                            'x': feed_data,
                            'y': feed_data
                        },
                        fetch_list=[z.name])

          # np-ndarray of shape=[3, 2, 1], dtype=float32, whose elements are 2
          print(out)
H
Huihuang Zheng 已提交
86 87 88 89 90 91 92

    """
    helper = LayerHelper('data', **locals())
    return helper.create_global_variable(
        name=name,
        shape=shape,
        dtype=dtype,
93
        type=core.VarDesc.VarType.LOD_TENSOR,
H
Huihuang Zheng 已提交
94
        stop_gradient=True,
95
        lod_level=lod_level,
H
Huihuang Zheng 已提交
96 97
        is_data=True,
        need_check_feed=True)