欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyTorch張量拼接、切分、索引的實(shí)現(xiàn)

 更新時(shí)間:2024年03月21日 10:21:40   作者:timerring  
在學(xué)習(xí)深度學(xué)習(xí)的過(guò)程中,遇到的第一個(gè)概念就是張量,張量在pytorch中的計(jì)算十分重要,本文主要介紹了PyTorch張量拼接、切分、索引的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下

一、張量拼接與切分

1.1 torch.cat

功能:將張量按維度dim 進(jìn)行拼接

  • tensors : 張量序列

  • dim: 要拼接的維度

 t = torch.ones((2, 3))

    t_0 = torch.cat([t, t], dim=0)
    t_1 = torch.cat([t, t, t], dim=1)

    print("t_0:{} shape:{}\nt_1:{} shape:{}".format(t_0, t_0.shape, t_1, t_1.shape))
t_0:tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]) shape:torch.Size([4, 3])
t_1:tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1., 1.]]) shape:torch.Size([2, 9])

(2,3) -> (2,6)

這里的dim維度與axis相同,0代表列,1代表行。

1.2 torch.stack

功能:在新創(chuàng)建的維度 dim 上進(jìn)行拼接(會(huì)拓寬原有的張量維度)

  • tensors:張量序列
  • dim:要拼接的維度

t = torch.ones((2, 3))
t_stack = torch.stack([t, t, t], dim=2)
print("\nt_stack:{} shape:{}".format(t_stack, t_stack.shape))

可見(jiàn),它在新的維度上進(jìn)行了拼接。

參數(shù)[t, t, t]的意思就是在第n個(gè)維度上拼接成這個(gè)樣子。

t_stack:tensor([[[1., 1., 1.],
         [1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.],
         [1., 1., 1.]]]) shape:torch.Size([2, 3, 3])
# 在第二維度上進(jìn)行了拼接
Process finished with exit code 0

1.3 torch.chunk

功能:將張量按維度 dim 進(jìn)行平均切分

返回值:張量列表

注意事項(xiàng):若不能整除,最后一份張量小于其他張量。

  • input : 要切分的張量
  • chunks 要切分的份數(shù)
  • dim 要切分的維度
    # cut into 3
    a = torch.ones((2, 7))  # 7
    list_of_tensors = torch.chunk(a, dim=1, chunks=3)   # 3

    for idx, t in enumerate(list_of_tensors):
        print("第{}個(gè)張量:{}, shape is {}".format(idx+1, t, t.shape))

可知,切分是7/3向上取整,每份是3,最后剩下的維度直接輸出即可。

第1個(gè)張量:tensor([[1., 1., 1.],
        [1., 1., 1.]]), shape is torch.Size([2, 3])
第2個(gè)張量:tensor([[1., 1., 1.],
        [1., 1., 1.]]), shape is torch.Size([2, 3])
第3個(gè)張量:tensor([[1.],
        [1.]]), shape is torch.Size([2, 1])

1.4 torch.split

torch.split(Tensor, split_size_or_sections, dim)

功能:將張量按維度 dim 進(jìn)行切分

返回值:張量列表

  • tensor : 要切分的張量
  • split_size_or_sections 為 int 時(shí),表示
    每一份的長(zhǎng)度;為 list 時(shí),按 list 元素切分
  • dim 要切分的維度
    t = torch.ones((2, 5))

    list_of_tensors = torch.split(t, [2, 1, 1], dim=1)  # [2 , 1, 2]
    for idx, t in enumerate(list_of_tensors):
        print("第{}個(gè)張量:{}, shape is {}".format(idx+1, t, t.shape))

是按照指定長(zhǎng)度list進(jìn)行切分的。注意list中長(zhǎng)度總和必須為原張量在改維度的大小,不然會(huì)報(bào)錯(cuò)。

第1個(gè)張量:tensor([[1., 1., 1.],
        [1., 1., 1.]]), shape is torch.Size([2, 3])
第2個(gè)張量:tensor([[1., 1., 1.],
        [1., 1., 1.]]), shape is torch.Size([2, 3])
第3個(gè)張量:tensor([[1.],
        [1.]]), shape is torch.Size([2, 1])

二、張量索引

2.1 torch.index_select

torch.index_select(input, dim, index, out=None)

功能:在維度dim 上,按 index 索引數(shù)據(jù)

