pytorch torch包
torch 包含了多維張量的數(shù)據(jù)結(jié)構(gòu)以及基于其上的多種數(shù)學(xué)操作古毛。
函數(shù)
- torch.is_tensor(obj)
判斷obj是否為一個 pytorch tensor唆铐。 - torch.is_storage(obj)
判斷obj是否為一個 pytorch storage對象绒瘦。 - torch.set_default_tensor_type(type)
設(shè)置默認(rèn)的tensor中的數(shù)據(jù)類型 - torch.numel(tensor)
返回tensor張量中的元素個數(shù) - torch.eye(n, m=None, out=None)
返回一個2維float張量椭盏,對角線位置全1,其它位置全0。m默認(rèn)等于n。 - torch.from_numpy(ndarray)
將numpy.ndarray 轉(zhuǎn)換為pytorch的 longtensor往声。注意:返回的張量tensor和numpy的ndarray共享同一內(nèi)存空間。修改一個會導(dǎo)致另外一個也被修改戳吝。 - torch.linspace(start, end, steps=step, out=None)
返回一個1維float張量浩销,包含在區(qū)間start 和 end 上均勻間隔的step個點(diǎn)。 輸出1維張量的長度為step听哭。 - torch.logspace(start, end, steps=step, out=None)
返回一個1維float張量慢洋,包含在區(qū)間和
上以對數(shù)刻度均勻間隔的steps個點(diǎn)。 輸出1維張量的長度為step陆盘。
- torch.rand(*sizes)
返回一個float張量普筹,包含了從區(qū)間[0,1)的均勻分布中抽取的一組隨機(jī)數(shù),形狀由可變參數(shù)sizes 決定隘马。 - torch.randn(*sizes)
返回一個float張量太防,包含了從標(biāo)準(zhǔn)正態(tài)分布N(0,1)中抽取的一組隨機(jī)數(shù),形狀由可變參數(shù)sizes 決定酸员。 - torch.randperm(n)
返回一個從0 到n -1 的隨機(jī)整數(shù)排列構(gòu)成的Longtensor蜒车。 - torch.arange(start, end, step=steps)
返回一個在區(qū)間[start,end)內(nèi),從start開始幔嗦,step為步長的一組值構(gòu)成的float張量酿愧。 - torch.zeros(*sizes)
返回一個全為標(biāo)量 0 的float張量,形狀由可變參數(shù)sizes 決定邀泉。 - torch.cat(inputs, dimension=dimension)
在給定維度上對輸入的張量序列inputs進(jìn)行連接操作嬉挡。inputs:tensor構(gòu)成的序列。 - torch.chunk(tensor, chunks, dim=dimension)
在給定維度上將輸入張量進(jìn)行分塊汇恤。chunks:分塊個數(shù)棘伴。 - torch.gather(input, dim, index)
沿給定軸dim,將輸入的index張量對應(yīng)的值進(jìn)行聚合屁置。
index必須為longtensor焊夸。
例子:
t = torch.Tensor([[1,2,3],[4,5,6]])
index_a = torch.LongTensor([[0,0],[0,1]])
index_b = torch.LongTensor([[0,1,1],[1,0,0]])
print(t)
print(torch.gather(t,dim=1,index=index_a))
print(torch.gather(t,dim=0,index=index_b))
>>tensor([[1., 2., 3.],
[4., 5., 6.]])
>>tensor([[1., 1.],
[4., 5.]])
>>tensor([[1., 5., 6.],
[4., 2., 3.]])
計(jì)算torch.gather(t,dim=1,index=index_a)細(xì)節(jié):
output[0,0] = input[0,index[0,0]]= input[0,0]=1
output[0,1] = input[0,index[0,1]]= input[0,0]=1
output[1,0] = input[1,index[1,0]]= input[1,0]=4
output[1,1] = input[1,index[1,1]]= input[1,1]=5
計(jì)算torch.gather(t,dim=0,index=index_b)細(xì)節(jié):
output[0,0] = input[index[0,0],0]=input[0,0] = 1
output[0,1] = input[index[0,1],1]=input[1,1] = 5
output[0,2] = input[index[0,2],2]=input[1,2] = 6
output[1,0] = input[index[1,0],0]=input[1,0] = 4
output[1,1] = input[index[1,1],1]=input[0,1] = 2
output[1,2] = input[index[1,2],2]=input[0,2] = 3
- torch.index_select(input, dim, index)
沿著指定維度對輸入選取指定index進(jìn)行切片.
index必須為longtensor。 - torch.masked_select(input, mask, out=None)
根據(jù)掩碼張量mask中的二元值蓝角,取輸入張量中的指定項(xiàng)( mask為一個 ByteTensor)阱穗,將取值返回到一個新的1維張量。
mask須跟input有相同數(shù)量的元素?cái)?shù)目使鹅,但形狀或維度不需要相同揪阶。 - torch.nonzero(input)
返回一個包含輸入input中非零元素索引的張量。 - torch.split(tensor, split_size, dim=0)
將輸入張量延指定維度分割成相等形狀的split_size個trunks患朱。 - torch.squeeze(input, dim=None)
將輸入張量形狀中的1 去除并返回鲁僚。若指定dim,則只會在該給定維度上擠壓。
注:返回張量與輸入張量共享內(nèi)存冰沙,所以改變其中一個的內(nèi)容會改變另一個侨艾。 - torch.stack(sequence, dim=0)
沿著一個維度對輸入張量序列進(jìn)行連接。
與cat的區(qū)別在于拓挥,stack會增加一個新的維度唠梨。例如a、b為2x3的tensor侥啤,在dim = 0時拼接当叭,cat返回的結(jié)果為2x6的tensor,stack返回的結(jié)果為2x2x3的結(jié)果盖灸。
a=torch.rand((1,2))
>>> b=torch.rand((1,2))
>>> print(a)
tensor([[0.8738, 0.8688]])
>>> print(b)
tensor([[0.9889, 0.6731]])
>>> print(torch.stack((a,b),0))
tensor([[[0.8738, 0.8688]],
[[0.9889, 0.6731]]])
>>> print(torch.stack((a,b),1))
tensor([[[0.8738, 0.8688],
[0.9889, 0.6731]]])
>>> print(torch.stack((a,b),0).size())
torch.Size([2, 1, 2])
>>> print(torch.stack((a,b),1).size())
torch.Size([1, 2, 2])
- torch.t(input)
輸入一個矩陣(2維張量)蚁鳖,并轉(zhuǎn)置0, 1維。被視為函數(shù)transpose(input, 0, 1)的簡寫函數(shù)赁炎。 - torch.transpose(input, dim0, dim1)
輸入矩陣input交換維度dim0和dim1醉箕。 輸出張量與輸入張量共享內(nèi)存,改變其中一個會導(dǎo)致另外一個也被修改甘邀。 - torch.unsqueeze(input, dim)
在指定的維度增加一維琅攘。 - torch.unbind(tensor,dim)
刪除某個維度垮庐。 - torch.manual_seed(seed)
設(shè)定生成隨機(jī)數(shù)的種子松邪。 - torch.initial_seed()
返回生成隨機(jī)數(shù)的原始種子值。 - torch.bernoulli(input)
以input 對應(yīng)位置的值作為伯努利分布中取1的概率哨查,按該分布采樣取值逗抑。 - torch.normal(means, std)
返回從正態(tài)分布采樣得到的隨機(jī)tensor。 - torch.multinomial(input, num_samples,replacement=False, out=None)
把input中的每行作為權(quán)重寒亥,采樣得到對應(yīng)的Index邮府。replacement =Ture代表可重復(fù)采樣。 - torch.save(obj,f)
將obj保存到f文件溉奕。 - torch.load(f,map_location = location )
讀取f文件
# Load all tensors onto the CPU
>>> torch.load('tensors.pt', map_location='cpu')
# Load all tensors onto the CPU, using a function
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage)
# Load all tensors onto GPU 1
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1))
# Map tensors from GPU 1 to GPU 0
>>> torch.load('tensors.pt', map_location={'cuda:1':'cuda:0'})
- torch.abs(input)
對張量每個元素計(jì)算絕對值 - torch.acos(input)
對張量每個元素計(jì)算反余弦 - torch.cos(input)
對張量每個元素計(jì)算余弦 - torch.cosh(input)
對張量每個元素計(jì)算雙曲余弦 - torch.sinh(input)
對張量每個元素計(jì)算雙曲正弦 - torch.sin(input)
對張量每個元素計(jì)算正弦 - torch.asin(input)
對張量每個元素計(jì)算反正弦 - torch.tan(input)
對張量每個元素計(jì)算正切 - torch.atan(input)
對張量每個元素計(jì)算反正切 - torch.tanh(input)
對張量每個元素計(jì)算雙曲正切 - torch.ceil(input)
對張量每個元素向上取整 - torch.floor(input)
對張量每個元素向下取整 - torch.round(input)
對張量每個元素四舍五入取整 - torch.add(input, value)
對張量每個元素加上value - torch.add(input, value=value, other)
= (other*value)+input
注:張量 input and other的尺寸不需要匹配褂傀,但元素總數(shù)必須一樣 - torch.addcdiv(tensor, value= value, tensor1, tensor2)
用tensor2對tensor1逐元素相除,然后乘以標(biāo)量值value 并加到tensor加勤。 - torch.addcmul(tensor, value=value, tensor1, tensor2)
用tensor2對tensor1逐元素相乘仙辟,并對結(jié)果乘以標(biāo)量值value然后加到tensor。 - torch.clamp(input, min, max)
對張量每個元素限制到區(qū)間[min,max] - torch.div(input, value)
對張量每個元素除以value - torch.div(input,other)
input與other逐元素相除 - torch.exp(tensor)
對張量每個元素求指數(shù) - torch.remainder(input, divisor)
返回input張量每個元素除以divisor的余數(shù)鳄梅,余數(shù)與除數(shù)divisor有相同的符號叠国。 - torch.fmod(input, divisor)
返回input張量每個元素除以divisor的余數(shù),余數(shù)與被除數(shù)input有相同的符號戴尸。 - torch.frac(tensor)
返回每個元素的分?jǐn)?shù)部分粟焊。 - torch.lerp(start, end, weight)
返回start和end做線性插值的結(jié)果。
out = start + weight*(end - start) - torch.log(input)
返回每個元素的自然對數(shù) - torch.log1p(input)
返回每個元素+1的自然對數(shù) - torch.mul(input, value)
返回每個元素*value - torch.mul(input, other)
返回input與other逐元素相乘 - torch.neg(input)
返回每個元素取負(fù) - torch.pow(input, exponent)
返回input按元素求exponent次冪值。
exponent為標(biāo)量:
exponent為張量: - torch.pow(base, input)
base為標(biāo)量项棠,input張量: - torch.reciprocal(input)
返回每個元素的倒數(shù) - torch.rsqrt(input)
返回每個元素的平方根倒數(shù) - torch.sigmoid(input)
返回每個元素的sigmoid函數(shù)對應(yīng)的值 - torch.sign(input)
返回每個元素的正負(fù)符號(+(-)1) - torch.sqrt(input)
返回每個元素的平方根 - torch.trunc(input)
返回一個新張量悲雳,包含輸入input張量每個元素的截?cái)嘀?舍棄其小數(shù)部分) - torch.cumprod(input, dim)
返回輸入沿指定維度的累積積。 - torch.cumsum(input, dim)
返回輸入沿指定維度的累積和沾乘。 - torch.dist(input, other, p=p)
返回 (input - other) 的 p范數(shù) 怜奖。 - torch.mean(input)
返回輸入張量所有元素的均值。 - torch.mean(input, dim)
返回輸入張量給定維度dim上每行的均值翅阵。 - torch.median(input, dim=-1)
返回給定維dim上歪玲,每行的中位數(shù)值和其索引。 - torch.mode(input, dim=-1)
返回給定維dim上掷匠,每行的眾數(shù)值和其索引滥崩。 - torch.norm(input, p=2)
返回輸入張量input 的p 范數(shù)。
torch.norm(input, p, dim)
返回輸入張量給定維dim 上每行的p 范數(shù)讹语。 - torch.prod(input)
返回輸入張量input 所有元素的積钙皮。 - torch.std(input)
返回輸入張量input 所有元素的標(biāo)準(zhǔn)差。 - torch.sum(input)
返回輸入張量input 所有元素的和顽决。 - torch.sum(input, dim)
返回輸入張量給定維度上每行的和短条。 - torch.var(input)
返回輸入張量所有元素的方差 - torch.eq(input, other)
逐元素比較input, other是否相等。 - torch.equal(tensor1, tensor2)
如果兩個張量有相同的形狀和元素值才菠,則返回True 茸时,否則 False - torch.ge(input, other)
逐元素比較input和other,即是否 input>=other赋访。 - torch.gt(input, other)
逐元素比較input和other可都。 - torch.kthvalue(input, k, dim=None)
取輸入張量input指定維上第k 個最小值和其索引。 - torch.le(input, other)
逐元素比較input和other 蚓耽, 即是否input<=other - torch.lt(input, other)
逐元素比較input和other 渠牲, 即是否 input<other - torch.max(input)
返回輸入張量所有元素的最大值。 - torch.min(input)
返回輸入張量所有元素的最小值步悠。 - torch.min(input, dim)
返回輸入張量給定維度上每行的最小值签杈,并同時返回每個最小值的位置索引 - torch.min(input, other)
input中逐元素與other相應(yīng)位置的元素對比,返回最小值到輸出張量鼎兽。
- torch.ne(input, other)
逐元素比較input和other 答姥, 即是否 input!=other。1相等接奈,0不相等踢涌。 - torch.sort(input, dim=None, descending=False)
對輸入張量input沿著指定維按升序排序。如果不給定dim序宦,則默認(rèn)為輸入的最后一維睁壁。如果指定參數(shù)descending為True,則按降序排序。 - torch.topk(input, k, dim=None, largest=True, sorted=True)
返回沿給定dim維度輸入張量input中 k 個最大值和其下標(biāo)潘明。largest為 False 行剂,則返回最小的 k 個值。返回一個元組 (values,indices)钳降,sorted 為True厚宰,將會確保返回的 k 個值被排序。 - torch.cross(input, other)
返回兩個張量input和other的向量積(叉積) - torch.diag(input, diagonal=0)
如果輸入是一個向量(1D 張量)遂填,則返回一個以input為對角線元素的2D方陣
如果輸入是一個矩陣(2D 張量)铲觉,則返回一個包含input對角線元素的1D張量
參數(shù)diagonal指定對角線 - torch.histc(input, bins=bins, min=min, max=max)
計(jì)算輸入張量的直方圖。以min和max為range邊界吓坚,將其均分成bins個直條撵幽,然后將排序好的數(shù)據(jù)劃分到各個直條(bins)中。如果min和max都為0, 則利用數(shù)據(jù)中的最大最小值作為邊界礁击。 - torch.renorm(input, p, dim, maxnorm)
返回一個張量盐杂,包含規(guī)范化后的各個子張量,使得沿著dim維劃分的各子張量的p范數(shù)小于maxnorm哆窿。
如果p范數(shù)的值小于maxnorm链烈,則當(dāng)前子張量不需要修改 - torch.trace(input)
返回輸入2維矩陣對角線元素的跡 - torch.tril(input, k=0)
返回一個張量,包含輸入矩陣(2D張量)的下三角部分挚躯,其余部分被設(shè)為0强衡。
參數(shù)k控制對角線: k = 0, 主對角線;k > 0, 主對角線之上 ;k < 0, 主對角線之下 - torch.triu(input, k=0)
返回一個張量,包含輸入矩陣(2D張量)的上三角部分秧均,其余部分被設(shè)為0食侮。
參數(shù)k控制對角線: k = 0, 主對角線;k > 0, 主對角線之上 ;k < 0, 主對角線之下 - torch.addbmm(beta=1, mat, alpha=1, batch1, batch2)
對兩個批batch1和batch2內(nèi)存儲的矩陣進(jìn)行批矩陣乘操作号涯,附帶reduced add 步驟( 所有矩陣乘結(jié)果沿著第一維相加)目胡。矩陣mat加到最終結(jié)果。
- torch.addmm(beta=1, mat, alpha=1, mat1, mat2)
對矩陣mat1和mat2進(jìn)行矩陣乘操作链快。矩陣mat加到最終結(jié)果誉己。
- torch.addmv(beta=1, tensor, alpha=1, mat, vec)
對矩陣mat和向量vec對進(jìn)行相乘操作。向量tensor加到最終結(jié)果域蜗。
- torch.addr(beta=1, mat, alpha=1, vec1, vec2)
對向量vec1和vec2對進(jìn)行張量積操作巨双。矩陣mat加到最終結(jié)果。
- torch.baddbmm(beta=1, mat, alpha=1, batch1, batch2)
對兩個批batch1和batch2內(nèi)存儲的矩陣進(jìn)行批矩陣乘操作霉祸,再加上矩陣mat得到最終結(jié)果筑累。
- torch.bmm(batch1, batch2)
對存儲在兩個批batch1和batch2內(nèi)的矩陣進(jìn)行批矩陣乘操作。 - torch.dot(tensor1, tensor2)
返回兩個張量的點(diǎn)乘結(jié)果 - torch.eig(a, eigenvectors=False)
返回實(shí)方陣a 的特征值和特征向量 - torch.ger(vec1, vec2)
計(jì)算兩向量vec1,vec2的張量積丝蹭。n*m的矩陣 - torch.inverse(input)
返回方陣input的逆 - torch.mm(mat1, mat2)
返回矩陣mat1和mat2進(jìn)行相乘的結(jié)果 - torch.mv(mat, vec)
返回矩陣mat和向量vec進(jìn)行相乘的結(jié)果 - torch.qr(input)
計(jì)算輸入矩陣的QR分解:返回兩個矩陣q ,r慢宗, 使得 x=q?r ,這里q 是一個半正交矩陣與 r 是一個上三角矩陣 - torch.svd(input, some=True)
返回對形如 n×m的實(shí)矩陣 A 進(jìn)行奇異值分解的結(jié)果,U,S,V=torch.svd(A), - torch.symeig(input, eigenvectors=False)
返回實(shí)對稱矩陣input的特征值和特征向量镜沽。eigenvectors=False只計(jì)算特征值