pytorch torch類

本文介紹一些可能有用的torch類中的方法。

torch.cat(inputs, dimension=0)

對input進(jìn)行拼接,參數(shù)表示在那個(gè)維度進(jìn)行拼接。

x = torch.randn(2, 3)
print(x)
print(torch.cat((x, x, x), 0))

torch.gather(input, dim, index, out=None)

沿給定軸dim,將輸入索引張量index指定位置的值進(jìn)行賦值。
賦值公式為如下:

out[i][j][k] = tensor[index[i][j][k]][j][k]  # dim=0
out[i][j][k] = tensor[i][index[i][j][k]][k]  # dim=1
out[i][j][k] = tensor[i][j][index[i][j][k]]  # dim=3

也就是修改輸入的dim維的數(shù)值排列。
示例:

t = torch.Tensor([[1, 2], [3, 4]])
# gather是對tensor進(jìn)行重新排序
print(torch.gather(t, 1, torch.LongTensor([[0, 0], [0, 1]])))

torch.masked_select(input, mask, out=None)

根據(jù)掩碼張量mask中的二元值,取輸入張量中的指定項(xiàng)( mask為一個(gè) ByteTensor),將取值返回到一個(gè)新的1D張量。
也就是在原本的input中選擇部分?jǐn)?shù)值。

torch.nonzero(input, out=None)

返回一個(gè)包含輸入input中非零元素索引的張量。輸出張量中的每行包含輸入中非零元素的索引。

t = torch.Tensor([[0, 2], [3, 2]])
print(torch.nonzero(t))
# tensor([[0, 1],
#         [1, 0],
#         [1, 1]])

torch.squeeze(input, dim=None, out=None)

將輸入張量形狀中的1 去除并返回。 如果輸入是形如(A×1×B×1×C×1×D),那么輸出形狀就為: (A×B×C×D)。

  • dim (int, optional) – 如果給定,則input只會(huì)在給定維度擠壓

示例:

x = torch.zeros(2,1,2,1,2)
print(x.size())
# torch.Size([2, 1, 2, 1, 2])
y = torch.squeeze(x)
print(y.size())
# torch.Size([2, 2, 2])
y = torch.squeeze(x, 0)
print(y.size())
# torch.Size([2, 1, 2, 1, 2])
y = torch.squeeze(x, 1)
print(y.size())
# torch.Size([2, 2, 1, 2])

torch.stack(sequence, dim=0)

沿著新的維度對輸入張量進(jìn)行排序。最外面的維度是0.

示例:

a = torch.Tensor([[1,2,3],[4,5,6],[7,8,9]])
b = a * 10
c = b * 10
print(a)
print(torch.stack([a,b,c],1))
# tensor([[[  1.,   2.,   3.],
#          [ 10.,  20.,  30.],
#          [100., 200., 300.]],
# 
#         [[  4.,   5.,   6.],
#          [ 40.,  50.,  60.],
#          [400., 500., 600.]],
# 
#         [[  7.,   8.,   9.],
#          [ 70.,  80.,  90.],
#          [700., 800., 900.]]])

torch.t(input, out=None)

對二維向量進(jìn)行轉(zhuǎn)置。

torch.unsqueeze(input, dim, out=None)

返回一個(gè)新的張量,對輸入的制定位置插入維度 1。如果dim為負(fù),則將會(huì)被轉(zhuǎn)化dim+input.dim()+1。

x = torch.Tensor([1, 2, 3, 4])
print(torch.unsqueeze(x, 1))
# tensor([[1.],
#         [2.],
#         [3.],
#         [4.]])

隨機(jī)抽樣

torch.manual_seed(seed)

設(shè)定生成隨機(jī)數(shù)的種子。

torch.initial_seed()

返回生成隨機(jī)數(shù)的原始種子值。

torch.normal(means, std, out=None)

返回一個(gè)張量,包含從給定參數(shù)means,std的離散正態(tài)分布中抽取隨機(jī)數(shù)。

  • means (Tensor) – 均值
  • std (Tensor) – 標(biāo)準(zhǔn)差
  • out (Tensor) – 可選的輸出張量

數(shù)學(xué)操作

torch.abs(input, out=None)

計(jì)算輸入張量的每個(gè)元素絕對值。

torch.acos(input, out=None)

返回一個(gè)新張量,包含輸入張量每個(gè)元素的反余弦。

torch.add(input, value, out=None)

對輸入張量input逐元素加上標(biāo)量值value,并返回結(jié)果到一個(gè)新的張量out,即 out=tensor+value。

torch.add(input, value=1, other, out=None)

other 張量的每個(gè)元素乘以一個(gè)標(biāo)量值value,并加到iput 張量上。返回結(jié)果到輸出張量out。即,out=input+(other?value)。

torch.ceil(input, out=None)

對輸入input張量每個(gè)元素向上取整。

torch.floor(input, out=None)

向下取整。

torch.clamp(input, min, max, out=None)

將輸入input張量每個(gè)元素的夾緊到區(qū)間 [min,max],并返回結(jié)果到一個(gè)新張量。


操作公式

torch.div(input, value, out=None)

將input逐元素除以標(biāo)量值value,并返回結(jié)果到輸出張量out。

torch.lerp(start, end, weight, out=None)

對兩個(gè)張量以start,end做線性插值, 將結(jié)果返回到輸出張量。


差值公式

torch.log(input, out=None)

計(jì)算input 的自然對數(shù)。

torch.mul(input, value, out=None)

