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

打印 上一主题 下一主题

主题 657|帖子 657|积分 1971

(Pdb) tmp = torch.randn(3,5)
(Pdb) indx = torch.tensor([1,0]).long()
(Pdb) temp(indx)
*** NameError: name ‘temp’ is not defined
(Pdb) tmp(indx)
*** TypeError: ‘Tensor’ object is not callable
(Pdb) tmp[indx]
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([1, 0])
(Pdb) indx2= torch.tensor([[1,0]]).long()
(Pdb) index2
*** NameError: name ‘index2’ is not defined
(Pdb) indx2
tensor([[1, 0]])
(Pdb) indx2.shape
torch.Size([1, 2])
(Pdb) tmp[indx2]
tensor([[[ 0.1633, 0.9389, 1.2806, -0.2525, 0.2817],
[ 0.6204, 0.5973, -1.7741, 0.3721, -0.5338]]])
(Pdb) tmp[indx2].shape
torch.Size([1, 2, 5])
(Pdb) tmp[:,indx2].shape
torch.Size([3, 1, 2])
(Pdb) tmp[:,indx2]
tensor([[[ 0.5973, 0.6204]],
[[ 0.9389, 0.1633]],
[[-0.2156, 0.4279]]])
(Pdb) t=torch.arange(10)
(Pdb) t
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
(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([t,t2])
    (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([t,t2])
    (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([t,t2])
    (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([2, 10])
    (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([200, 2, 10])
还是很方便的

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

光之使者

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表