torch复现论文简明笔记
1)常數初始化:
x = F.normalize(x, p=2, dim=1)按行計算
x = F.normalize(x, p=2, dim=0)按列計算
torch.empty(size)返回形狀為size的空tensor
torch.zeros(size)全部是0的tensor
torch.zeros_like(input)返回跟input的tensor一個size的全零tensor
torch.ones(size)全部是1的tensor
torch.ones_like(input)返回跟input的tensor一個size的全一tensor
torch.arange(start=0, end, step=1)返回一個從start到end的序列,可以只輸入一個end參數,就跟python的range()一樣了。
torch.full(size, fill_value)這個有時候比較方便,把fill_value這個數字變成size形狀的張量
2)隨機抽樣(隨機初始化):
torch.rand(size) [0,1)內的均勻分布隨機數
torch.rand_like(input)返回跟input的tensor一樣size的0-1隨機數
torch.randn(size)返回標準正太分布N(0,1)的隨機數
torch.normal(mean, std, out=None)正態分布。這里注意,mean和std都是tensor,返回的形狀由mean和std的形狀決定,一般要求兩者形狀一樣。如果,mean缺失,則默認為均值0,如果std缺失,則默認標準差為1.
3)tensor的切片、合并、變形、抽取操作
torch.cat(seq, dim=0, out=None),把一堆tensor丟進去,按照dim指定的維度拼接、堆疊在一起.
示例:
In [70]: x = torch.tensor([[1,2,3]])
In [71]: x
Out[71]: tensor([[1, 2, 3]])
?#按第0維度堆疊,對于矩陣,相當于“豎著”堆
In [72]: print(torch.cat((x,x,x),0))
tensor([[1, 2, 3],
? ? ? ? [1, 2, 3],
? ? ? ? [1, 2, 3]])
?#按第1維度堆疊,對于矩陣,相當于“橫著”拼
In [73]: print(torch.cat((x,x,x),1))?
tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3]])
torch.chunk(tensor, chunks, dim=0)把tensor切成塊,數量由chunks指定。
示例:
In [74]: a = torch.arange(10)
In [75]: a
Out[75]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [76]: torch.chunk(a,4)
Out[76]: (tensor([0, 1, 2]), tensor([3, 4, 5]), tensor([6, 7, 8]), tensor([9]))
切塊還有torch.split(tensor, split_size_or_sections, dim=0)具體區別大家自行查閱文檔
按index選擇:torch.index_select(input, dim, index, out=None)
按mask選擇:torch.masked_select(input, mask, out=None)
經常會使用的“壓扁”函數:torch.squeeze(input),壓縮成1維。注意,壓縮后的tensor和原來的tensor共享地址
改變形狀:torch.reshape(input, shape)以及tensor.view(shape).前者是把tensor作為函數的輸入,后者是任何tensor的函數。實際上,二者的返回值,都只是讓我們從另一種視角看某個tensor,所以不會改變本來的形狀,除非你把結果又賦值給原來的tensor。下面給一個例子對比二者的用法:
示例:
In [82]: a
Out[82]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 單純的調用view函數:
In [83]: a.view(2,5)
Out[83]:
tensor([[0, 1, 2, 3, 4],
? ? ? ? [5, 6, 7, 8, 9]])
# a的形狀并不會變化
In [84]: print(a)?
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 試試reshape函數:
In [86]: torch.reshape(a,[5,2])
Out[86]:
tensor([[0, 1],
? ? ? ? [2, 3],
? ? ? ? [4, 5],
? ? ? ? [6, 7],
? ? ? ? [8, 9]])
# a的形狀依然不會變化:
In [87]: a
Out[87]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
要想讓a的形狀變化,比如把結果賦值給a,比如a = a.view(2,5)。
4)基本數學操作
加法直接加:x+y ? torch.add(x,y) ? ? torch.add(input, value, out=None)
乘法:torch.mul(input, other, out=None)用input乘以other
除法:torch.div(input, other, out=None)用input除以other
指數:torch.pow(input, exponent, out=None)
開根號:torch.sqrt(input, out=None)
四舍五入到整數:torch.round(input, out=None)
argmax函數:torch.argmax(input, dim=None, keepdim=False)返回指定維度最大值的序號,dim給定的定義是:the demention to reduce.也就是把dim這個維度的,變成這個維度的最大值的index。例如:
sigmoid函數:torch.sigmoid(input, out=None)
tanh函數:torch.tanh(input, out=None)
torch.abs(input, out=None)取絕對值
torch.ceil(input, out=None)向上取整,等于向下取整+1
torch.clamp(input, min, max, out=None)刀削函數,把輸入數據規范在min-max區間,超過范圍的用min、max代替
5)Torch Tensor與Numpy的互相轉換
Tensor-->Numpy ? 直接用.numpy()即可。
Numpy-->Tensor ? 用torch.from_numpy()來轉換。
示例:
In [11]: a = torch.ones(2,4)
In [12]: a
Out[12]:
tensor([[1., 1., 1., 1.],
? ? ? ? [1., 1., 1., 1.]])
In [13]: b = a.numpy()
In [14]: b
Out[14]:
array([[1., 1., 1., 1.],
? ? ? ?[1., 1., 1., 1.]], dtype=float32)
In [15]: a.add_(1)
Out[15]:
tensor([[2., 2., 2., 2.],
? ? ? ? [2., 2., 2., 2.]])
In [16]: b
Out[16]:
array([[2., 2., 2., 2.],
? ? ? ?[2., 2., 2., 2.]], dtype=float32)
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
?
?
總結
以上是生活随笔為你收集整理的torch复现论文简明笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: torch基础学习
- 下一篇: 解决deepin微信无法登录