1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        深度學(xué)習(xí)Pytorch框架Tensor張量

        共 3569字,需瀏覽 8分鐘

         ·

        2022-03-03 08:20

        ↑ 點(diǎn)擊藍(lán)字?關(guān)注極市平臺(tái)

        作者 | 秦一@知乎(已授權(quán))
        來(lái)源 | https://zhuanlan.zhihu.com/p/399350505
        編輯 | 極市平臺(tái)
        禁止二次轉(zhuǎn)載

        極市導(dǎo)讀

        ?

        本文主要介紹了Tensor的裁剪運(yùn)算、索引與數(shù)據(jù)篩選、組合/拼接、切片、變形操作、填充操作和Tensor的頻譜操作(傅里葉變換)。>>加入極市CV技術(shù)交流群,走在計(jì)算機(jī)視覺(jué)的最前沿

        1 Tensor的裁剪運(yùn)算

        • 對(duì)Tensor中的元素進(jìn)行范圍過(guò)濾
        • 常用于梯度裁剪(gradient clipping),即在發(fā)生梯度離散或者梯度爆炸時(shí)對(duì)梯度的處理
        • torch.clamp(input, min, max, out=None) → Tensor:將輸入input張量每個(gè)元素的夾緊到區(qū)間 [min,max],并返回結(jié)果到一個(gè)新張量。

        2 Tensor的索引與數(shù)據(jù)篩選

        • torch.where(codition,x,y):按照條件從x和y中選出滿足條件的元素組成新的tensor,輸入?yún)?shù)condition:條件限制,如果滿足條件,則選擇a,否則選擇b作為輸出。
        • torch.gather(input,dim,index,out=None):在指定維度上按照索引賦值輸出tensor
        • torch.inex_select(input,dim,index,out=None):按照指定索引賦值輸出tensor
        • torch.masked_select(input,mask,out=None):按照mask輸出tensor,輸出為向量
        • torch.take(input,indices):將輸入看成1D-tensor,按照索引得到輸出tensor
        • torch.nonzero(input,out=None):輸出非0元素的坐標(biāo)
        import?torch
        #torch.where

        a?=?torch.rand(4,?4)
        b?=?torch.rand(4,?4)

        print(a)
        print(b)

        out?=?torch.where(a?>?0.5,?a,?b)

        print(out)
        print("torch.index_select")
        a?=?torch.rand(4,?4)
        print(a)
        out?=?torch.index_select(a,?dim=0,
        ???????????????????index=torch.tensor([0,?3,?2]))
        #dim=0按列,index取的是行
        print(out,?out.shape)
        print("torch.gather")
        a?=?torch.linspace(1,?16,?16).view(4,?4)

        print(a)

        out?=?torch.gather(a,?dim=0,
        ?????????????index=torch.tensor([[0,?1,?1,?1],
        ?????????????????????????????????[0,?1,?2,?2],
        ?????????????????????????????????[0,?1,?3,?3]]))
        print(out)
        print(out.shape)
        #注:從0開始,第0列的第0個(gè),第一列的第1個(gè),第二列的第1個(gè),第三列的第1個(gè),,,以此類推
        #dim=0,?out[i,?j,?k]?=?input[index[i,?j,?k],?j,?k]
        #dim=1,?out[i,?j,?k]?=?input[i,?index[i,?j,?k],?k]
        #dim=2,?out[i,?j,?k]?=?input[i,?j,?index[i,?j,?k]]
        print("torch.masked_index")
        a?=?torch.linspace(1,?16,?16).view(4,?4)
        mask?=?torch.gt(a,?8)
        print(a)
        print(mask)
        out?=?torch.masked_select(a,?mask)
        print(out)
        print("torch.take")
        a?=?torch.linspace(1,?16,?16).view(4,?4)

        b?=?torch.take(a,?index=torch.tensor([0,?15,?13,?10]))

        print(b)
        #torch.nonzero
        print("torch.take")
        a?=?torch.tensor([[0,?1,?2,?0],?[2,?3,?0,?1]])
        out?=?torch.nonzero(a)
        print(out)
        #稀疏表示

        3 Tensor的組合/拼接

        • torch.cat(seq,dim=0,out=None):按照已經(jīng)存在的維度進(jìn)行拼接
        • torch.stack(seq,dim=0,out=None):沿著一個(gè)新維度對(duì)輸入張量序列進(jìn)行連接。序列中所有的張量都應(yīng)該為相同形狀。
        print("torch.stack")
        a?=?torch.linspace(1,?6,?6).view(2,?3)
        b?=?torch.linspace(7,?12,?6).view(2,?3)
        print(a,?b)
        out?=?torch.stack((a,?b),?dim=2)
        print(out)
        print(out.shape)

        print(out[:,?:,?0])
        print(out[:,?:,?1])

        4 Tensor的切片

        • torch.chunk(tensor,chunks,dim=0):按照某個(gè)維度平均分塊(最后一個(gè)可能小于平均值)
        • torch.split(tensor,split_size_or_sections,dim=0):按照某個(gè)維度依照第二個(gè)參數(shù)給出的list或者int進(jìn)行分割tensor

        5 Tensor的變形操作

        • torch().reshape(input,shape)
        • torch().t(input):只針對(duì)2D tensor轉(zhuǎn)置
        • torch().transpose(input,dim0,dim1):交換兩個(gè)維度
        • torch().squeeze(input,dim=None,out=None):去除那些維度大小為1的維度
        • torch().unbind(tensor,dim=0):去除某個(gè)維度
        • torch().unsqueeze(input,dim,out=None):在指定位置添加維度,dim=-1在最后添加
        • torch().flip(input,dims):按照給定維度翻轉(zhuǎn)張量
        • torch().rot90(input,k,dims):按照指定維度和旋轉(zhuǎn)次數(shù)進(jìn)行張量旋轉(zhuǎn)
        import?torch
        a?=?torch.rand(2,?3)
        print(a)
        out?=?torch.reshape(a,?(3,?2))
        print(out)
        print(a)
        print(torch.flip(a,?dims=[2,?1]))

        print(a)
        print(a.shape)
        out?=?torch.rot90(a,?-1,?dims=[0,?2])?#順時(shí)針旋轉(zhuǎn)90°??
        print(out)
        print(out.shape)

        6 Tensor的填充操作

        • torch.full((2,3),3.14)

        7 Tensor的頻譜操作(傅里葉變換)

        如果覺(jué)得有用,就請(qǐng)分享到朋友圈吧!

        △點(diǎn)擊卡片關(guān)注極市平臺(tái),獲取最新CV干貨

        公眾號(hào)后臺(tái)回復(fù)“CVPR21檢測(cè)”獲取CVPR2021目標(biāo)檢測(cè)論文下載~


        極市干貨
        項(xiàng)目/比賽:珠港澳人工智能算法大賽算法打榜
        算法trick目標(biāo)檢測(cè)比賽中的tricks集錦從39個(gè)kaggle競(jìng)賽中總結(jié)出來(lái)的圖像分割的Tips和Tricks
        技術(shù)綜述:一文弄懂各種loss function工業(yè)圖像異常檢測(cè)最新研究總結(jié)(2019-2020)


        #?CV技術(shù)社群邀請(qǐng)函?#

        △長(zhǎng)按添加極市小助手
        添加極市小助手微信(ID : cvmart4)

        備注:姓名-學(xué)校/公司-研究方向-城市(如:小極-北大-目標(biāo)檢測(cè)-深圳)


        即可申請(qǐng)加入極市目標(biāo)檢測(cè)/圖像分割/工業(yè)檢測(cè)/人臉/醫(yī)學(xué)影像/3D/SLAM/自動(dòng)駕駛/超分辨率/姿態(tài)估計(jì)/ReID/GAN/圖像增強(qiáng)/OCR/視頻理解等技術(shù)交流群


        每月大咖直播分享、真實(shí)項(xiàng)目需求對(duì)接、求職內(nèi)推、算法競(jìng)賽、干貨資訊匯總、與?10000+來(lái)自港科大、北大、清華、中科院、CMU、騰訊、百度等名校名企視覺(jué)開發(fā)者互動(dòng)交流~



        覺(jué)得有用麻煩給個(gè)在看啦~??
        瀏覽 67
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            邻居交换做爰伦理 | 国产一级黄大片 | 日韩精品一区二区三区免费视频 | 99精品人妻 | 日本不卡中文字幕 | 少妇的呻吟声 | 久久久久久久大香蕉 | 婷婷色天使18禁久久yyy | 91短视频在线 | 成人电影一区二区三区 |