pytorch之torch.unbind


官方文檔地址

說明:

        移除指定維后,返回一個元組,包含了沿着指定維切片后的各個切片。

參數:

  • tensor(Tensor) -- 輸入張量
  • dim(int) -- 刪除的維度(按照某一個維度展開,返回切片)

注意:

  不改變原來的tensor的shape,只是返回展開后的切片

import torch 
t = torch.rand(3,3) #隨機生成一個tensor
print(t)
print(t.shape)
r = torch.unbind(t,dim=0)#dim = 0指定拆除的維度
print(r)
s = torch.unbind(t,dim=1)#dim = 1指定拆除的維度
print(s)

jupyter notebook輸出結果:

tensor([[0.9493, 0.8956, 0.2844],
        [0.9451, 0.3998, 0.9539],
        [0.7620, 0.7965, 0.8650]])
torch.Size([3, 3])
(tensor([0.9493, 0.9451, 0.7620]), tensor([0.8956, 0.3998, 0.7965]), tensor([0.2844, 0.9539, 0.8650]))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM