最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch中的scatter_add_函數(shù)的使用解讀

 更新時(shí)間:2023年06月14日 08:59:37   作者:*Lisen  
這篇文章主要介紹了pytorch中的scatter_add_函數(shù)的使用解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

pytorch scatter_add_函數(shù)的使用

關(guān)于這個(gè)函數(shù),很少博客詳細(xì)的介紹。下面就我個(gè)人理解簡單介紹下。

函數(shù):

self_tensor.scatter_add_(dim, index_tensor, other_tensor) → 輸出tensor

該函數(shù)的意思是:

將other_tensor中的數(shù)據(jù),按照index_tensor中的索引位置,添加至self_tensor矩陣中。

參數(shù):

  • dim:表示需要改變的維度,但是注意,假如dim=1,并不是說self_tensor在dim=0上的數(shù)據(jù)不會改變,這個(gè)dim只是在取矩陣數(shù)據(jù)時(shí)不固定dim=1維的索引,使用index_tensor矩陣中的索引??赡苓@樣說還是不太理解,下面會用例子說明。其中self_tensor表示我們需要改變的tensor矩陣
  • index_tensor:索引矩陣;
  • other_tensor:需要添加到self_tensor中的tensor

要求:

1、self_tensor,index_tensor, other_tensor 的維度需要相同,即self.tensor.dim() = index_tensor.dim() = other_tensor.dim();

2、假設(shè)dim=d,那么index_tensor矩陣中的所有數(shù)據(jù)必須小于d-1;

3、假設(shè)dim=d,index_tensor矩陣在d維度上的size必須小于self_tensor和other_tensor的size;即index.size(d) <= other_tensor.size(d) 且index.size(d) <= self_tensor.size(d)

三維計(jì)算公式:

self[index[i][j][k]][j][k] += other[i][j][k] # 如果 dim == 0
self[i][index[i][j][k]][k] += other[i][j][k] # 如果 dim == 1
self[i][j][index[i][j][k]] += other[i][j][k] # 如果 dim == 2

二維計(jì)算公式:

self[index[i][j]][j] += other[i][j] # 如果 dim == 0
self[i][index[i][j]] += other[i][j] # 如果 dim == 1
? index_tensor = torch.tensor([[0,1],[1,1]])
? print('index_tensor: \n', index_tensor)
? self_tensor = torch.arange(0, 4).view(2, 2)
? print('self_tensor: \n', self_tensor)
? other_tensor = torch.arange(5, 9).view(2, 2)
? print('other_tensor: \n', other_tensor)
? dim = 0
? for i in range(index_tensor.size(0)):
? ? ? for j in range(index_tensor.size(1)):
? ? ? ? ? replace_index = index_tensor[i][j]
? ? ? ? ? if dim == 0:
? ? ? ? ? ? ? # self矩陣的第0維索引
? ? ? ? ? ? ? self_tensor[replace_index][j] += other_tensor[i][j]
? ? ? ? ? elif dim == 1:
? ? ? ? ? ? ? # self矩陣的第1維索引
? ? ? ? ? ? ? self_tensor[i][replace_index] += other_tensor[i][j] ? ? ??
? print(self_tensor)

結(jié)果:

    index_tensor: 
 tensor([[0, 1],
        [1, 1]])
self_tensor: 
 tensor([[0, 1],
        [2, 3]])
other_tensor: 
 tensor([[5, 6],
        [7, 8]])
tensor([[ 5,  1],
        [ 9, 17]])

使用函數(shù)計(jì)算:

index_tensor = torch.tensor([[0,1],[1,1]])
print('index_tensor: \n', index_tensor)
self_tensor = torch.arange(0, 4).view(2, 2)
print('self_tensor: \n', self_tensor)
other_tensor = torch.arange(5, 9).view(2, 2)
print('other_tensor: \n', other_tensor)
self_tensor.scatter_add_(0, index_tensor, other_tensor)?
print(self_tensor)

結(jié)果:

index_tensor: 
 tensor([[0, 1],
        [1, 1]])
self_tensor: 
 tensor([[0, 1],
        [2, 3]])
other_tensor: 
 tensor([[5, 6],
        [7, 8]])
tensor([[ 5,  1],
        [ 9, 17]])

scatter_add()函數(shù)通俗理解

self [ index[i,j] , j ] += src [ i , j ] # if dim == 0
self [ i , index[i,j] ] += src[ i, j ] # if dim == 1

