日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pytorch教程龙曲良11-15

發(fā)布時(shí)間:2024/4/13 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytorch教程龙曲良11-15 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

11創(chuàng)建tensor02

randn一般是均值為0,方差為1的正態(tài)分布N(0,1),也可以自定義N(u,std)用torch.normal

torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1)) #比如先把一個(gè)2*5的矩陣打平變成1*10,然后normal后再reshape為2*5 #torch.full先構(gòu)建一個(gè)一維長度為10全是0的tensor,這樣他們的mean就都是0 #torch.arange使得標(biāo)準(zhǔn)差std分布為[1,0.9,0.8....]

full
默認(rèn)類型是FloatTensor
torch.full([2,3],7)#dim=2,即兩行三列的tensor
torch.full([],7)#dim=0,即標(biāo)量
torch.full([1],7)#dim=1,一維一個(gè)元素的向量
torch.full([2],7)#dim=1, 一維兩個(gè)元素的向量
tensor([7.,7.])


arange等差數(shù)列
[start,end) 左閉右開

linspace按照steps等分切割 等差數(shù)列
[start,end]左閉右閉
/logspace按照steps等分切割
按照step^start ——>step^end等分
[start,end]左閉右閉
例:torch.logspace(0,1,steps=10)
10^0=1 到 10^1=10 間按10^0.1 10^0.2…劃分

ones:所有元素為1
zero:所有元素為1
eye:對角線賦值為1
如果不是對角矩陣后面賦值為0
以上接收的都是shape

torch.eye(3)#3*3的對角矩陣 torch.zeros(3*3)#3*3的0矩陣 torch.ones_like(a)#和a一樣形狀的3*3的1矩陣 a=torch.ones(3,3)#dim=2 因?yàn)檫€是一個(gè)二維的平面矩陣 print(a) ''' tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]]) '''


randperm 打散
idx=torch.randperm(2)#因?yàn)閍、b都有兩行,所以參數(shù)是2,
a[idx]
b[idx]必須是同一索引

import torch a=torch.rand(2,3) b=torch.rand(2,2) idx=torch.randperm(2)#因?yàn)閍、b都有兩行,所以參數(shù)是2 print(a) print(b) print(idx)#[1,0] print(a[idx])#對于a,bidx必須是同一索引 print(b[idx]) print(a) print(b) '''tensor([[0.2436, 0.6611, 0.8526],[0.1075, 0.2996, 0.2687]]) tensor([[0.9829, 0.6718],[0.0802, 0.7499]]) tensor([1, 0])#表示dim=1,這樣行互換,如果是[0,1]保持不變 tensor([[0.1075, 0.2996, 0.2687],[0.2436, 0.6611, 0.8526]]) tensor([[0.0802, 0.7499],[0.9829, 0.6718]])'''

12索引與切片1
indexing
dim 0 first

a=toch.rand(4,3,28,28)#b c h w b表示batchsize就是幾張圖片 a[0].shape#取第一個(gè)維度的 就是第一張圖片 包含三個(gè)維度:通道,長,寬 a[0,0].shape#取dim=0,dim=1的shape即第0張圖片的第0通道 a[0,0,2,4]#第0張圖片的第0通道的第二行第四列的像素點(diǎn) 打印出來是一個(gè)常量,標(biāo)量,dim=0


select first/last N
取連續(xù)的片段

a[:2].shape #0->2,即0,1不包含2,所以包含的是前兩張圖片的shape #結(jié)果是[2,3,28,28] a[:2,:1,:,:].shape #即第0,1張圖片的第0個(gè)通道的所有圖片的數(shù)據(jù), #:默認(rèn)取全部即從0-28全部都取a[:2,1:,:,:].shape #:2即第0,1張圖片,兩張圖片 #1:從第1個(gè)通道開始到全部的通道即第1,2兩個(gè)通道#正向索引0,1,2 #反向索引-3,-2,-1 a[:2,-1:,:,:].shape #:2即第0,1張圖片,兩張圖片 #-1:從最后一個(gè)通道開始到結(jié)束,所以只有一個(gè)通道

