1. 前言
在torch和numpy中经常会遇到对tensor进行切片操作,如x[..., :3], [:, :2]等,对于:的操作很好理解,与python列表中操作相同。而...就是在切片的过程中自动判断维度的意思,大概就是总size-逗号个数,所以在切片操作中不会出现两个省略号。
2. 示例
以torch中的切片为例,如下:
import torch
x = torch.rand((2,3,4))
print(x)
y = x[..., 2:4]
z = x[..., :, -1]
k = x[:, :, -1]
j = x[1, ...]
print("y", y)
print("z:", z)
print("k", k)
print("j", j)
输出如下:
tensor([[[0.2626, 0.8649, 0.0496, 0.0013],
[0.5466, 0.1964, 0.1133, 0.5052],
[0.8863, 0.2080, 0.0276, 0.0924]],
[[0.4060, 0.8400, 0.4646, 0.3968],
[0.2729, 0.3125, 0.7060, 0.3351],
[0.3627, 0.8817, 0.9210, 0.3325]]])
y: tensor([[[0.0496, 0.0013],
[0.1133, 0.5052],
[0.0276, 0.0924]],
[[0.4646, 0.3968],
[0.7060, 0.3351],
[0.9210, 0.3325]]])
z: tensor([[0.0013, 0.5052, 0.0924],
[0.3968, 0.3351, 0.3325]])
k tensor([[0.0013, 0.5052, 0.0924],
[0.3968, 0.3351, 0.3325]])
j tensor([[0.4060, 0.8400, 0.4646, 0.3968],
[0.2729, 0.3125, 0.7060, 0.3351],
[0.3627, 0.8817, 0.9210, 0.3325]])
通过实际案例也可以发现:...代替了他前面所有的:,例如上面案例中y,他里面的...代替了第0维和第1维的两个冒号,而对于z案例的情况省略号和冒号的作用相同。
标签:...,冒号,tensor,python,0.3325,0.3968,0.3351,print,多维 From: https://www.cnblogs.com/tangjielin/p/16786425.html