Tensor索引操作

记录下tensor的索引操作吧

一般地,如果 a是一个tensor,则a[i:j, m:n] 是取tensor a的i到j-1行和m到n-1列(行号和列号都从0开始)

下面是一些例子

a = torch.rand(3,3)
a
Out[5]: 
tensor([[0.4887, 0.3113, 0.0250],
        [0.7234, 0.9017, 0.6038],
        [0.1178, 0.0292, 0.2529]])
a[1]	#输出tensor a的第1行
Out[6]: tensor([0.7234, 0.9017, 0.6038])
a[:, :1]
Out[8]: tensor([[0.4887],
        		[0.7234],
        		[0.1178]])
a[:, :2]	# tensor a 的所有行,0到1列
Out[7]: tensor([[0.4887, 0.3113],
        		[0.7234, 0.9017],
        		[0.1178, 0.0292]])
a[:, :3]
Out[9]: tensor([[0.4887, 0.3113, 0.0250],
        		[0.7234, 0.9017, 0.6038],
        		[0.1178, 0.0292, 0.2529]])
a[:2, :3]
Out[10]: tensor([[0.4887, 0.3113, 0.0250],
        		[0.7234, 0.9017, 0.6038]])
a[0:2, :3]
Out[11]: tensor([[0.4887, 0.3113, 0.0250],
        		[0.7234, 0.9017, 0.6038]])

你可能感兴趣的:(Tensor索引操作)