select by steps
寫兩個(gè):表示隔行采樣
a[:,:,0:28:2,0:28:2].shape
#0:28:2表示[0,28)且每隔2取一次,所以是14

a[:,:,::2,::2].shape
#::2取所有,每隔2取一次, 所以是14,與上同
冒號(hào)總結(jié):
: all,只有冒號(hào)取所有[0,n)
:x [0,x)
x: [x,n)
start:end:step [start,end)隔step取一次

13索引與切片2

select by specific index
具體的索引
先定義了一個(gè)tensor,這里用到了linspace和view方法.

1第一個(gè)參數(shù)是索引的對象
2第二個(gè)參數(shù)0表示按行索引,1表示按列進(jìn)行索引
3第三個(gè)參數(shù)是一個(gè)tensor,就是索引的序號(hào),比如b里面tensor[0, 2]表示第0行和第2行,c里面tensor[1, 3]表示第1列和第3列。
返回切片后的張量tensor.

import torcha = torch.linspace(1, 12, steps=12).view(3, 4) print(a) #tensor([[ 1., 2., 3., 4.], # [ 5., 6., 7., 8.], # [ 9., 10., 11., 12.]])b = torch.index_select(a, 0, torch.tensor([0, 2])) print(b) #tensor([[ 1., 2., 3., 4.], # [ 9., 10., 11., 12.]])c = torch.index_select(a, 1, torch.tensor([1, 3])) print(c) #tensor([[ 2., 4.], # [ 6., 8.], # [10., 12.]]) a=torch.rand(4,3,28,28) print(a)b=a.index_select(0,torch.tensor([0,2]))#0表示是第0個(gè)維度,[0,2]表示指定的是第0張和第2張圖片 #其他三個(gè)維度的數(shù)全取 所以打印的是一個(gè)[2,3,28,28]的tensor print(b) print(b.shape)c=a.index_select(1,torch.tensor([1,2]))#1表示是第1個(gè)維度,[1,2]表示指定的是第1和第2通道 #其他三個(gè)維度的數(shù)全取 所以打印的是一個(gè)[4,2,28,28]的tensor 四張圖片,取rgb的gb兩個(gè)通道,其他全取 print(c) print(c.shape)#torch.Size([4, 2, 28, 28])d=a.index_select(2,torch.arange(8))#對第2個(gè)維度操作,就是對行操作,取[0,8)即0-7行共8行,其他全取 print(d.shape)#torch.Size([4, 3, 8, 28])


表示任意多的維度可以取代任意多的:,:,:,,根據(jù)實(shí)際情況判斷是取多少維度

print(a[...].shape)#其實(shí)就是a torch.Size([4, 3, 28, 28]) print(a[0,...].shape)#其實(shí)就是a[0] ...代表chw 取的是第0張圖片,其他全取 torch.Size([3, 28, 28]) print(a[0,...,::2].shape)#其實(shí)就是a[0,:,:,::2] ...代表ch 取第0張圖片,在列上隔2取 torch.Size([3, 28, 14]) print(a[:,1,...,:].shape)#其實(shí)就是a[:,1]或者a[:,1,:,:] 取第1個(gè)通道其他全取 torch.Size([4, 28, 28]) print(a[...,:2].shape)#其實(shí)就是a[:,:,:,:2]前三維度全取,最后列只取0和1列 torch.Size([4, 3, 28, 2]) torch.Size([4, 3, 28, 2])

select by mask
用掩碼,但是把數(shù)據(jù)打平處理
mask=x.ge(0.5)#值大于0.5的將對應(yīng)位置索引的矩陣置TRUE
掩碼為TRUE的值取出來
但是會(huì)把34的矩陣打平成112的向量,再取出符合的值,最后的shape 維度是1

