溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Pytorch中Tensor怎么用

發(fā)布時間:2021-08-07 13:58:33 來源:億速云 閱讀:129 作者:小新 欄目:開發(fā)技術

這篇文章給大家分享的是有關Pytorch中Tensor怎么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1. 范數(shù)

示例代碼:

import torch
 
a = torch.full([8], 1)
b = a.reshape([2, 4])
c = a.reshape([2, 2, 2])
 
# 求L1范數(shù)(所有元素絕對值求和)
print(a.norm(1), b.norm(1), c.norm(1))
# 求L2范數(shù)(所有元素的平方和再開根號)
print(a.norm(2), b.norm(2), c.norm(2))
 
# 在b的1號維度上求L1范數(shù)
print(b.norm(1, dim=1))
# 在b的1號維度上求L2范數(shù)
print(b.norm(2, dim=1))
 
# 在c的0號維度上求L1范數(shù)
print(c.norm(1, dim=0))
# 在c的0號維度上求L2范數(shù)
print(c.norm(2, dim=0))

輸出結果:

tensor(8.) tensor(8.) tensor(8.)
tensor(2.8284) tensor(2.8284) tensor(2.8284)
tensor([4., 4.])
tensor([2., 2.])
tensor([[2., 2.],
  [2., 2.]])
tensor([[1.4142, 1.4142],
  [1.4142, 1.4142]])

2. 一些常用操作

(1)均值、累加、最小、最大、累積

示例代碼:

b = torch.arange(8).reshape(2, 4).float()
print(b)
# 均值,累加,最小,最大,累積
print(b.mean(), b.sum(), b.min(), b.max(), b.prod())
# 打平后的最小最大值索引
print(b.argmax(), b.argmin())

輸出結果:

tensor([[0., 1., 2., 3.],
  [4., 5., 6., 7.]])
tensor(3.5000) tensor(28.) tensor(0.) tensor(7.) tensor(0.)
tensor(7) tensor(0)

注意:上面的argmax、argmin操作默認會將Tensor打平后取最大值索引和最小值索引,如果不希望Tenosr打平,而是求給定維度上的索引,需要指定在哪一個維度上求最大值索引或最小值索引。

比如,有shape=[4, 10]的Tensor,表示4張圖片在10分類的概率結果,我們需要知道每張圖片的最可能的分類結果:

a = torch.rand(4, 10)
print(a)
# 在第二維度上求最大值索引
print(a.argmax(dim=1))

輸出結果:

tensor([[0.0711, 0.5641, 0.7945, 0.6964, 0.3609, 0.5817, 0.1705, 0.6913, 0.1263,
   0.8346],
  [0.0810, 0.0771, 0.1983, 0.0344, 0.1067, 0.9591, 0.8515, 0.3046, 0.0491,
   0.1291],
  [0.3527, 0.2676, 0.9859, 0.2656, 0.1985, 0.3759, 0.8221, 0.3571, 0.5340,
   0.7759],
  [0.0969, 0.3954, 0.5478, 0.3543, 0.8253, 0.9291, 0.4960, 0.4390, 0.3780,
   0.5858]])
tensor([9, 5, 2, 5])

(2)直接使用max和min配合dim參數(shù)也可以獲得最值索引,同時得到最值的具體值:

print(c.max(dim=1))

輸出結果:

(tensor([0.9589, 1.7394, 1.3448, 2.2079]), tensor([2, 2, 5, 7]))

(3)使用keepdim=True可以保持應有的dim,即僅僅是將求最值的那個dim的size變成了1,返回的結果是符合原Tensor語義的。

print(c.argmax(dim=1, keepdim=True))
print(c.max(dim=1, keepdim=True))

輸出結果:

tensor([[2],
  [2],
  [5],
  [7]])
(tensor([[0.9589],
  [1.7394],
  [1.3448],
  [2.2079]]), tensor([[2],
  [2],
  [5],
  [7]]))

(4)取前k大/前k小/第k小的概率值及其索引

使用topk代替max可以完成更靈活的需求,有時候不是僅僅要概率最大的那一個,而是概率最大的k個。如果不是求最大的k個,而是求最小的k個,只要使用參數(shù)largest=False,kthvalue還可以取第k小的概率值及其索引。

示例代碼:

# 2個樣本,分為10個類別的置信度
d = torch.randn(2, 10) 
# 最大概率的3個類別
print(d.topk(3, dim=1)) 
# 最小概率的3個類別
print(d.topk(3, dim=1, largest=False)) 
# 求第8小概率的類別(一共10個那就是第3大)
print(d.kthvalue(8, dim=1))

輸出結果:

(tensor([[2.0692, 1.6490, 0.9526],
  [1.5983, 1.5737, 1.5532]]), tensor([[6, 3, 5],
  [8, 1, 2]]))
(tensor([[-1.0023, -0.6423, 0.0655],
  [-1.2959, -1.1504, -0.9859]]), tensor([[4, 0, 2],
  [0, 5, 3]]))
(tensor([0.9526, 1.5532]), tensor([5, 2]))

(5)比較操作

示例代碼:

import torch
 
a = torch.randn(2, 3)
b = torch.randn(2, 3)
print(a)
print(b)
# 比較是否大于0,是對應位置返回1,否對應位置返回0,注意得到的是ByteTensor
print(a > 0) 
print(torch.gt(a, 0))
# 是否不等于0,是對應位置返回1,否對應位置返回0
print(a != 0)
# 比較每個位置是否相等,是對應位置返回1,否對應位置返回0
print(torch.eq(a, b)) 
# 比較每個位置是否相等,全部相等時才返回True
print(torch.equal(a, b), torch.equal(a, a))

輸出結果:

tensor([[-0.1425, -1.1142, 0.2224],
  [ 0.6142, 1.7455, -1.1776]])
tensor([[-0.0774, -1.1012, -0.4862],
  [-0.3110, -0.2110, 0.0381]])
tensor([[0, 0, 1],
  [1, 1, 0]], dtype=torch.uint8)
tensor([[0, 0, 1],
  [1, 1, 0]], dtype=torch.uint8)
tensor([[1, 1, 1],
  [1, 1, 1]], dtype=torch.uint8)
tensor([[0, 0, 0],
  [0, 0, 0]], dtype=torch.uint8)
False True

感謝各位的閱讀!關于“Pytorch中Tensor怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

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

AI