Pytorch中的 flatten() ,squeeze() 和 unsqueeze() 的区分

解释

flatten() 用于将数据展开。

squeeze() 用于将数据进行压缩,移除某个维度

  • Compute torch.squeeze(input). It squeezes (removes) the size 1 and returns a tensor with all other dimensions of the input tensor.

unsqueeze() 用于将数据解压缩,扩充一个维度

  • Compute torch.unsqueeze(input, dim). It inserts a new dimension of size 1 at the given dim and returns the tensor.

举例:

# Python program to squeeze and unsqueeze a tensor
# import necessary library
import torch

# Create a tensor of all one
T = torch.ones(2,1,2) # size 2x1x2
print("Original Tensor T:\n", T )
print("Size of T:", T.size())

原数据 T 的输出:

在这里插入图片描述

原数据 T 的 flatten() 输出

T.flatten()
torch.flatten() 

在这里插入图片描述

原数据 T 的 squeeze() 输出

T.squeeze(0)
troch.squeeze(T)

注意观察,两者的维度,(中括号个数)
在这里插入图片描述

原数据 T 的 unsqueeze() 输出

T.unsqueeze(dim=0)
torch.unsqueeze(T, dim=0)

在这里插入图片描述

参考链接

https://www.tutorialspoint.com/how-to-squeeze-and-unsqueeze-a-tensor-in-pytorch

https://pytorch.org/docs/stable/generated/torch.squeeze.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值