Tensor切片操作:省略号...

Tensor切片操作:省略号…

1、以torch的切片为例

import torch

a = torch.arange(20).reshape(4, 5)
print(a)
print(a[..., 0])
print(a[..., 1])
print(a[..., 2])

b = torch.arange(20).reshape(2, 2, 5)
print(b)
print(b[..., 0])
print(b[..., 1])
print(b[..., 2])

2、输出如下:

# a
tensor([[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]])
# a[..., 0]
tensor([ 0,  5, 10, 15])
# a[..., 1]
tensor([ 1,  6, 11, 16])
# a[..., 2]
tensor([ 2,  7, 12, 17])
# b
tensor([[[ 0,  1,  2,  3,  4],
         [ 5,  6,  7,  8,  9]],

        [[10, 11, 12, 13, 14],
         [15, 16, 17, 18, 19]]])
# b[..., 0]
tensor([[ 0,  5],
        [10, 15]])
# b[..., 1]
tensor([[ 1,  6],
        [11, 16]])
# b[..., 2]
tensor([[ 2,  7],
        [12, 17]])

结论:…是省略号的意思,忽略掉省略号所表示所有的维度,后面的数字表示取该维度的指定列

你可能感兴趣的:(Pytorch,python,深度学习,pytorch)