pytorch 使用tensor混合:进行index操作

pytorch 使用tensor混合:进行index操作

码农世界 2024-06-05 前端 83 次浏览 0个评论

(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])

    还是很方便的

转载请注明来自码农世界,本文标题:《pytorch 使用tensor混合:进行index操作》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,83人围观)参与讨论

还没有评论,来说两句吧...

Top