pytorch?tensor按廣播賦值scatter_函數(shù)的用法
pytorch tensor按廣播賦值scatter函數(shù)
普通廣播
>>> import torch >>> a = torch.tensor([[1,2,3],[4,5,6]]) # 和a shape相同,但是用0填充 >>> b = torch.full_like(a,0) >>> c = torch.tensor([[0,0,1],[1,0,1]]) # 賦值索引 >>> c[:,0] tensor([0, 1]) # 賦值語句:使用廣播機(jī)制進(jìn)行賦值 >>> b[range(n),c[:,0]] = 1 >>> b tensor([[1, 0, 0], ? ? ? ? [0, 1, 0]])
為什么會(huì)出現(xiàn)這樣的結(jié)果?
賦值語句的意思是:
- 1.range(n)表示對(duì)b的所有行進(jìn)行賦值操作
- 2.c[:,0]] 表示執(zhí)行賦值操作的b的列索引,[0, 1] 表示第一行對(duì)索引為0的列進(jìn)行操作(賦值為1);第二行對(duì)索引為1的列進(jìn)行操作(賦值為1)
- 3.最右邊的1表示對(duì)應(yīng)索引位置所賦的值
scatter函數(shù)
import torch
label = torch.zeros(3, 6) #首先生成一個(gè)全零的多維數(shù)組
print("label:",label)
a = torch.ones(3,5)
b = [[0,1,2],[0,1,3],[1,2,3]]
#這里需要解釋的是,b的行數(shù)要小于等于label的行數(shù),列數(shù)要小于等于a的列數(shù)
print(a)
label.scatter_(1,torch.LongTensor(b),a)?
#參數(shù)解釋:‘1':需要賦值的維度,是label的維度;‘torch.LongTensor(b)':需要賦值的索引;‘a(chǎn)':要賦的值
print("new_label: ",label)
label:?
tensor([[0., 0., 0., 0., 0., 0.],
? ? ? ? [0., 0., 0., 0., 0., 0.],
? ? ? ? [0., 0., 0., 0., 0., 0.]])
tensor([[1., 1., 1., 1., 1.],
? ? ? ? [1., 1., 1., 1., 1.],
? ? ? ? [1., 1., 1., 1., 1.]])
new_label: ?
tensor([[1., 1., 1., 0., 0., 0.],
? ? ? ? [1., 1., 0., 1., 0., 0.],
? ? ? ? [0., 1., 1., 1., 0., 0.]])舉例
>>> b = torch.full_like(a,0) >>> b tensor([[0, 0, 0], ? ? ? ? [0, 0, 0]]) >>> c = torch.tensor([[0,0],[1,0]]) >>> c tensor([[0, 0], ? ? ? ? [1, 0]]) # 1表示對(duì)b的列進(jìn)行賦值,以c的每一行的值作為b的列索引,一行一行地進(jìn)行賦值 # c第一行 [0,0] 表示分別將b的 第一行 第0列、第0列 元素賦值為1 (重復(fù)操作了) # c第二行 [1,0] 表示 將b的 第1列、第0列 元素賦值為1 (逆序了) # 上面的這兩個(gè)賦值操作其實(shí)有重復(fù)的、逆序的 >>> b.scatter_(1,torch.LongTensor(c),1) >>> b tensor([[1, 0, 0], ? ? ? ? [1, 1, 0]])
scatter()和scatter_()的作用和區(qū)別
scatter和scatter_函數(shù)原型如下
Tensor.scatter_(dim, index, src, reduce=None)->Tensor scatter(input, dim, index, src)->Tensor
函數(shù)作用是將src中的數(shù)據(jù)按照dim中指定的維度和index中的索引寫入self中。
dim(int)- 操作的維度index(LongTensor)- 填充依據(jù)的索引,src(Tensor of float)- 操作的src數(shù)據(jù)reduce(str, optional)- reduce選擇運(yùn)算方式,有’add’和’mutiply’方式, 默認(rèn)為替換 dim(int)
在scatter中self指返回的tensor,scatter_中self指輸入的tensor自身。
對(duì)于一個(gè)三維張量,self更新結(jié)果如下
self[index[i][j][k]][j][k] = src[i][j][k] ?# if dim == 0 self[i][index[i][j][k]][k] = src[i][j][k] ?# if dim == 1 self[i][j][index[i][j][k]] = src[i][j][k] ?# if dim == 2
使用示例
>>> src = torch.arange(1, 11).reshape((2, 5)) >>> src tensor([[ 1, ?2, ?3, ?4, ?5], ? ? ? ? [ 6, ?7, ?8, ?9, 10]]) >>> index = torch.tensor([[0, 1, 2, 0]]) >>> torch.zeros(3, 5, dtype=src.dtype).scatter_(0, index, src) tensor([[1, 0, 0, 4, 0], ? ? ? ? [0, 2, 0, 0, 0], ? ? ? ? [0, 0, 3, 0, 0]])
dim=0, 說明按照行賦值,index[0][1]=1, 代表更改input中的第1行,src[0][1]=2,因此更改input中[1][1]中的元素為2
>>> index = torch.tensor([[0, 1, 2], [0, 1, 4]]) >>> torch.zeros(3, 5, dtype=src.dtype).scatter_(1, index, src) tensor([[1, 2, 3, 0, 0], ? ? ? ? [6, 7, 0, 0, 8], ? ? ? ? [0, 0, 0, 0, 0]])
dim,說明按照列賦值,index[0][1]=1, 代表更改input中的第1列,src[0][1]=2, 更改input中[0][1]元素為2
>>> torch.full((2, 4), 2.).scatter_(1, torch.tensor([[2], [3]]), ... ? ? ? ? ? ?1.23, reduce='multiply') tensor([[2.0000, 2.0000, 2.4600, 2.0000], ? ? ? ? [2.0000, 2.0000, 2.0000, 2.4600]]) >>> torch.full((2, 4), 2.).scatter_(1, torch.tensor([[2], [3]]), ... ? ? ? ? ? ?1.23, reduce='add') tensor([[2.0000, 2.0000, 3.2300, 2.0000], ? ? ? ? [2.0000, 2.0000, 2.0000, 3.2300]])
scatter的應(yīng)用, one-hot編碼
def one_hot(x, n_class, dtype=torch.float32): ? ? # X shape: (batch), output shape: (batch, n_class) ? ? x=x.long() ? ? res=torch.zeros(x.shape[0], n_class, dtype=dtype, device=x.device) # shape為[batch, n_class]全零向量 ? ? res.scatter_(1, x.view(-1,1), 1)? ? ? # scatter_(input, dim, index, src)將src中數(shù)據(jù)根據(jù)index的索引按照dim的方向填進(jìn)input中 ? ? return res x=torch.tensor([5,7,0]) one_hot(x, 10) tensor([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], ? ? ? ? [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], ? ? ? ? [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中Timedelta轉(zhuǎn)換為Int或Float方式
這篇文章主要介紹了Python中Timedelta轉(zhuǎn)換為Int或Float方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
python調(diào)用C/C++動(dòng)態(tài)庫的實(shí)踐案例
python是動(dòng)態(tài)語言,c++是靜態(tài)語言,下面這篇文章主要介紹了python調(diào)用C/C++動(dòng)態(tài)庫的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-09-09
機(jī)器學(xué)習(xí)Erdos?Renyi隨機(jī)圖生成方法及特性
這篇文章主要為大家介紹了機(jī)器學(xué)習(xí)Erdos?Renyi隨機(jī)圖生成方法及特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python腳本如何在bilibili中查找彈幕發(fā)送者
這篇文章主要介紹了如何在bilibili中查找彈幕發(fā)送者,本文給大家分享小編寫的一個(gè)python腳本來實(shí)現(xiàn)bilibili彈幕發(fā)送者,需要的朋友可以參考下2020-06-06
詳解Python 數(shù)據(jù)庫 (sqlite3)應(yīng)用
本篇文章主要介紹了Python標(biāo)準(zhǔn)庫14 數(shù)據(jù)庫 (sqlite3),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。2016-12-12
深入講解Python函數(shù)中參數(shù)的使用及默認(rèn)參數(shù)的陷阱
這篇文章主要介紹了Python函數(shù)中參數(shù)的使用及默認(rèn)參數(shù)的陷阱,文中將函數(shù)的參數(shù)分為必選參數(shù)、默認(rèn)參數(shù)、可變參數(shù)和關(guān)鍵字參數(shù)來講,要的朋友可以參考下2016-03-03
python爬蟲爬取監(jiān)控教務(wù)系統(tǒng)的思路詳解
這篇文章主要介紹了python爬蟲監(jiān)控教務(wù)系統(tǒng),主要實(shí)現(xiàn)思路是對(duì)已有的成績(jī)進(jìn)行處理,變?yōu)閘ist集合,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-01-01
Python使用OpenCV實(shí)現(xiàn)物體跟蹤的實(shí)踐方法
文章介紹了使用OpenCV進(jìn)行物體跟蹤的小項(xiàng)目,涵蓋選擇ROI、初始化跟蹤器、逐幀更新等核心流程,代碼示例展示了不同OpenCV版本的兼容性處理、跟蹤失敗提示和交互控制,此外,還提供了常見問題解答和進(jìn)一步學(xué)習(xí)建議,需要的朋友可以參考下2026-04-04

