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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax

發(fā)布時(shí)間:2023/11/28 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

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)題。

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