用標(biāo)量值value乘以輸入input的每個(gè)元素,并返回一個(gè)新的結(jié)果張量。

torch.reciprocal(input, out=None)

返回一個(gè)新張量,包含輸入input張量每個(gè)元素的倒數(shù),即 1.0/x。

torch.remainder(input, divisor, out=None)

返回一個(gè)新張量,包含輸入input張量每個(gè)元素的除法余數(shù)。

torch.round(input, out=None)

四舍五入。

torch.rsqrt(input, out=None)

計(jì)算每個(gè)元素的平方根倒數(shù)。

torch.sigmoid(input, out=None)

輸入input張量每個(gè)元素的sigmoid值。

累計(jì)操作

torch.cumprod(input, dim, out=None)

返回輸入沿指定維度的累積積。例如,如果輸入是一個(gè)N 元向量,則結(jié)果也是一個(gè)N 元向量,第i 個(gè)輸出元素值為yi=x1?x2?x3?...?xi。

torch.cumsum(input, dim, out=None)

返回輸入沿指定維度的累積和。第i 個(gè)輸出元素值為 yi=x1+x2+x3+...+xi。

torch.mean(input)

返回輸入張量所有元素的均值。

torch.prod(input, dim, out=None)

返回輸入張量給定維度上每行的積。
示例:

a = torch.Tensor([[1,2,3],[4,5,6],[7,8,9]])
print(torch.prod(a,0))
# tensor([  6., 120., 504.])

torch.std(input) → float

返回輸入張量input 所有元素的標(biāo)準(zhǔn)差。

torch.sum(input, dim, out=None) → Tensor

返回輸入張量給定維度上每行的和。 輸出形狀與輸入相同,除了給定維度上為1。

torch.var(input) → float

返回輸入張量所有元素的方差。

比較操作

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

對input張量和第二個(gè)參數(shù)進(jìn)行比較。如果相同則在對應(yīng)位置返回1.第二個(gè)參數(shù)可以是張量或數(shù)。
示例:

print(torch.eq(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# tensor([[1, 0],
#         [0, 1]], dtype=torch.uint8)
print(torch.eq(torch.Tensor([[1, 2], [3, 4]]), 1))
# tensor([[1, 0],
#         [0, 0]], dtype=torch.uint8)

torch.equal(tensor1, tensor2) → bool

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

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

逐元素比較input和other的大小,即是否 input>=other。

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

逐元素比較input和other的大小 , 即是否input>other。

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

逐元素比較input和other , 即是否input<=other 。

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

逐元素比較input和other , 即是否 input<other。

torch.max()

返回輸入張量所有元素的最大值。

torch.max(input, dim, max=None, max_indices=None) -> (Tensor, LongTensor)

返回輸入張量給定維度上每行的最大值,并同時(shí)返回每個(gè)最大值的位置索引。

torch.min(input) → float

返回輸入張量所有元素的最小值。

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

input中逐元素與other相應(yīng)位置的元素對比,返回最小值到輸出張量。即,outi=min(tensori,otheri)。

torch.sort(input, dim=None, descending=False, out=None) -> (Tensor, LongTensor)

對輸入張量input沿著指定維按升序排序。如果不給定dim,則默認(rèn)為輸入的最后一維。如果指定參數(shù)descending為True,則按降序排序。
返回元組 (sorted_tensor, sorted_indices) , sorted_indices 為原始輸入中的下標(biāo)。

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中測元素下標(biāo)。 如果設(shè)定布爾值sorted 為True,將會(huì)確保返回的 k 個(gè)值被排序。

其他操作

torch.cross(input, other, dim=-1, out=None) → Tensor

返回沿著維度dim上,兩個(gè)張量input和other的向量積(叉積)。 input和other 必須有相同的形狀,且指定的dim維上size必須為3。

矩陣運(yùn)算

torch.addbmm(beta=1, mat, alpha=1, batch1, batch2, out=None) → Tensor

對兩個(gè)批batch1和batch2內(nèi)存儲(chǔ)的矩陣進(jìn)行批矩陣乘操作。

torch.addmm(beta=1, mat, alpha=1, mat1, mat2, out=None) → Tensor

對矩陣mat1和mat2進(jìn)行矩陣乘操作。
out=(beta?M)+(alpha?mat1@mat2)

torch.bmm(batch1, batch2, out=None) → Tensor

對存儲(chǔ)在兩個(gè)批batch1和batch2內(nèi)的矩陣進(jìn)行批矩陣乘操作。batch1和 batch2都為包含相同數(shù)量矩陣的3維張量。 如果batch1是形為b×n×m的張量,batch1是形為b×m×p的張量,則out和mat的形狀都是n×p,即 res=(beta?M)+(alpha?sum(batch1i@batch2i,i=0,b))

torch.dot(tensor1, tensor2) → float

計(jì)算兩個(gè)張量的點(diǎn)乘(內(nèi)乘),兩個(gè)張量都為1-D 向量.

torch.mm(mat1, mat2, out=None) → Tensor

對矩陣mat1和mat2進(jìn)行相乘。 如果mat1 是一個(gè)n×m 張量,mat2 是一個(gè) m×p 張量,將會(huì)輸出一個(gè) n×p 張量out。

torch.mv(mat, vec, out=None) → Tensor

對矩陣mat和向量vec進(jìn)行相乘。 如果mat 是一個(gè)n×m張量,vec 是一個(gè)m元 1維張量,將會(huì)輸出一個(gè)n 元 1維張量。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容