笔记:对多维torch进行任意维度的多“行”操作

从二维torch开始

新建torch

import torch

# 新建一个二维 torch 
a = torch.tensor ( [[1,2,3,4],
                    [2,3,1,5],
                    [5,1,7,2]])
a.shape

Out[5]: torch.Size([3, 4])

取出某一行

a[1]   # 取出第1行(从0行开始)

Out[6]: tensor([2, 3, 1, 5])

a[1].shape

Out[28]: torch.Size([4])

取出某一列

a[:,2]   # 虽说取出的是第2列,但还是以行的形式显示

Out[26]: tensor([3, 1, 7])

a[:,2].shape

Out[27]: torch.Size([3])

一次性取出多行

取出连续的多行

——有多种操作方式

######## scheme 1 ##########
a[[0, 1]]  # 取出前两行
### 此时需注意,需要使用两个中括号,使用 a[0,1] 的格式取出的是 a 的第0行第1列的具体某个元素“tensor(2)”

Out[7]: 
tensor([[1, 2, 3, 4],
        [2, 3, 1, 5]])

######## scheme 2 ###########
a[[range(2)]]   # 取出前两行
### 当没有别的指示(如冒号等)时,默认对dim=0进行操作

Out[8]: 
tensor([[1, 2, 3, 4],
        [2, 3, 1, 5]])

######### scheme 3 ###########
a[range(2)]   # 也可以不使用两个中括号
Out[31]: 
tensor([[1, 2, 3, 4],
        [2, 3, 1, 5]])     
取出不连续的多行

如:取出第0行和第2行

a[[0,2]]

Out[38]: 
tensor([[1, 2, 3, 4],
        [5, 1, 7, 2]])

一次取出多列

取出连续的多列

——同样拥有多种方案

############## scheme 1 ###############
a[:,range(2)]  # 取出前两列

Out[31]: 
tensor([[1, 2],
        [2, 3],
        [5, 1]])

############ scheme 2 #################
a[:,[0,1]]
Out[32]: 
tensor([[1, 2],
        [2, 3],
        [5, 1]])
取出不连续的多列

如取出第0列和第3列

a[:,[0,3]]

Out[40]: 
tensor([[1, 4],
        [2, 5],
        [5, 2]])

考虑三维torch

# 新建一个三维torch
b = torch.tensor([ [[1, 2, 3, 7], [4, 5, 6, 9]], 
[[7, 8, 9, 2], [10, 11, 12, 3]], 
[[13, 14, 15, 4], [16, 17, 18, 6]]])

b.shape

Out[10]: torch.Size([3, 2, 4])

b

Out[11]: 
tensor([[[ 1,  2,  3,  7],
         [ 4,  5,  6,  9]],
        [[ 7,  8,  9,  2],
         [10, 11, 12,  3]],
        [[13, 14, 15,  4],
         [16, 17, 18,  6]]])

此三维torch可视化如下:
在这里插入图片描述

取出三维torch的任意两行(means 在dim=0上操作)

取出连续的行

以前两行为例

b[range(2)]   # 还是使用中括号

Out[12]: 
tensor([[[ 1,  2,  3,  7],
         [ 4,  5,  6,  9]],
        [[ 7,  8,  9,  2],
         [10, 11, 12,  3]]])

b[range(2)].shape

Out[13]: torch.Size([2, 2, 4])

b[range(2)] == b[[range(2)]] # 使用一个中括号还是两个中括号,都是一样的效果
                             ### 但是不能使用三个,shape 会变成 torch.Size([1,2,2,4])

Out[34]: 
tensor([[[True, True, True, True],
         [True, True, True, True]],
        [[True, True, True, True],
         [True, True, True, True]]])
取出不连续的行

如取出第0行和第2行

b[[0,2]]

Out[42]: 
tensor([[[ 1,  2,  3,  7],
         [ 4,  5,  6,  9]],
        [[13, 14, 15,  4],
         [16, 17, 18,  6]]])

取出任意列

取出连续的列 & 取出任意列
######### 取出中间维度(dim=1)的前一列
b[:,range(1)].shape 

Out[19]: torch.Size([3, 1, 4])

b[:,range(1)]

Out[25]: 
tensor([[[ 1,  2,  3,  7]],
        [[ 7,  8,  9,  2]],
        [[13, 14, 15,  4]]])

############# 取出前两列
b[:,range(2)]   

Out[43]: 
tensor([[[ 1,  2,  3,  7],
         [ 4,  5,  6,  9]],
        [[ 7,  8,  9,  2],
         [10, 11, 12,  3]],
        [[13, 14, 15,  4],
         [16, 17, 18,  6]]])
         
b[:,range(2)].shape

Out[44]: torch.Size([3, 2, 4])

############## 取出任意一列
b[:,1]

Out[46]: 
tensor([[ 4,  5,  6,  9],
        [10, 11, 12,  3],
        [16, 17, 18,  6]])

取出任意页(dim=2)

取出前n页
##################### 取出前两页
b[:,:,range(2)]

Out[47]: 
tensor([[[ 1,  2],
         [ 4,  5]],
        [[ 7,  8],
         [10, 11]],
        [[13, 14],
         [16, 17]]])
         
b[:,:,range(2)].shape

Out[48]: torch.Size([3, 2, 2])
取出任意页

如取出第0页,第2页和第3页

b[:,:,[0,2,3]]

Out[49]: 
tensor([[[ 1,  3,  7],
         [ 4,  6,  9]],
        [[ 7,  9,  2],
         [10, 12,  3]],
        [[13, 15,  4],
         [16, 18,  6]]])
         
b[:,:,[0,2,3]].shape

Out[50]: torch.Size([3, 2, 3])

else:取出dim=0/dim=1/dim=2的任意元素操作

待补充

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值