paddle 基础函数 expand,expand_as

expand 官方文档:https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/expand_cn.html

paddle.fluid.layers.expand(xexpand_timesname=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(xtarget_tensorname=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)

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值