<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          從零開始深度學(xué)習(xí)Pytorch筆記(5)——張量的索引與變換

          共 1631字,需瀏覽 4分鐘

           ·

          2020-01-19 23:21

          0ac5f713b9c3b4f9f1b0518cdda4833a.webp889b35608fc061f8f8fbedde501111bd.webp前文傳送門:從零開始深度學(xué)習(xí)Pytorch筆記(1)——安裝Pytorch從零開始深度學(xué)習(xí)Pytorch筆記(2)——張量的創(chuàng)建(上)從零開始深度學(xué)習(xí)Pytorch筆記(3)——張量的創(chuàng)建(下)從零開始深度學(xué)習(xí)Pytorch筆記(4)——張量的拼接與切分
          在該系列的上一篇,我們介紹了更多Pytorch中的張量的拼接與切分,本文研究張量的索引與變換。張量的索引

          import?torch
          使用torch.masked_select()索引
          torch.masked_select(input,?mask,?out=None)
          其中:
          input: 要索引的張量mask: 與input同形狀的布爾類型張量
          t?=?torch.randint(0,12,size=(4,3))
          mask?=?t.ge(6)?#ge是greater?than?or?equal?,gt是greater?than?,?le???lt?
          t_select?=?torch.masked_select(t,mask)
          print(t,'\n',mask,'\n',t_select)#將大于等于6的數(shù)據(jù)挑出來,返回一維張量
          b9b114bad4006dd4d451d98c5d2a334f.webp張量變換
          使用torch.reshape()變換變換張量的形狀torch.reshape(input, shape)參數(shù):input: 要變換的張量shape: 新張量的形狀
          t?=?torch.randperm(10)
          t1?=?torch.reshape(t,(2,5))
          print(t,'\n',t1)
          cfd7e6565756b0c2ea1f52840d153a1f.webp
          t1?=?torch.reshape(t,(-1,5))#?-1代表根據(jù)其他維度計算得到
          print(t,'\n',t1)
          d53efdf020207fa0c51b9b9a149b208e.webp當(dāng)張量在內(nèi)存中是連續(xù)時,新張量和input共享數(shù)據(jù)內(nèi)存
          t?=?torch.randperm(10)
          t[0]?=?1024
          print(t,'\n',t1)
          print(id(t.data),id(t1.data))
          #共享內(nèi)存,id相同
          b8f386602d188892089c895a947ae834.webp使用torch.transpose()變換交換張量的兩個維度
          torch.transpose(input,?dim0,?dim1)
          參數(shù):
          input:要變換的張量dim0:要交換的維度dim1:要交換的維度
          t?=?torch.rand((4,3,2))
          t1?=?torch.transpose(t,dim0=0,dim1=1)#交換他們的第0,1維度
          print(t.shape,t1.shape)
          a3875c500fbbba1794c36b175bbd757d.webp使用torch.t()變換2 維張量轉(zhuǎn)置,對于矩陣而言,等價于torch.transpose(input,0,1)
          torch.t(input)
          參數(shù):input:要變換的張量
          x?=?torch.randn(3,2)
          print(x)
          torch.t(x)
          c6ab98a3d979e5b04ea0e49872f78ffc.webp使用torch.squeeze()變換壓縮長度為1的維度(軸)
          torch.squeeze(input,?dim=None,?out=None)
          參數(shù):dim: 若為None,移除所有長度為1的軸;若指定維度,當(dāng)且僅當(dāng)該軸長度為1時,可以被移除。
          t?=?torch.rand((1,2,1,1))
          t1?=?torch.squeeze(t)
          t2?=?torch.squeeze(t,dim=0)
          t3?=?torch.squeeze(t,dim=1)#指定的軸長度不為1,不能移除
          print(t.shape,'\n',t1.shape,t2.shape,t3.shape)
          ac50d6f8cb0841bc4a33bd20ee18d89f.webp使用torch.unsqueeze()變換依據(jù)dim擴(kuò)展維度
          torch.unsqueeze(input,?dim,?out=None)
          參數(shù):dim:擴(kuò)展的維度
          x?=?torch.tensor([1,?2,?3,?4,?5])
          torch.unsqueeze(x,?0)
          20f0103a741425bf054427ce92de9775.webp從中括號數(shù)量可以看出已經(jīng)從1維變成2維的了。
          torch.unsqueeze(x,?1)
          044a13ddc956db247e614549aab2b762.webp歡迎關(guān)注公眾號學(xué)習(xí)之后的深度學(xué)習(xí)連載部分~29ef171bd2efd5601324b21351989805.webp喜歡記得點再看哦,證明你來看過~
          瀏覽 77
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  无码人妻精品一区二区蜜桃在线看 | 亚洲精品aaa | 天天操天天摸天天日天天爱 | 国产亚洲婷婷 | 久久一区二区三区在线 |