import torchx=torch.randn(3,4) print(x)mask=x.ge(0.5) print(mask)a=torch.masked_select(x,mask) print(a) print(a.shape) ''' tensor([[ 1.3789, -1.8209, 0.9027, 1.6865],[ 0.3729, -0.6339, 0.5470, 0.6969],[-0.3212, 0.0666, -1.2894, -2.0430]]) tensor([[ True, False, True, True],[False, False, True, True],[False, False, False, False]]) tensor([1.3789, 0.9027, 1.6865, 0.5470, 0.6969]) torch.Size([5])'''``` **select by flatten index**比較index_select(dim,torch.tensor([x1,x2])) 把2*3的tensor打平成1*6的vector所以索引變了```bash src=torch.randn(2,3) print(src) c=torch.take(src,torch.tensor([0,2,5])) print(c) ''' tensor([[-2.6371, 1.2871, -0.6298],[-0.7618, -1.1859, 0.4471]]) tensor([-2.6371, -0.6298, 0.4471])'''

14維度變換1

Operation
? View/reshape
? Squeeze/unsqueeze
? Transpose/t/permute
? Expand/repeat

View/reshape
缺點(diǎn)是維度丟失,因?yàn)閎可能是a.view(4,784)而來,但是b不知道原來a怎么存儲(chǔ)的,即不知道[B,C,W,H]是什么具體的值

a=torch.rand(4,1,28,28)#load進(jìn)4張圖片,通道為1表示灰度單色圖,長28,寬28 a.view(4,28*28)#chw后三個(gè)維度合在一起,變成[4,784]就是每張圖片忽略位置信息上下信息,合成784,這使用于全連接層a.view(4*28,28)#前三個(gè)維度合并在一起bch合并,變成[N,28]表示只關(guān)注每行的28個(gè)像素點(diǎn)的信息a.view(4*1,28,28)#前兩個(gè)維度b和c合在一起,表示四個(gè)方框框,只關(guān)注features map這個(gè)屬性,不關(guān)注灰度是單色還是彩色,不關(guān)注features map是來自哪個(gè)通道


view的時(shí)候必須保證size是一樣
prod(a.size)==prod(a’.size)

15維度變換2

squeeze v.s unsqueeze擠壓維度和展開維度

unsqueeze(pos/index) 表示在pos/index這個(gè)位置插入一個(gè)維度,對數(shù)據(jù)本身沒影響,只是增加維度,換個(gè)角度理解數(shù)據(jù)的存儲(chǔ)

a的torch.Size([4,1,28,28]) a.unsqueeze(0).shape#torch.Size([1,4,1,28,28]) 可以理解成在batch之前添加了一組或一個(gè)集合,里面有四張照片,但是本身的數(shù)據(jù)沒變化,只是理解方式變化了 a.unsqueeze(-1).shape#torch.Size([4,1,28,28,1]) 可以理解成在最后面增加了一個(gè)方差或者均值的維度,但是本身的數(shù)據(jù)沒變化,只是理解方式變化了

index的范圍是[-a.dim()-1,a.din()+1] 即[-5,5)
索引的對應(yīng)如下

0 1 2 3 4 正的索引是在當(dāng)前索引前插入 -5 -4 -3 -2 -1 負(fù)的索引是在當(dāng)前索引后插入 總的來說就是放在index所指的位置上即可

a=torch.tensor([1.2,2.3]) print(a) print(a.dim()) print(a.shape) b=a.unsqueeze(-1) print(b) print(b.shape) c=a.unsqueeze(0) print(c) print(c.shape) '''tensor([1.2000, 2.3000]) 1 torch.Size([2]) tensor([[1.2000],[2.3000]]) torch.Size([2, 1]) tensor([[1.2000, 2.3000]]) torch.Size([1, 2])'''

超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的pytorch教程龙曲良11-15的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。