Pytorch框架的學習與使用_day02


索引,切片,連接,換位Indexing, Slicing, Joining, MutatingOps
 
 
torch.cat(inputs, dimension = 0) -> Tensor

給定維度上對輸入張量序列seq進行連接操作

torch.cat可以看作torch.split和torch.chunk的反操作

參數:

inputs(sequence of Tensors) - 可以是任意相同Tensor類型的Python序列

dimension(int, optional) - 沿着此維連接張量序列

例子:

>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.8240, -0.6438,  0.1779],
        [ 1.8698, -0.3803,  0.0556]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.8240, -0.6438,  0.1779],
        [ 1.8698, -0.3803,  0.0556],
        [ 0.8240, -0.6438,  0.1779],
        [ 1.8698, -0.3803,  0.0556],
        [ 0.8240, -0.6438,  0.1779],
        [ 1.8698, -0.3803,  0.0556]])  # size 6*3 0維向量進行連接
>>> torch.cat((x, x, x), 1)
tensor([[ 0.8240, -0.6438,  0.1779,  0.8240, -0.6438,  0.1779,  0.8240, -0.6438,
          0.1779],
        [ 1.8698, -0.3803,  0.0556,  1.8698, -0.3803,  0.0556,  1.8698, -0.3803,
          0.0556]]) # size 2*9 1維向量進行連接

 

torch.chunk

torch.chunk(tensor, chunks, dim = 0)

在給定維度上將張量進行分塊

參數:

tensor - 待分塊的輸入張量

chunks(int) - 分塊的個數

dim(int) - 沿着此維度進行分塊

例子:

>>> x
tensor([[ 0.8240, -0.6438,  0.1779],
        [ 1.8698, -0.3803,  0.0556]])

>>> torch.chunk(x, 2, 0)
(tensor([[ 0.8240, -0.6438, 0.1779]]), tensor([[ 1.8698, -0.3803, 0.0556]]))


>>> torch.chunk(x, 2, 1) (tensor([[ 0.8240, -0.6438], [ 1.8698, -0.3803]]), tensor([[0.1779], [0.0556]]))

torch.gather

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

沿給定軸dim 將輸入索引張量index指定位置的值進行聚合

對於一個三維張量,輸出可以定義為

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 = 2 

參數:

input(Tensor) - 源張量

dim(int) - 索引的軸

index(Long Tensor) - 聚合元素的下標

out(Tensor, optional) - 目標張量

例子:

>>> t = torch.Tensor([[1, 2], [3, 4]])
>>> torch.gather(t, 1, torch.LongTensor([[0, 0], [1,0]]))
tensor([[1., 1.],
        [4., 3.]])

可以看出,gather的作用是這樣的,index實際上是索引,具體是行還是列的索引要看前面dim 的指定,比如對於我們的栗子,【1,2;3,4】,指定dim=1,也就是橫向,那么索引就是列號。index的大小就是輸出的大小,所以比如index是【0,0;0,0】,那么看index第一行,0列指的是1,同理,第二行為1,0。這樣就輸入為【1,1;4,3】,參考這樣的解釋看上面的輸出結果,即可理解gather的含義

torch.index_select

torch.index_select(input, dim, index, out = None) -->Tensor

沿着制定維度對輸入進行切片,取index中指定的相應項,回到一個新的張量,返回張量與原始張量tensor有相同的維度

返回的張量不與原始張亮共享內存空間

參數:

input(Tensor) - 源張量

dim(int) - 索引的軸

index(Long Tensor) - 包含索引下標的一維張量

out(Tensor, optional) - 目標張量

例子:

>>> x = torch.randn(3, 4)
>>> x
tensor([[-0.1374, -0.6321,  0.8015, -2.0121],
        [-0.8106,  1.0078, -0.7167,  0.5915],
        [-0.8627,  0.5883,  0.9542,  0.1841]])
