美文网首页
(五)索引与切片

(五)索引与切片

作者: zelda2333 | 来源:发表于2020-02-12 10:32 被阅读0次

1. 索引

# [b, c,h, w]
a = torch.rand(4,3,28,28)  

取第0张图片

In [12]:   a[0].shape
Out[13]:   torch.Size([3, 28, 28])

取第0张图片的第0个通道

In [12]:   a[0, 0].shape
Out[13]:   torch.Size([28, 28])

取第0张图片的第0个通道的2行第4列的像素点

In [12]:   a[0, 0, 2, 4]
Out[13]:   tensor(0.8072)

2. select first/last N

a[:2] ==[0,2)


3. select by steps

    1. all
    1. n: 等价于 n<--end
      :n 等价于 start-->n
    1. start:end 等价于 [start:end)
    1. start:end:2
      每隔阂一行取
      默认start:end:1

4. select by specific index

  • a.index_select(dim, 索引号)

  • ...代表任意长的维度

4. select by mask

5. select by flatten index

相关文章

网友评论

      本文标题:(五)索引与切片

      本文链接:https://www.haomeiwen.com/subject/eeklactx.html