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
))
full 默認類型是FloatTensor torch.full([2,3],7)#dim=2,即兩行三列的tensor torch.full([],7)#dim=0,即標量 torch.full([1],7)#dim=1,一維一個元素的向量 torch.full([2],7)#dim=1, 一維兩個元素的向量 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
)
torch.zeros
( 3*3
)
torch.ones_like
( a
)
a
= torch.ones
( 3,3
)
print
( a
)
'' '
tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]])
' ''
randperm 打散 idx=torch.randperm(2)#因為a、b都有兩行,所以參數(shù)是2, a[idx] b[idx]必須是同一索引
import torch
a
= torch.rand
( 2,3
)
b
= torch.rand
( 2,2
)
idx
= torch.randperm
( 2
)
print
( a
)
print
( b
)
print
( idx
)
print
( a
[ idx
] )
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
)
a
[ 0
] .shape
a
[ 0,0
] .shape
a
[ 0,0,2,4
]
select first/last N 取連續(xù)的片段
a
[ :2
] .shape
a
[ :2,:1,:,:
] .shape
a
[ :2,1:,:,:
] .shape
a
[ :2,-1:,:,:
] .shape
select by steps 寫兩個:表示隔行采樣 a[:,:,0:28:2,0:28:2].shape #0:28:2表示[0,28)且每隔2取一次,所以是14
a[:,:,::2,::2].shape #::2取所有,每隔2取一次, 所以是14,與上同 冒號總結(jié): : all,只有冒號取所有[0,n) :x [0,x) x: [x,n) start:end:step [start,end)隔step取一次
13索引與切片2
select by specific index 具體的索引 先定義了一個tensor,這里用到了linspace和view方法.
1第一個參數(shù)是索引的對象 2第二個參數(shù)0表示按行索引,1表示按列進行索引 3第三個參數(shù)是一個tensor,就是索引的序號,比如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
)
b
= torch.index_select
( a, 0, torch.tensor
( [ 0, 2
] ))
print
( b
)
c
= torch.index_select
( a, 1, torch.tensor
( [ 1, 3
] ))
print
( c
)
a
= torch.rand
( 4,3,28,28
)
print
( a
) b
= a.index_select
( 0,torch.tensor
( [ 0,2
] ))
print
( b
)
print
( b.shape
) c
= a.index_select
( 1,torch.tensor
( [ 1,2
] ))
print
( c
)
print
( c.shape
) d
= a.index_select
( 2,torch.arange
( 8
))
print
( d.shape
)
… 表示任意多的維度可以取代任意多的:,:,:,,根據(jù)實際情況判斷是取多少維度
print
( a
[ .. .
] .shape
)
print
( a
[ 0,
.. .
] .shape
)
print
( a
[ 0,
.. .,::2
] .shape
)
print
( a
[ :,1,
.. .,:
] .shape
)
print
( a
[ .. .,:2
] .shape
)
select by mask 用掩碼,但是把數(shù)據(jù)打平處理 mask=x.ge(0.5)#值大于0.5的將對應位置索引的矩陣置TRUE 掩碼為TRUE的值取出來 但是會把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 缺點是維度丟失,因為b可能是a.view(4,784)而來,但是b不知道原來a怎么存儲的,即不知道[B,C,W,H]是什么具體的值
a
= torch.rand
( 4,1,28,28
)
a.view
( 4,28*28
) a.view
( 4*28,28
) a.view
( 4*1,28,28
)
view的時候必須保證size是一樣 prod(a.size)==prod(a’.size)
15維度變換2
squeeze v.s unsqueeze 擠壓維度和展開維度 unsqueeze(pos/index) 表示在pos/index這個位置插入一個維度,對數(shù)據(jù)本身沒影響,只是增加維度,換個角度理解數(shù)據(jù)的存儲
a的torch.Size
( [ 4,1,28,28
] )
a.unsqueeze
( 0
) .shape
a.unsqueeze
( -1
) .shape
index的范圍是[-a.dim()-1,a.din()+1] 即[-5,5) 索引的對應如下
0 1 2 3 4 正的索引是在當前索引前插入
-5 -4 -3 -2 -1 負的索引是在當前索引后插入
總的來說就是放在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])' ''
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結(jié)
以上是生活随笔 為你收集整理的pytorch教程龙曲良11-15 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。