返回值:依index 索引數(shù)據(jù)拼接的張量

  • input : 要索引的張量
  • dim 要索引的維度
  • index 要索引數(shù)據(jù)的序號(hào)
    t = torch.randint(0, 9, size=(3, 3))
    idx = torch.tensor([0, 2], dtype=torch.long)    # if float will report an error
    t_select = torch.index_select(t, dim=0, index=idx)
    print(idx)
    print("t:\n{}\nt_select:\n{}".format(t, t_select))

可見(jiàn)idx是一個(gè)存儲(chǔ)序號(hào)的張量,而torch.index_select通過(guò)該張量索引原tensor并且拼接返回。

tensor([0, 2])
t:
tensor([[4, 5, 0],
        [5, 7, 1],
        [2, 5, 8]])
t_select:
tensor([[4, 5, 0],
        [2, 5, 8]])

2.2 torch.masked_select

功能:按mask 中的 True 進(jìn)行索引

返回值:一維張量(無(wú)法確定true的個(gè)數(shù),因此也就無(wú)法顯示原來(lái)的形狀,因此這里返回一維張量)

  • input : 要索引的張量
  • mask 與 input 同形狀的布爾類(lèi)型張量
    t = torch.randint(0, 9, size=(3, 3))
    mask = t.le(5)  # ge is mean greater than or equal/   gt: greater than  le  lt
    t_select = torch.masked_select(t, mask)
    print("t:\n{}\nmask:\n{}\nt_select:\n{} ".format(t, mask, t_select))

通過(guò)掩碼來(lái)索引。

tensor([[4, 5, 0],
        [5, 7, 1],
        [2, 5, 8]])
mask:
tensor([[ True,  True,  True],
        [ True, False,  True],
        [ True,  True, False]])
t_select:
tensor([4, 5, 0, 5, 1, 2, 5]) 

Process finished with exit code 0

到此這篇關(guān)于PyTorch張量拼接、切分、索引的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)PyTorch張量拼接切分索引內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例

    python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例

    今天小編就為大家分享一篇python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • python中類(lèi)的一些方法分析

    python中類(lèi)的一些方法分析

    這篇文章主要介紹了python中類(lèi)的一些方法分析,實(shí)例講述了Python中子類(lèi)調(diào)用父類(lèi)時(shí)繼承的方法問(wèn)題,需要的朋友可以參考下
    2014-09-09
  • Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練詳解

    Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練詳解

    目標(biāo)檢測(cè)是計(jì)算機(jī)視覺(jué)上的一個(gè)重要任務(wù),下面這篇文章主要給大家介紹了關(guān)于Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • 詳解Python發(fā)送郵件實(shí)例

    詳解Python發(fā)送郵件實(shí)例

    這篇文章主要介紹了Python發(fā)送郵件實(shí)例,Python發(fā)送郵件需要smtplib和email兩個(gè)模塊,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Random 在 Python 中的使用方法

    Random 在 Python 中的使用方法

    random() 方法返回隨機(jī)生成的一個(gè)實(shí)數(shù),它在[0,1)范圍內(nèi)。這篇文章主要介紹了Random 在 Python 中的使用方法,需要的朋友可以參考下
    2018-08-08
  • Python如何發(fā)布程序的詳細(xì)教程

    Python如何發(fā)布程序的詳細(xì)教程

    Python是一種面向?qū)ο蟮慕忉屝陀?jì)算機(jī)程序設(shè)計(jì)語(yǔ)言,現(xiàn)在很多人都在使用,尤其是其跨平臺(tái)特性及自然語(yǔ)言屬性,獲得很多人的鐘情,那么如何把Python程序打包為Windows系統(tǒng)中的exe可執(zhí)行程序呢
    2018-10-10
  • Python 批量操作設(shè)備的實(shí)現(xiàn)步驟

    Python 批量操作設(shè)備的實(shí)現(xiàn)步驟

    本文將結(jié)合實(shí)例代碼,介紹Python 批量操作設(shè)備的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • python字典dict中常用內(nèi)置函數(shù)的使用

    python字典dict中常用內(nèi)置函數(shù)的使用

    本文主要介紹了python字典dict中常用內(nèi)置函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Python與C++中梯度方向直方圖的實(shí)現(xiàn)

    Python與C++中梯度方向直方圖的實(shí)現(xiàn)

    在學(xué)習(xí)HOG特征的時(shí)候,發(fā)現(xiàn)一片英文文章講得淺顯易懂。因此翻譯在這里學(xué)習(xí),感興趣的朋友快來(lái)看看吧
    2022-03-03
  • Python unittest單元測(cè)試框架及斷言方法

    Python unittest單元測(cè)試框架及斷言方法

    這篇文章主要介紹了Python unittest單元測(cè)試框架及斷言方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評(píng)論