pytorch-tensor-维度

背景:tensor的维度一直令我疑惑,在此记录一下。


注意: 首先,能添加的维度是有限制的,假设为tensor添加维度,其中添加的维度是有个范围限制(左闭右开)的:
[ − tensor . d i m ( ) − 1 , tensor . d i m ( ) + 1 ) [-\text{tensor}.dim() - 1, \text{tensor}.dim() + 1) [tensor.dim()1,tensor.dim()+1)当添加的维度为负数时,可以根据以下公式转换到正数的维度 dim = dim + tensor . d i m ( ) + 1. \text{dim} = \text{dim} + \text{tensor}.dim() + 1. dim=dim+tensor.dim()+1.

0维度指的是最外面的维度, 也就是最外层的

a = torch.rand(3, 3)
print(a)

b = a.unsqueeze(0) # 添加一个0维度
print(b)

tensor([[0.7927, 0.5813, 0.9975],
        [0.0247, 0.5305, 0.5472],
        [0.5818, 0.2442, 0.4680]])
tensor([[[0.7927, 0.5813, 0.9975],
         [0.0247, 0.5305, 0.5472],
         [0.5818, 0.2442, 0.4680]]])

可以看到,在看到在0维度添加一个维度以后,在最外面多了一个。 以此类推,一个tensor的维度的最大值指的是最里面的那个

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值