理解scatter_add()函數(shù),看index就行了,index有多少個(gè),self坐標(biāo)就會變多少次。

self是一個(gè)二維的數(shù)組,self[第一維,第二維],dim==0,就是將src對應(yīng)坐標(biāo),對應(yīng)到 index 坐標(biāo)里面的值,放置到self的第一維中。

例如:

src[i,j]對應(yīng)到index[i,j],假設(shè) index[i,j] ==0,則self[第一維,第二維] 為self[0,j],只改變第一維,第二維的值和src第二維一樣。

然后self[0,j]的值就會變?yōu)?self[0,j]=self[0,j]+src[i,j]

代碼中 self=torch.zeros(3,5), dim=0, index=[0,1,2,0,0], src=torch.ones(2,5)

我們只看 src,當(dāng) src[0,0]=1, index[0,0]=0, self[0,0]=self[0,0]+src[0,0]=1。

當(dāng)src[0,1]=1, index[0,1]=1, self[1,1]=self[1,1]+src[0,1]=1, self的第一維是index的值決定的為1,第二維是src的第二維坐標(biāo)決定也為1。

當(dāng)index的值沒有時(shí),就停止變換,self沒有變換過的坐標(biāo)值就保持不變。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python批量下載圖片的三種方法

    python批量下載圖片的三種方法

    用python批量下載一個(gè)網(wǎng)頁中的圖片,需要用到擴(kuò)展庫來解析html代碼
    2013-04-04
  • django admin 添加自定義鏈接方式

    django admin 添加自定義鏈接方式

    這篇文章主要介紹了django admin 添加自定義鏈接方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 對pytorch中的梯度更新方法詳解

    對pytorch中的梯度更新方法詳解

    今天小編就為大家分享一篇對pytorch中的梯度更新方法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • YOLO?v5引入解耦頭部完整步驟

    YOLO?v5引入解耦頭部完整步驟

    網(wǎng)上有很多添加解耦頭的博客,在此記錄下我使用解耦頭對YOLOv5改進(jìn),下面這篇文章主要給大家介紹了關(guān)于YOLO?v5引入解耦頭部的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Python實(shí)現(xiàn)階乘的四種寫法

    Python實(shí)現(xiàn)階乘的四種寫法

    本文主要介紹了Python實(shí)現(xiàn)階乘的六種寫法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • GitHub上值得推薦的8個(gè)python 項(xiàng)目

    GitHub上值得推薦的8個(gè)python 項(xiàng)目

    GitHub 無疑是代碼托管領(lǐng)域的先行者,Python 作為一種通用編程語言,已經(jīng)被千千萬萬的開發(fā)人員用來構(gòu)建各種有意思或有用的項(xiàng)目。以下我們會介紹一些使用 Python 構(gòu)建的GitHub上優(yōu)秀的項(xiàng)目。
    2020-10-10
  • python切片的步進(jìn)、添加、連接簡單操作示例

    python切片的步進(jìn)、添加、連接簡單操作示例

    這篇文章主要介紹了python切片的步進(jìn)、添加、連接簡單操作,結(jié)合實(shí)例形式分析了Python切片運(yùn)算的常見操作技巧,需要的朋友可以參考下
    2019-07-07
  • 詳解Python中的文件操作

    詳解Python中的文件操作

    這篇文章主要介紹了Python中文件操作的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2021-01-01
  • python創(chuàng)建堆的方法實(shí)例講解

    python創(chuàng)建堆的方法實(shí)例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python創(chuàng)建堆的方法實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • Python比較set的規(guī)則及簡單例子

    Python比較set的規(guī)則及簡單例子

    在Python中,集合可以通過比較運(yùn)算符進(jìn)行比較,檢查子集、超集、相等性等關(guān)系,文中通過代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-11-11

最新評論

容城县| 五原县| 泾川县| 九江县| 长阳| 百色市| 肃北| 东阳市| 平江县| 临夏市| 汉阴县| 厦门市| 黑龙江省| 珠海市| 铜梁县| 雷山县| 平罗县| 德惠市| 抚远县| 鄂伦春自治旗| 五大连池市| 田林县| 罗江县| 高要市| 南部县| 诸城市| 寿阳县| 富川| 彭山县| 雷州市| 淮南市| 天祝| 隆安县| 磴口县| 安岳县| 锡林浩特市| 策勒县| 汝南县| 万荣县| 石首市| 久治县|