PyTorch定義Tensor及索引和切片(最新推薦)
深度學(xué)習(xí)--PyTorch定義Tensor
一、創(chuàng)建Tensor
1.1未初始化的方法
?這些方法只是開(kāi)辟了空間,所附的初始值(非常大,非常小,0),后面還需要我們進(jìn)行數(shù)據(jù)的存入。
torch.empty():返回一個(gè)沒(méi)有初始化的Tensor,默認(rèn)是FloatTensor類(lèi)型。
#torch.empty(d1,d2,d3)函數(shù)輸入的是shape torch.empty(2,3,5) #tensor([[[-1.9036e-22, 6.8944e-43, 0.0000e+00, 0.0000e+00, -1.0922e-20], # [ 6.8944e-43, -2.8812e-24, 6.8944e-43, -5.9272e-21, 6.8944e-43], # [ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]], # # [[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00], # [ 0.0000e+00, 0.0000e+00, 1.4013e-45, 0.0000e+00, 0.0000e+00], # [ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]]])
torch.FloatTensor():返回沒(méi)有初始化的FloatTensor。
#torch.FloatTensor(d1,d2,d3) torch.FloatTensor(2,2) #tensor([[-0.0000e+00, 4.5907e-41], # [-7.3327e-21, 6.8944e-43]])
torch.IntTensor():返回沒(méi)有初始化的IntTensor。
#torch.IntTensor(d1,d2,d3) torch.IntTensor(2,2) #tensor([[ 0, 1002524760], # [-1687359808, 492]], dtype=torch.int32)
1.2 隨機(jī)初始化
- 隨機(jī)均勻分布:rand/rand_like,randint
rand:[0,1)均勻分布;randint(min,max,[d1,d2,d3]) 返回[min,max)的整數(shù)均勻分布
#torch.rand(d1,d2,d3) torch.rand(2,2) #tensor([[0.8670, 0.6158], # [0.0895, 0.2391]]) #rand_like() a=torch.rand(3,2) torch.rand_like(a) #tensor([[0.2846, 0.3605], # [0.3359, 0.2789], # [0.5637, 0.6276]]) #randint(min,max,[d1,d2,d3]) torch.randint(1,10,[3,3,3]) #tensor([[[3, 3, 8], # [2, 7, 7], # [6, 5, 9]], # # [[7, 9, 9], # [6, 3, 9], # [1, 5, 6]], # # [[5, 4, 8], # [7, 1, 2], # [3, 4, 4]]])
- 隨機(jī)正態(tài)分布 randn
randn返回一組符合N(0,1)正態(tài)分布的隨機(jī)數(shù)據(jù)
#randn(d1,d2,d3) torch.randn(2,2) #tensor([[ 0.3729, 0.0548], # [-1.9443, 1.2485]]) #normal(mean,std) 需要給出均值和方差 torch.normal(mean=torch.full([10],0.),std=torch.arange(1,0,-0.1)) #tensor([-0.8547, 0.1985, 0.1879, 0.7315, -0.3785, -0.3445, 0.7092, 0.0525, 0.2669, 0.0744]) #后面需要用reshape修正成自己想要的形狀
1.3 賦值初始化
full:返回一個(gè)定值
#full([d1,d2,d3],num) torch.full([2,2],6) #tensor([[6, 6], # [6, 6]]) torch.full([],6) #tensor(6) 標(biāo)量 torch.full([1],6) #tensor([6]) 向量
arange:返回一組階梯,等差數(shù)列
#torch.arange(min,max,step):返回一個(gè)[min,max),步長(zhǎng)為step的集體數(shù)組,默認(rèn)為1 torch.arange(0,10) #tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) torch.arange(0,10,2) #tensor([0, 2, 4, 6, 8])
linspace/logspace:返回一組階梯
#torch.linspace(min,max,steps):返回一個(gè)[min,max],數(shù)量為steps的數(shù)組 torch.linspace(1,10,11) #tensor([ 1.0000, 1.9000, 2.8000, 3.7000, 4.6000, 5.5000, 6.4000, 7.3000, # 8.2000, 9.1000, 10.0000]) #torch.logspace(a,b,steps):返回一個(gè)[10^a,10^b],數(shù)量為steps的數(shù)組 torch.logspace(0,1,10) #tensor([ 1.0000, 1.2915, 1.6681, 2.1544, 2.7826, 3.5938, 4.6416, 5.9948, # 7.7426, 10.0000])
ones/zeros/eye:返回全1全0或者對(duì)角陣 ones_like/zeros_like
#torch.ones(d1,d2) torch.ones(2,2) #tensor([[1., 1.], # [1., 1.]]) #torch.zeros(d1,d2) torch.zeros(2,2) #tensor([[0., 0.], # [0., 0.]]) #torch.eye() 只能接收一個(gè)或兩個(gè)參數(shù) torch.eye(3) #tensor([[1., 0., 0.], # [0., 1., 0.], # [0., 0., 1.]]) torch.eye(2,3) #tensor([[1., 0., 0.], # [0., 1., 0.]])
1.4 隨機(jī)打散變量
randperm:一般用于位置操作。類(lèi)似random.shuffle()。
torch.randperm(8) #tensor([2, 6, 7, 5, 3, 4, 1, 0])
二、索引與切片
簡(jiǎn)單索引方式
a=torch.rand(4,3,28,28) a[0].shape #torch.Size([3, 28, 28]) a[0,0,0,0] #tensor(0.9373)
批量索引方式 開(kāi)始位置:結(jié)束位置 左邊取的到,右邊取不到 算是一種切片 [0,1,2]->[-3,-2,-1]
a[:2].shape #torch.Size([2, 3, 28, 28]) a[1:].shape #torch.Size([3, 3, 28, 28])
隔行采樣方式 開(kāi)始位置:結(jié)束位置:間隔
a[:,:,0:28:2,:].shape #torch.Size([4, 3, 14, 28])
任意取樣方式 a.index_select(d,[d層的數(shù)據(jù)索引])
a.index_select(0,torch.tensor([0,2])).shape #torch.Size([2, 3, 28, 28]) a.index_select(1,torch.tensor([0,2])).shape #torch.Size([4, 2, 28, 28])
...任意維度取樣
a[...].shape #torch.Size([4, 3, 28, 28]) a[0,...].shape #torch.Size([3, 28, 28]) a[:,2,...].shape #torch.Size([4, 28, 28])
掩碼索引mask x.ge(0.5) 表示大于等于0.5的為1,小于0.5的為0
#torch.masked_select 取出掩碼對(duì)應(yīng)位置的值 x=torch.randn(3,4) mask=x.ge(0.5) torch.masked_select(x,mask) #tensor([1.6950, 1.2207, 0.6035])
具體索引 take(變量,位置) 會(huì)把變量變?yōu)橐痪S的
x=torch.randn(3,4) torch.take(x,torch.tensor([0,1,5])) #tensor([-2.2092, -0.2652, 0.4848])
到此這篇關(guān)于PyTorch定義Tensor以及索引和切片的文章就介紹到這了,更多相關(guān)PyTorch Tensor索引和切片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python基礎(chǔ)知識(shí)方法重寫(xiě)+文件處理+異常處理
這篇文章主要介紹了Python基礎(chǔ)知識(shí)方法重寫(xiě)+文件處理+異常處理,這是基礎(chǔ)知識(shí)分享的第四篇,看到這里了相信大家前幾篇都學(xué)得還不錯(cuò)吧,下面我們繼續(xù)鞏固Python基礎(chǔ)知識(shí),需要的朋友也可以參考一下2022-05-05
Python獲取中國(guó)節(jié)假日數(shù)據(jù)記錄入JSON文件
項(xiàng)目系統(tǒng)內(nèi)置的日歷應(yīng)用為了提升用戶體驗(yàn),特別設(shè)置了在調(diào)休日期顯示“休”的UI圖標(biāo)功能,那么問(wèn)題是這些調(diào)休數(shù)據(jù)從哪里來(lái)呢?我嘗試一種更為智能的方法:Python獲取中國(guó)節(jié)假日數(shù)據(jù)記錄入JSON文件2025-04-04
PyQt5高級(jí)界面控件之QTableWidget的具體使用方法
這篇文章主要介紹了PyQt5高級(jí)界面控件之QTableWidget的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python?基于xml.etree.ElementTree實(shí)現(xiàn)XML對(duì)比示例詳解
這篇文章主要介紹了Python?基于xml.etree.ElementTree實(shí)現(xiàn)XML對(duì)比,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
在Pandas中進(jìn)行數(shù)據(jù)重命名的方法示例
Pandas作為Python中最流行的數(shù)據(jù)處理庫(kù),提供了強(qiáng)大的數(shù)據(jù)操作功能,其中數(shù)據(jù)重命名是常見(jiàn)且基礎(chǔ)的操作之一,本文將通過(guò)簡(jiǎn)潔明了的講解和豐富的代碼示例,帶你快速掌握如何在Pandas中進(jìn)行數(shù)據(jù)重命名,需要的朋友可以參考下2025-01-01
python實(shí)現(xiàn)的分析并統(tǒng)計(jì)nginx日志數(shù)據(jù)功能示例
這篇文章主要介紹了python實(shí)現(xiàn)的分析并統(tǒng)計(jì)nginx日志數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Python針對(duì)nginx日志ip、訪問(wèn)url、狀態(tài)等數(shù)據(jù)的相關(guān)讀取、解析操作技巧,需要的朋友可以參考下2019-12-12
基于Python編寫(xiě)一個(gè)簡(jiǎn)單的端口掃描器
端口掃描是非常實(shí)用的,不止用在信息安全方面,日常的運(yùn)維也用得到。本文將利用Python編寫(xiě)一個(gè)簡(jiǎn)單的端口掃描器,需要的可以參考一下2022-04-04
ubuntu16.04制作vim和python3的開(kāi)發(fā)環(huán)境
本文給大家介紹的是在ubuntu系統(tǒng)下制作python3開(kāi)發(fā)環(huán)境的詳細(xì)步驟,非常的實(shí)用,有需要的小伙伴可以參考下2018-09-09

