paddle之squeeze()函数详解

官网注解

paddle.squeeze(xaxis=Nonename=None)

该OP会删除输入Tensor的Shape中尺寸为1的维度。如果指定了axis,则会删除axis中指定的尺寸为1的维度。如果没有指定axis,那么所有等于1的维度都会被删除。

请注意,在动态图模式下,输出Tensor将与输入Tensor共享数据,并且没有Tensor数据拷贝的过程。 如果不希望输入与输出共享数据,请使用 Tensor.clone ,例如 squeeze_clone_x = x.squeeze().clone() 。

Case 1:

    Input:
    x.shape = [1, 3, 1, 5]  # If axis is not provided, all dims equal of size 1 will be removed.
     #(翻译)如果未提供轴,则所有大小为1的暗边都将被删除。
    axis = None
    Output:
    out.shape = [3, 5]

Case 2:

    Input:
    x.shape = [1, 3, 1, 5]  # If axis is provided, it will remove the dimension(s) by given axis that of size 1.
    #(翻译)如果提供了坐标轴,它将根据给定的大小为1的坐标轴删除维度。
    axis = 0
    Output:
    out.shape = [3, 1, 5]

Case 3:

    Input:
    x.shape = [1, 3, 1, 5]  # If the dimension of one given axis (3) is not of size 1, the dimension remain unchanged.
    # 如果给定轴(3)的尺寸不为1,则其尺寸保持不变。
    axis = [0, 2, 3]
    Output:
    out.shape = [3, 5]

Case 4:

    Input:
    x.shape = [1, 3, 1, 5]  # If axis is negative, axis = axis + ndim (number of dimensions in x).
    # 如果axis为负数,则axis = axis + ndim (x中的维数)。
    axis = [-2]
    Output:
    out.shape = [1, 3, 5]

 

参数

  • x (Tensor) - 输入的 Tensor ,数据类型为:float32、float64、bool、int8、int32、int64。

  • axis (int|list|tuple, 可选) - 输入一个或一列整数,代表要压缩的轴。axis的范围: [−ndim(x), ndim(x))] 。 如果axis为负数, 则axis=axis+ndim(x) 。默认为None,表示对所有尺寸为1的维度进行压缩。

  • name (str, 可选) - 操作的名称(可选,默认值为None)。更多信息请参见 Name

返回

对维度进行压缩后的Tensor,数据类型与输入Tensor一致。

 

import paddle

x = paddle.rand([5, 1, 10])
output = paddle.squeeze(x, axis=1)

print(x.shape)  # [5, 1, 10]
print(output.shape)  # [5, 10]

# 在动态图模式下,输出output与输入x共享数据
x[0, 0, 0] = 10.
print(output[0, 0]) # [10.]

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值