expand 官方文档:https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/expand_cn.html
paddle.fluid.layers.
expand
(x, expand_times, name=None)
根据参数 expand_times
对输入 x
的各维度进行复制。通过参数 expand_times
来为 x
的每个维度设置复制次数。
expand_as官方文档:https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/expand_as_cn.html
paddle.fluid.layers.
expand_as
(x, target_tensor, name=None)
根据输入的variable target_tensor
对输入 x
的各维度进行广播。
expand 示例:
import paddle.fluid as fluid
# example 1:
data_1 = fluid.layers.fill_constant(shape=[2, 3, 1], dtype='int32', value=0)
expanded_1 = fluid.layers.expand(data_1, expand_times=[1, 2, 2])
# the shape of expanded_1 is [2, 6, 2].
print(expanded_1.shape)
结果:
[2, 6, 2]
expand_as 示例:
import paddle.fluid as fluid
import numpy as np
data = fluid.data(name="data", shape=[-1,10], dtype='float64')
target_tensor = fluid.data(name="target_tensor", shape=[-1,20], dtype='float64')
result = fluid.layers.expand_as(x=data, target_tensor=target_tensor)
use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
x = np.random.rand(3,10)
y = np.random.rand(3,20)
output= exe.run(feed={"data":x,
"target_tensor":y},
fetch_list=[result.name])
print(output)
#(3,20)
结果: