pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax
目錄
gather
squeeze?
expand
sum
contiguous
softmax
max
argmax
gather
torch.gather(input,dim,index,out=None)。對(duì)指定維進(jìn)行索引。比如4*3的張量,對(duì)dim=1進(jìn)行索引,那么index的取值范圍就是0~2.
input是一個(gè)張量,index是索引張量。input和index的size要么全部維度都相同,要么指定的dim那一維度值不同。輸出為和index大小相同的張量。
import torch
a=torch.tensor([[.1,.2,.3],
[1.1,1.2,1.3],
[2.1,2.2,2.3],
[3.1,3.2,3.3]])
b=torch.LongTensor([[1,2,1],
[2,2,2],
[2,2,2],
[1,1,0]])
b=b.view(4,3)
print(a.gather(1,b))
print(a.gather(0,b))
c=torch.LongTensor([1,2,0,1])
c=c.view(4,1)
print(a.gather(1,c))
輸出:
tensor([[ 0.2000, 0.3000, 0.2000],
[ 1.3000, 1.3000, 1.3000],
[ 2.3000, 2.3000, 2.3000],
[ 3.2000, 3.2000, 3.1000]])
tensor([[ 1.1000, 2.2000, 1.3000],
[ 2.1000, 2.2000, 2.3000],
[ 2.1000, 2.2000, 2.3000],
[ 1.1000, 1.2000, 0.3000]])
tensor([[ 0.2000],
[ 1.3000],
[ 2.1000],
[ 3.2000]])
squeeze?
將維度為1的壓縮掉。如size為(3,1,1,2),壓縮之后為(3,2)
import torch
a=torch.randn(2,1,1,3)
print(a)
print(a.squeeze())
輸出:
tensor([[[[-0.2320, 0.9513, 1.1613]]],
[[[ 0.0901, 0.9613, -0.9344]]]])
tensor([[-0.2320, 0.9513, 1.1613],
[ 0.0901, 0.9613, -0.9344]])
expand
擴(kuò)展某個(gè)size為1的維度。如(2,2,1)擴(kuò)展為(2,2,3)
import torch
x=torch.randn(2,2,1)
print(x)
y=x.expand(2,2,3)
print(y)
輸出:
tensor([[[ 0.0608],
[ 2.2106]],
[[-1.9287],
[ 0.8748]]])
tensor([[[ 0.0608, 0.0608, 0.0608],
[ 2.2106, 2.2106, 2.2106]],
[[-1.9287, -1.9287, -1.9287],
[ 0.8748, 0.8748, 0.8748]]])
sum
size為(m,n,d)的張量,dim=1時(shí),輸出為size為(m,d)的張量
import torch
a=torch.tensor([[[1,2,3],[4,8,12]],[[1,2,3],[4,8,12]]])
print(a.sum())
print(a.sum(dim=1))
輸出:
tensor(60)
tensor([[ 5, 10, 15],
[ 5, 10, 15]])
contiguous
返回一個(gè)內(nèi)存為連續(xù)的張量,如本身就是連續(xù)的,返回它自己。一般用在view()函數(shù)之前,因?yàn)関iew()要求調(diào)用張量是連續(xù)的。可以通過(guò)is_contiguous查看張量?jī)?nèi)存是否連續(xù)。
import torch
a=torch.tensor([[[1,2,3],[4,8,12]],[[1,2,3],[4,8,12]]])
print(a.is_contiguous)
print(a.contiguous().view(4,3))
輸出:
<built-in method is_contiguous of Tensor object at 0x7f4b5e35afa0>
tensor([[ 1, 2, 3],
[ 4, 8, 12],
[ 1, 2, 3],
[ 4, 8, 12]])
softmax
假設(shè)數(shù)組V有C個(gè)元素。對(duì)其進(jìn)行softmax等價(jià)于將V的每個(gè)元素的指數(shù)除以所有元素的指數(shù)之和。這會(huì)使值落在區(qū)間(0,1)上,并且和為1。
?
import torch
import torch.nn.functional as F
a=torch.tensor([[1.,1],[2,1],[3,1],[1,2],[1,3]])
b=F.softmax(a,dim=1)
print(b)
輸出:
tensor([[ 0.5000, 0.5000],
[ 0.7311, 0.2689],
[ 0.8808, 0.1192],
[ 0.2689, 0.7311],
[ 0.1192, 0.8808]])
max
返回最大值,或指定維度的最大值以及index
import torch
a=torch.tensor([[.1,.2,.3],
[1.1,1.2,1.3],
[2.1,2.2,2.3],
[3.1,3.2,3.3]])
print(a.max(dim=1))
print(a.max())
輸出:
(tensor([ 0.3000, 1.3000, 2.3000, 3.3000]), tensor([ 2, 2, 2, 2]))
tensor(3.3000)
argmax
返回最大值的index
import torch
a=torch.tensor([[.1,.2,.3],
[1.1,1.2,1.3],
[2.1,2.2,2.3],
[3.1,3.2,3.3]])
print(a.argmax(dim=1))
print(a.argmax())
輸出:
tensor([ 2, 2, 2, 2])
tensor(11)
---------------------
作者:歡樂(lè)的小豬
來(lái)源:CSDN
原文:https://blog.csdn.net/hbu_pig/article/details/81454503
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
總結(jié)
以上是生活随笔為你收集整理的pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: pytorch lstm crf 代码理
- 下一篇: 计算机组成原理中的“上溢”和“下溢”分别