PyTorch定義Tensor及索引和切片(最新推薦)
深度學(xué)習(xí)--PyTorch定義Tensor
一、創(chuàng)建Tensor
1.1未初始化的方法
?這些方法只是開辟了空間,所附的初始值(非常大,非常小,0),后面還需要我們進(jìn)行數(shù)據(jù)的存入。
torch.empty():返回一個沒有初始化的Tensor,默認(rèn)是FloatTensor類型。
#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():返回沒有初始化的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():返回沒有初始化的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:返回一個定值
#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):返回一個[min,max),步長為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):返回一個[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):返回一個[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或者對角陣 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() 只能接收一個或兩個參數(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:一般用于位置操作。類似random.shuffle()。
torch.randperm(8) #tensor([2, 6, 7, 5, 3, 4, 1, 0])
二、索引與切片
簡單索引方式
a=torch.rand(4,3,28,28) a[0].shape #torch.Size([3, 28, 28]) a[0,0,0,0] #tensor(0.9373)
批量索引方式 開始位置:結(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])
隔行采樣方式 開始位置:結(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 取出掩碼對應(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(變量,位置) 會把變量變?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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django中使用 Closure Table 儲存無限分級數(shù)據(jù)
對于數(shù)據(jù)量大的情況(比如用戶之間有邀請鏈,有點三級分銷的意思),就要用到 closure table 的結(jié)構(gòu)來進(jìn)行存儲。這篇文章主要介紹了Django中使用 Closure Table 儲存無限分級數(shù)據(jù),需要的朋友可以參考下2019-06-06pytorch實現(xiàn)mnist手寫彩色數(shù)字識別
這篇文章主要介紹了pytorch-實現(xiàn)mnist手寫彩色數(shù)字識別,文章圍繞主題展開詳細(xì)的內(nèi)容姐介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09Pytorch使用DataLoader實現(xiàn)批量加載數(shù)據(jù)
這篇文章主要介紹了Pytorch使用DataLoader實現(xiàn)批量加載數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02python3 常見解密加密算法實例分析【base64、MD5等】
這篇文章主要介紹了python3 常見解密加密算法,結(jié)合實例形式分析了Python的base64模塊加密,以及基于pycrypto模塊的MD5加密等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法
這篇文章主要介紹了Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法,結(jié)合實例形式分析了win32com模塊下載、連接mysql、查詢獲取表結(jié)構(gòu)以及使用win32com生成word表格的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07