您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“torch.mean()和torch.pow()怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“torch.mean()和torch.pow()怎么使用”吧!
產(chǎn)生大小為指定的,正態(tài)分布的采樣點,數(shù)據(jù)類型是tensor
torch.mean(input) 輸出input 各個元素的的均值,不指定任何參數(shù)就是所有元素的算術(shù)平均值,指定參數(shù)可以計算每一行或者 每一列的算術(shù)平均數(shù)
例如:
a=torch.randn(3) #生成一個一維的矩陣 b=torch.randn(1,3) #生成一個二維的矩陣 print(a) print(b) torch.mean(a)
結(jié)果:
tensor([-1.0737, -0.8689, -0.9553]) tensor([[-0.4005, -0.6812, 0.0958]]) tensor(-0.9659)
如果指定參數(shù)的話,
a=torch.randn(4,4) print(a) c=torch.mean(a,dim=0,keepdim=True) print(c) d=torch.mean(a,dim=1,keepdim=True) print(d)
結(jié)果:
tensor([[ 0.2378, -1.1380, 0.7964, -0.1413], [ 0.4622, -1.7003, -1.1628, 0.8930], [-2.0379, -1.7137, 0.6423, -0.2026], [ 0.3512, -0.1251, -0.8315, 2.2642]]) tensor([[-0.2467, -1.1693, -0.1389, 0.7033]]) tensor([[-0.0612], [-0.3770], [-0.8280], [ 0.4147]])
可以看到dim指定為1時,求得是行的平均值,指定為0時,求得是列的平均值。
對輸入的每分量求冪次運算
a=torch.tensor(3) b=torch.pow(a,2) print(b) c=torch.randn(4) print(c) d=torch.pow(c,2) print(d)
結(jié)果:
tensor(9) tensor([ 0.0923, 0.7006, -0.2963, 0.6543]) tensor([0.0085, 0.4909, 0.0878, 0.4282])
torch.matmul 是做矩陣乘法
例如:
a=torch.tensor([1,2,3]) b=torch.tensor([3,4,5]) torch.matmul(a, b)
結(jié)果:
tensor(26)
torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
返回一個填充了標(biāo)量值1的張量,其大小與之相同 input。
我是在Pytorch自動求導(dǎo)中第一次發(fā)現(xiàn)此方法的,
例如:
import torch from torch.autograd import Variable m = Variable(torch.FloatTensor([[2, 3]]), requires_grad=True) # 構(gòu)建一個 1 x 2 的矩陣 n = Variable(torch.zeros(1, 2)) # 構(gòu)建一個相同大小的 0 矩陣 print(m) print(n) # 通過 m 中的值計算新的 n 中的值 n[0, 0] = m[0, 0] ** 2 n[0, 1] = m[0, 1] ** 3 print(n[0,0]) print(n)
結(jié)果:
tensor([[2., 3.]], requires_grad=True) tensor([[0., 0.]]) tensor(4., grad_fn=<SelectBackward>) tensor([[ 4., 27.]], grad_fn=<CopySlices>)
n.backward(torch.ones_like(n)) # 相當(dāng)于n.backward(torch.FloatTensor([[1,1]])) print(m.grad)
結(jié)果:
tensor([[ 4., 27.]])
到此,相信大家對“torch.mean()和torch.pow()怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。