光之使者 发表于 2024-6-22 16:31:51

pytorch 使用tensor肴杂:进行index操作

(Pdb) tmp = torch.randn(3,5)
(Pdb) indx = torch.tensor().long()
(Pdb) temp(indx)
*** NameError: name ‘temp’ is not defined
(Pdb) tmp(indx)
*** TypeError: ‘Tensor’ object is not callable
(Pdb) tmp
tensor([[ 0.1633, 0.9389, 1.2806, -0.2525, 0.2817],
[ 0.6204, 0.5973, -1.7741, 0.3721, -0.5338]])
(Pdb) tmp
tensor([[ 0.6204, 0.5973, -1.7741, 0.3721, -0.5338],
[ 0.1633, 0.9389, 1.2806, -0.2525, 0.2817],
[ 0.4279, -0.2156, 2.4653, 0.3173, -0.0719]])
(Pdb) indx
tensor()
(Pdb) indx2= torch.tensor([]).long()
(Pdb) index2
*** NameError: name ‘index2’ is not defined
(Pdb) indx2
tensor([])
(Pdb) indx2.shape
torch.Size()
(Pdb) tmp
tensor([[[ 0.1633, 0.9389, 1.2806, -0.2525, 0.2817],
[ 0.6204, 0.5973, -1.7741, 0.3721, -0.5338]]])
(Pdb) tmp.shape
torch.Size()
(Pdb) tmp[:,indx2].shape
torch.Size()
(Pdb) tmp[:,indx2]
tensor([[[ 0.5973, 0.6204]],
[[ 0.9389, 0.1633]],
[[-0.2156, 0.4279]]])
(Pdb) t=torch.arange(10)
(Pdb) t
tensor()
(Pdb) sp=torch.randn(200,300)
(Pdb) sp[:,t:t+3]
*** TypeError: only integer tensors of a single element can be converted to an index
(Pdb) t:t+3
(Pdb) print(t:t+3)
*** SyntaxError: invalid syntax
(Pdb) sp[:,t]
tensor([[ 0.8808, 1.0081, -1.0811, …, 1.8065, 0.8156, 1.3579],
[-0.5902, 0.0588, 0.4373, …, -1.4304, -0.4398, -1.6143],
[-0.9567, -0.6260, 0.8671, …, 0.3011, -0.1352, -1.7263],
…,
[ 0.7137, -0.0028, -1.8195, …, 1.0860, 0.5901, -0.9922],
[-0.9040, -0.2345, -1.1723, …, -1.3859, -1.2003, -0.3777],
[-0.2986, -0.6276, -0.5059, …, 2.4101, 0.0195, -1.7069]])
(Pdb) t2=torch.arange(2,12)
(Pdb) tw
*** NameError: name ‘tw’ is not defined
(Pdb) t2
tensor([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
(Pdb) t3 = torch.cat(t,t2)
*** TypeError: cat() received an invalid combination of arguments - got (Tensor, Tensor), but expected one of:


[*](tuple of Tensors tensors, int dim, *, Tensor out)
[*](tuple of Tensors tensors, name dim, *, Tensor out)
(Pdb) t3 = torch.cat()
(Pdb) t3
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11])
(Pdb) t3=torch.hstack()
(Pdb) t3
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11])
(Pdb) t3=torch.vstack()
(Pdb) t3
tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
(Pdb) t3.shape
torch.Size()
(Pdb) sp[:,t3]
tensor([[[ 0.8808, 1.0081, -1.0811, …, 1.8065, 0.8156, 1.3579],
[-1.0811, 0.0215, -0.0749, …, 1.3579, 0.5030, -0.6049]],
[[-0.5902, 0.0588, 0.4373, …, -1.4304, -0.4398, -1.6143],
[ 0.4373, -0.4506, 1.2061, …, -1.6143, 1.0399, -0.9514]],
[[-0.9567, -0.6260, 0.8671, …, 0.3011, -0.1352, -1.7263],
[ 0.8671, -0.2235, -0.1658, …, -1.7263, -0.3991, -1.3480]],
…,[[ 0.7137, -0.0028, -1.8195, …, 1.0860, 0.5901, -0.9922],
[-1.8195, 0.7648, -1.2249, …, -0.9922, -1.9712, 1.7941]],
[[-0.9040, -0.2345, -1.1723, …, -1.3859, -1.2003, -0.3777],
[-1.1723, -0.8177, -0.5682, …, -0.3777, 0.6858, 0.0616]],
[[-0.2986, -0.6276, -0.5059, …, 2.4101, 0.0195, -1.7069],
[-0.5059, 0.3183, -1.1891, …, -1.7069, 0.6224, 0.2936]]])
(Pdb) sp[:,t3].shape
torch.Size()
还是很方便的

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: pytorch 使用tensor肴杂:进行index操作