溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在PyTorch中使用Tensor進(jìn)行數(shù)據(jù)統(tǒng)計(jì)

發(fā)布時(shí)間:2021-04-06 16:24:19 來(lái)源:億速云 閱讀:313 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了怎么在PyTorch中使用Tensor進(jìn)行數(shù)據(jù)統(tǒng)計(jì),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

張量范數(shù):torch.norm(input, p=2) → float

返回輸入張量 input 的 p 范數(shù)

舉個(gè)例子:

>>> import torch
>>> a = torch.full([8], 1)
>>> b = a.view(2, 4)
>>> c = a.view(2, 2, 2)
>>> a.norm(1), b.norm(1), c.norm(1)	# 求 1- 范數(shù)
(tensor(8.), tensor(8.), tensor(8.))
>>> a.norm(2), b.norm(2), c.norm(2)	# 求 2- 范數(shù)
(tensor(2.8284), tensor(2.8284), tensor(2.8284))
>>> a.norm(3), b.norm(3), c.norm(3)# 求 ∞- 范數(shù)
(tensor(2.), tensor(2.), tensor(2.))
>>> b
tensor([[1., 1., 1., 1.],
    [1., 1., 1., 1.]])
>>> b.norm(1, 1) # 在 1 維度上求 1- 范數(shù)
tensor([4., 4.])
>>> b.norm(2, 1) # 在 1 維度上求 2- 范數(shù)
b.norm(1, 2)
>>> c
tensor([[[1., 1.],
     [1., 1.]],

    [[1., 1.],
     [1., 1.]]])
>>> c.norm(1, 0) # 在 0 維度上求 1- 范數(shù)
tensor([[2., 2.],
    [2., 2.]])
>>> c.norm(2, 0) # 在 0 維度上求 2- 范數(shù)
tensor([[1.4142, 1.4142],
    [1.4142, 1.4142]])

只有一個(gè)參數(shù)時(shí),表示對(duì)整個(gè)張量求范數(shù),參數(shù)表示范數(shù)的冪指數(shù)值。

有兩個(gè)參數(shù)時(shí),表示在張量某一維度對(duì)尺寸中每一部分求范數(shù),第一個(gè)參數(shù)是范數(shù)的冪指數(shù)值,第二個(gè)參數(shù)是選擇的維度。

張量統(tǒng)計(jì)

最基礎(chǔ)的統(tǒng)計(jì)方法,比如張量中的最小值、最大值、均值、累加、累積。

舉個(gè)例子:

>>> a = torch.arange(8).view(2, 4).float()
>>> a
tensor([[0., 1., 2., 3.],
    [4., 5., 6., 7.]])
>>> a.min(), a.max(), a.mean(), a.sum(), a.prod() # 分別求最小值、最大值、均值、累加、累積
(tensor(0.), tensor(7.), tensor(3.5000), tensor(28.), tensor(0.))
>>> a.argmin(), a.argmax() # 分別是把張量打平后最小值、最大值的索引
(tensor(0), tensor(7))
>>> a.argmin(1), a.argmax(1) # 不打平求 1 維度中每一部分最小值、最大值的索引
(tensor([0, 0]), tensor([3, 3]))

dim和keepdim

>>> a = torch.randn(5, 10)
>>> a
tensor([[-0.6346, -0.9074, 0.1525, 0.1901, -0.5391, -0.2437, 1.0150, -0.0427,
     -1.5336, 0.8542],
    [-0.1879, 1.9947, -0.3524, -1.2559, -0.8129, -0.3018, 0.5654, 0.8428,
     -0.3517, -0.7787],
    [ 0.0686, 0.6166, 0.2632, -0.0947, -0.5592, -1.4041, 1.5565, 1.5616,
     -1.3076, -0.1137],
    [ 0.5205, -1.5716, -1.1277, 0.8096, -0.2123, -0.0974, 0.7698, 1.1373,
     0.5165, 0.5256],
    [-0.4162, 0.3170, 0.2368, 1.1695, -0.1960, -0.3285, 0.2420, 1.6468,
     0.2646, 0.4573]])
>>> a.max(dim=1)
(tensor([1.0150, 1.9947, 1.5616, 1.1373, 1.6468]), tensor([6, 1, 7, 7, 7]))
>>> a.argmax(dim=1)
tensor([6, 1, 7, 7, 7])

max 添加 dim 后不僅顯示了 1 維度中每一部分的最大值,還顯示了其索引

>>> a.max(dim=1, keepdim=True)
(tensor([[1.0150],
    [1.9947],
    [1.5616],
    [1.1373],
    [1.6468]]), tensor([[6],
    [1],
    [7],
    [7],
    [7]]))