>>> indices = torch.LongTensor([0, 2])
>>> torch.index_select(x, 0, indices)    # dim 0 按列索引
tensor([[-0.1374, -0.6321,  0.8015, -2.0121],
        [-0.8627,  0.5883,  0.9542,  0.1841]])
>>> torch.index_select(x, 1, indices)    # dim 1 按行索引
tensor([[-0.1374,  0.8015],
        [-0.8106, -0.7167],
        [-0.8627,  0.9542]])

 

torch.nonzero

torch.nonzero(input, out = None)  --> Tensor

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

如果輸入input有n維,則輸出的索引張量output的形狀為z×n 這里z是輸入張量input中所有非零元素的個數

參數:

input(Tensor) - 源張量

out(LongTensor, optional) - 包含索引值的結果張量

例子:

>>> torch.nonzero(torch.Tensor([1, 1, 1, 0, 1]))
tensor([[0],
        [1],
        [2],
        [4]])   # 4×1

 

torch.split

torch.split(tensor, split_size, dim = 0)

將輸入的張量分割成相等形狀的chunks(如果能分), 如果沿指定維的張量形狀大小不能被split_size整分,則最后一個小分塊會小於其他分塊

參數:

tensor(Tensor) - 待分割張量

split_size(int) - 單個分塊的形狀大小

dim(int) - 沿着此維度進行分割

 

torch.squeeze

將輸入張量形狀中的 1 去除並返回。 如果輸入是形如 (A×1×B×1×C×1×D×1),那么輸出形狀就為:(A×B×C×D)
當給定 dim 時,那么擠壓操作只在給定維度上。
例如,輸入形狀為: (A×1×B)
squeeze(input, 0) 將會保持張量不變,只有用 squeeze(input, 1) ,形狀會變成(A×B) 。
注意: 返回張量與輸入張量共享內存,所以改變其中一個的內容會改變另一個。
參數:

input(Tensor) - 源張量

dim(int, optional) - 如果給定,則input只會在給定維度擠壓

out(Tensor, optional) - 目標張量

例子:

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

 

torch.stack

torch.stack(squence, dim = 0)

沿着一個新維度對輸入張量序列進行連接。序列中所有的張量都應該為相同形狀

參數:

sqequence(Sqequence) - 待連接的張量序列

dim(int) - 插入的維度,必須介於0與待連接的張量序列數之間

 

torch.t

torch.t(input, out = None) --> Tensor

輸入一個矩陣(2維張量),並轉置0,1維。可以被視為函數transpose(input, 0, 1)的簡寫函數

參數:

input(Tensor)- 輸入張量

out(Tensor, optional) - 結果張量

>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.5121, -0.5057, -0.5253],
        [ 0.0310,  0.8405, -0.7914]])
>>> torch.t(x)
tensor([[ 0.5121,  0.0310],
        [-0.5057,  0.8405],
        [-0.5253, -0.7914]])

 

torch.transpose

torch.transpose(input, dim0, dim1, out = None) --> Tensor

相當於torch.t

 

torch.unbind

torch.unbind(tensor, dim = 0)

移除指定維度后,返回一個元組, 包含了沿着指定維切片以后的各個切片

參數:

tensor(Tensor) - 輸入張量

dim(int) - 刪除的維度

>>> x = torch.randn(2, 3)
>>> x
tensor([[-0.1249, -0.3148,  1.8918],
        [ 1.9341, -2.3968, -1.0895]])
>>> torch.unbind(x, dim = 1)
(tensor([-0.1249,  1.9341]), tensor([-0.3148, -2.3968]), tensor([ 1.8918, -1.0895]))

 

torch.unsqueeze

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

返回一個新的張量,對輸入 的制定位置插入維度1,新舊張量共享內存

如果dim為負,則將會被轉化dim+input.dim()+1

參數:

tensor(Tensor) - 輸入張量

dim(int) - 插入維度的索引

out(Tensor, optional) - 結果張量

例子:

>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[1, 2, 3, 4]])
>>> torch.unsqueeze(x, 1)
tensor([[1],
        [2],
        [3],
        [4]])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM