empty¶
创建形状大小为 shape 并且数据类型为 dtype 的 Tensor,其中元素值是未初始化的。
参数¶
shape (list|tuple|Tensor) – 指定创建 Tensor 的形状(shape),数据类型为 int32 或者 int64。
dtype (np.dtype|str,可选)- 输出变量的数据类型,可以是 bool、float16、float32、float64、int32、int64。若为 None,则输出变量的数据类型为系统全局默认类型,默认值为 None。
name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。
返回¶
返回一个根据 shape
和 dtype
创建并且尚未初始化的 Tensor。
代码示例¶
import paddle
paddle.set_device("cpu") # and use cpu device
# example 1: argument ``shape`` is a list which doesn't contain Tensor.
data1 = paddle.empty(shape=[2, 3], dtype='float32')
print(data1)
# Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[0.00000000, 0. , 0.00000000],
# [0. , 0.29652897, 0.09356152]]) # uninitialized
# example 2: argument ``shape`` is a Tensor, the data type must be int64 or int32.
shape_data = paddle.to_tensor([2, 3]).astype('int32')
data2 = paddle.empty(shape=shape_data, dtype='float32')
print(data2)
# Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[-0.50543123, -0.09872390, -0.92634487],
# [-0.51007903, -0.02454148, 1.29315734]]) # uninitialized
# example 3: argument ``shape`` is a list which contains Tensor.
dim2 = paddle.to_tensor([3]).astype('int32')
data3 = paddle.empty(shape=[2, dim2], dtype='float32')
print(data3)
# Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
# [[ 0.00000000, 0. , -0.92634487],
# [-0.51007903, -0.02454148, 1.29315734]]) # uninitialized