>>> a.argmax(dim=1, keepdim=True)
tensor([[6],
    [1],
    [7],
    [7],
    [7]])

保持維度一致。添加 keepdim 后,得出的結(jié)果維度不改變,原來(lái)是二維的數(shù)據(jù),得出的結(jié)果還是二維。不添加得出的結(jié)果就是一維的。

比較操作

torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)

沿給定 dim 維度返回輸入張量 input 中 k 個(gè)最大值。 如果不指定 dim,則默認(rèn)為 input 的最后一維。 如果為 largest 為 False ,則返回最小的 k 個(gè)值。

返回一個(gè)元組 (values,indices),其中 indices 是原始輸入張量 input 中測(cè)元素下標(biāo)。 如果設(shè)定布爾值 sorted 為_(kāi)True_,將會(huì)確保返回的 k 個(gè)值被排序。

torch.kthvalue(input, k, dim=None, out=None) -> (Tensor, LongTensor) 取輸入張量 input 指定維上第 k 個(gè)最小值。如果不指定 dim,則默認(rèn)為 input 的最后一維。

返回一個(gè)元組 (values,indices),其中indices是原始輸入張量input中沿dim維的第 k 個(gè)最小值下標(biāo)。

舉個(gè)例子:

>>> b = torch.randn(5, 10)
>>> b
tensor([[ 0.1863, 0.0160, -1.0657, -1.8984, 2.3274, 0.6534, 1.8126, 1.8666,
     0.4830, -0.7800],
    [-0.9359, -1.0655, 0.8321, 1.6265, 0.6812, -0.2870, 0.6987, 0.6067,
     -0.1318, 0.7819],
    [-3.1129, 0.9571, -0.1319, -1.0016, 0.7267, 0.1060, -0.2926, 0.3492,
     1.0026, 0.2924],
    [-0.7101, -0.8327, 0.5463, 0.3805, -0.8720, -1.6723, 0.0365, 1.5540,
     0.1940, 1.4294],
    [ 0.4174, -0.9414, -0.0351, -1.6142, -0.7802, -2.3916, -2.4822, 0.7233,
     -0.7037, 0.2725]])
>>> b.topk(3, dim=1)
(tensor([[2.3274, 1.8666, 1.8126],
    [1.6265, 0.8321, 0.7819],
    [1.0026, 0.9571, 0.7267],
    [1.5540, 1.4294, 0.5463],
    [0.7233, 0.4174, 0.2725]]), tensor([[4, 7, 6],
    [3, 2, 9],
    [8, 1, 4],
    [7, 9, 2],
    [7, 0, 9]]))
>>> b.topk(3, dim=1, largest=False)
(tensor([[-1.8984, -1.0657, -0.7800],
    [-1.0655, -0.9359, -0.2870],
    [-3.1129, -1.0016, -0.2926],
    [-1.6723, -0.8720, -0.8327],
    [-2.4822, -2.3916, -1.6142]]), tensor([[3, 2, 9],
    [1, 0, 5],
    [0, 3, 6],
    [5, 4, 1],
    [6, 5, 3]]))
>>> a.kthvalue(8, dim=1)
(tensor([0.1034, 0.8940, 0.6155, 0.4210, 0.1955]), tensor([1, 2, 6, 4, 7]))

topk 添加 largest=False 就是返回最小,不添加就是返回最大。

kthvalue 返回以從大到小排列的指定位置的數(shù)。上面代碼中即為返回第 8 小的數(shù)。

torch.eq(input, other, out=None) → Tensor

比較元素相等性。第二個(gè)參數(shù)可為一個(gè)數(shù)或與第一個(gè)參數(shù)同類型形狀的張量。

torch.equal(tensor1, tensor2) → bool

如果兩個(gè)張量有相同的形狀和元素值,則返回 True ,否則 False。

舉個(gè)例子:

>>> a = torch.ones(2, 3)
>>> b = torch.randn(2, 3)
>>> torch.eq(a, b)
tensor([[0, 0, 0],
    [0, 0, 0]], dtype=torch.uint8)
>>> torch.eq(a, a)
tensor([[1, 1, 1],
    [1, 1, 1]], dtype=torch.uint8)
>>> torch.equal(a, a)
True

eq 比較張量中的每個(gè)數(shù)據(jù),equal 比較整個(gè)張量

上述內(nèi)容就是怎么在PyTorch中使用Tensor進(jìn)行數(shù)據(jù)統(tǒng)計(jì),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI