假设n是一个numpy或者torch.tensor张量,那么
n[a:b,c:d]
则代表从a到b行(不含b行),c到d列(不含d列)的切片
当然,ab、cd中的数是可以省略掉,只要abcd中有一个数即可
import torch
m = [[-1.5805, 1.6342, 0.2004, 0.1923, 0.2503, -0.0467, -1.9203], \
[-1.6402, 0.5139, 1.8963, 0.5725, 0.4209, 0.6523, -1.6669], \
[-1.8716, 0.0378, 0.5280, 1.3742, 0.5176, 0.3444, -1.4203], \
[-1.7557, 0.1472, 0.0563, 0.5518, 1.3115, 0.0915, -1.7161], \
[-1.9358, 0.1463, -0.2262, 0.3781, 0.2074, 1.5654, -1.4256]]
n = torch.tensor(a)
print(n)
print(n[1:4,0:2])
print(n[:4,0:2])
print(n[1:4,:2])
print(n[1:,])
标签:tensor,python,torch,矩阵,切片,print
From: https://www.cnblogs.com/chengjunkai/p/16724225.html