data

paddle.static. data ( name, shape, dtype=None, lod_level=0 ) [源代码]

该OP会在全局block中创建变量(Tensor),该全局变量可被计算图中的算子(operator)访问。该变量可作为占位符用于数据输入。例如用执行器(Executor)feed数据进该变量,当 dtype 为None时, dtype 将通过 padle.get_default_dtype() 获取全局类型。

参数:

  • name (str)- 被创建的变量的名字,具体用法请参见 Name

  • shape (list|tuple)- 声明维度信息的list或tuple。可以在某个维度上设置None或-1,以指示该维度可以是任何大小。例如,将可变batchsize设置为None或-1。

  • dtype (np.dtype|str,可选)- 数据类型,支持bool,float16,float32,float64,int8,int16,int32,int64,uint8。默认值为None。当 dtype 为None时, dtype 将通过 padle.get_default_dtype() 获取全局类型。

  • lod_level (int,可选)- LoDTensor变量的LoD level数,LoD level是PaddlePaddle的高级特性,一般任务中不会需要更改此默认值,关于LoD level的详细适用场景和用法请见 cn_user_guide_lod_tensor 。默认值为0。

返回:Tensor, 全局变量,可进行数据访问

代码示例

  1. import numpy as np
  2. import paddle
  3. paddle.enable_static()
  4. # Creates a variable with fixed size [3, 2, 1]
  5. # User can only feed data of the same shape to x
  6. # the dtype is not set, so it will set "float32" by
  7. # paddle.get_default_dtype(). You can use paddle.get_default_dtype() to
  8. # change the global dtype
  9. x = paddle.static.data(name='x', shape=[3, 2, 1])
  10. # Creates a variable with changeable batch size -1.
  11. # Users can feed data of any batch size into y,
  12. # but size of each data sample has to be [2, 1]
  13. y = paddle.static.data(name='y', shape=[-1, 2, 1], dtype='float32')
  14. z = x + y
  15. # In this example, we will feed x and y with np-ndarray "1"
  16. # and fetch z, like implementing "1 + 1 = 2" in PaddlePaddle
  17. feed_data = np.ones(shape=[3, 2, 1], dtype=np.float32)
  18. exe = paddle.static.Executor(paddle.framework.CPUPlace())
  19. out = exe.run(paddle.static.default_main_program(),
  20. feed={
  21. 'x': feed_data,
  22. 'y': feed_data
  23. },
  24. fetch_list=[z.name])
  25. # np-ndarray of shape=[3, 2, 1], dtype=float32, whose elements are 2
  26. print(out)

使用本API的教程文档