pytorch中的[..., 0]/[0,...]/[0,...,0]等用法

在看程序的时候看到了x[…, 0]的语句不是很理解,后来自己做实验略微了解,以此记录方便自己查看。

b=torch.Tensor([[[[10,2],[4,5],[7,8]],[[1,2],[4,5],[7,8]]]])
print(b.size())
(1, 2, 3, 2)
print(b[…,0])
tensor([[[10., 4., 7.],
[ 1., 4., 7.]]])
print(b[…,0].size())
(1, 2, 3)
print(b[…,2])
Traceback (most recent call last):
File “”, line 1, in
IndexError: index 2 is out of bounds for dimension 3 with size 2

print(b[0,…])
tensor([[[10., 2.],
[ 4., 5.],
[ 7., 8.]],
[[ 1., 2.],
[ 4., 5.],
[ 7., 8.]]])
print(b[0,…].size())
(2, 3, 2)

print(b[0,…,0].size())
(2, 3)
print(b[0,…,0])
tensor([[10., 4., 7.],
[ 1., 4., 7.]])

[…, 0]表示抽取tensor b的第4根轴上的第一列数字组成tensor,[0, …]表示抽取tensor b的第一根轴上的第一列数字组成tensor,[0, …, 0]表示抽取b的第一根和第四根轴上的第一列数字组成tensor。
还发现一个现象

print(b[…,0:])
tensor([[[[10., 2.],
[ 4., 5.],
[ 7., 8.]],
[[ 1., 2.],
[ 4., 5.],
[ 7., 8.]]]])

print(b[…,1:])
tensor([[[[2.],
[5.],
[8.]],
[[2.],
[5.],
[8.]]]])

print(b[…,2:])
tensor([], size=(1, 2, 3, 0))

 
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树人工智能深度学习134510 人正在系统学习中
posted @ 2022-09-27 09:26  海_纳百川  阅读(55)  评论(0编辑  收藏  举报
本站总访问量