Pytorch學習筆記14----torch中相關函數使用:view函數、max()函數、squeeze()函數


1.View函數

把原先tensor中的數據按照行優先的順序排成一個一維的數據(這里應該是因為要求地址是連續存儲的),然后按照參數組合成其他維度的tensor。比如說是不管你原先的數據是[[[1,2,3],[4,5,6]]]還是[1,2,3,4,5,6],因為它們排成一維向量都是6個元素,所以只要view后面的參數一致,得到的結果都是一樣的。
小案例:
import torch
a=torch.Tensor([[[1,2,3],[4,5,6]]])
b=torch.Tensor([1,2,3,4,5,6])

print(a.view(1,6))
print(b.view(1,6))
#得到的結果都是tensor([[1., 2., 3., 4., 5., 6.]])

#再看一個例子:
a=torch.Tensor([[[1,2,3],[4,5,6]]])
print(a.view(3,2))

控制台輸出:

參數不可為空。參數中的-1就代表這個位置由其他位置的數字來推斷,只要在不致歧義的情況下,view參數就可以推斷出來,也就是人可以推斷出形狀的情況下,view函數也可以推斷出來。比如a tensor的數據個數是6個,如果view(1,-1),我們就可以根據tensor的元素個數推斷出-1代表6。而如果是view(-1,-1,2),人不知道怎么推斷,機器也不知道。還有一種情況是人可以推斷出來,但是機器推斷不出來的:view(-1,-1,6),人可以知道-1都代表1,但是機器不允許同時有兩個-1。
 
2.max函數
import  torch
a = torch.randn(3,3)
print(a)#返回生成的隨機tensor(3*3)
print(torch.max(a,0))#返回每一列中最大值的那個元素,且返回索引(返回最大元素在這一列的行索引)
print(torch.max(a,1))#返回每一行中最大值的那個元素,且返回其索引(返回最大元素在這一行的列索引)
print(torch.max(a))#返回tensor a 中的最大值
print(torch.max(a,0)[0] )# 只返回最大值的每個數
print(torch.max(a,0)[1])#只返回最大值的每個索引

控制台輸出:

D:\softwaretools\anaconda\python.exe D:/pycharmprojects/hoteltest01/hoteltest01/testpy/test16.py
tensor([[ 1.6294,  0.2568,  0.2093],
        [ 0.4974, -2.1175,  0.1659],
        [ 0.0640,  1.4387, -0.5895]])
torch.return_types.max(
values=tensor([1.6294, 1.4387, 0.2093]),
indices=tensor([0, 2, 0]))
torch.return_types.max(
values=tensor([1.6294, 0.4974, 1.4387]),
indices=tensor([0, 0, 1]))
tensor(1.6294)
tensor([1.6294, 1.4387, 0.2093])
tensor([0, 2, 0])

Process finished with exit code 0

max函數用法總結:

torch.max(a) 返回輸入tensor a中所有元素的最大值

torch.max(a,0) 返回每一列中最大值的那個元素,且返回索引(返回最大元素在這一列的行索引)

torch.max(a,1) 返回每一行中最大值的那個元素,且返回其索引(返回最大元素在這一行的列索引)

torch.max()[0], 只返回最大值的每個數

troch.max()[1], 只返回最大值的每個索引

torch.max()[1].data 只返回variable中的數據部分(去掉Variable containing:)

torch.max()[1].data.numpy() 把數據轉化成numpy ndarry

torch.max()[1].data.numpy().squeeze() 把數據條目中維度為1 的刪除掉

torch.max(tensor1,tensor2) element-wise 比較tensor1 和tensor2 中的元素,返回較大的那個值

3.squeeze()函數
torch.unsqueeze(input, dim, out=None)

作用:擴展維度

返回一個新的張量,對輸入的既定位置插入維度 1

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

參數:

tensor (Tensor) – 輸入張量

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

out (Tensor, optional) – 結果張量

import torch

x = torch.Tensor([1, 2, 3, 4])  # torch.Tensor是默認的tensor類型(torch.FlaotTensor)的簡稱。

print('-' * 50)
print(x)  # tensor([1., 2., 3., 4.])
print(x.size())  # torch.Size([4])
print(x.dim())  # 1
print(x.numpy())  # [1. 2. 3. 4.]

print('-' * 50)
print(torch.unsqueeze(x, 0))  # tensor([[1., 2., 3., 4.]])
print(torch.unsqueeze(x, 0).size())  # torch.Size([1, 4])
print(torch.unsqueeze(x, 0).dim())  # 2
print(torch.unsqueeze(x, 0).numpy())  # [[1. 2. 3. 4.]]

print('-' * 50)
print(torch.unsqueeze(x, 1))
# tensor([[1.],
#         [2.],
#         [3.],
#         [4.]])
print(torch.unsqueeze(x, 1).size())  # torch.Size([4, 1])
print(torch.unsqueeze(x, 1).dim())  # 2

print('-' * 50)
print(torch.unsqueeze(x, -1))
# tensor([[1.],
#         [2.],
#         [3.],
#         [4.]])
print(torch.unsqueeze(x, -1).size())  # torch.Size([4, 1])
print(torch.unsqueeze(x, -1).dim())  # 2

print('-' * 50)
print(torch.unsqueeze(x, -2))  # tensor([[1., 2., 3., 4.]])
print(torch.unsqueeze(x, -2).size())  # torch.Size([1, 4])
print(torch.unsqueeze(x, -2).dim())  # 2

控制台輸出:

D:\softwaretools\anaconda\python.exe D:/pycharmprojects/hoteltest01/hoteltest01/testpy/test16.py
--------------------------------------------------
tensor([1., 2., 3., 4.])
torch.Size([4])
1
[1. 2. 3. 4.]
--------------------------------------------------
tensor([[1., 2., 3., 4.]])
torch.Size([1, 4])
2
[[1. 2. 3. 4.]]
--------------------------------------------------
tensor([[1.],
        [2.],
        [3.],
        [4.]])
torch.Size([4, 1])
2
--------------------------------------------------
tensor([[1.],
        [2.],
        [3.],
        [4.]])
torch.Size([4, 1])
2
--------------------------------------------------
tensor([[1., 2., 3., 4.]])
torch.Size([1, 4])
2

Process finished with exit code 0

4.torch.squeeze 詳解

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

作用:降維

將輸入張量形狀中的1 去除並返回。 如果輸入是形如(A×1×B×1×C×1×D),那么輸出形狀就為: (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) – 輸出張量

案例:

import torch
print("*" * 50)

m = torch.zeros(2, 1, 2, 1, 2)
print(m.size())  # torch.Size([2, 1, 2, 1, 2])

n = torch.squeeze(m)
print(n.size())  # torch.Size([2, 2, 2])

n = torch.squeeze(m, 0)  # 當給定dim時,那么擠壓操作只在給定維度上
print(n.size())  # torch.Size([2, 1, 2, 1, 2])

n = torch.squeeze(m, 1)
print(n.size())  # torch.Size([2, 2, 1, 2])

n = torch.squeeze(m, 2)
print(n.size())  # torch.Size([2, 1, 2, 1, 2])

n = torch.squeeze(m, 3)
print(n.size())  # torch.Size([2, 1, 2, 2])

控制台輸出:

5.cat()函數
torch.cat(inputs, dimension=0) → Tensor

cat是concatnate的意思:拼接,聯系在一起。

參數:

  • inputs (sequence of Tensors) – 可以是任意相同Tensor 類型的python 序列
  • dimension (intoptional) – 沿着此維連接張量序列

注意:輸入數據必須是序列,序列中數據是任意相同的shape的同類型tensor

按維數0拼接(豎着拼)

C = torch.cat( (A,B),0 )

按維數1拼接(橫着拼)

C = torch.cat( (A,B),1 )

案例:

import torch
A=torch.ones(2,3)    #2x3的張量(矩陣)
print("A:\n",A,"\nA.shape:\n",A.shape,"\n")

B=2*torch.ones(4,3)  #4x3的張量(矩陣)
print("B:\n",B,"\nB.shape:\n",B.shape,"\n")

C=torch.cat((A,B),0)  #按維數0(行)拼接
print("C:\n",C,"\nC.shape:\n",C.shape,"\n")

控制台輸出:

D:\softwaretools\anaconda\python.exe D:/pycharmprojects/hoteltest01/hoteltest01/testpy/test16.py
A:
 tensor([[1., 1., 1.],
        [1., 1., 1.]]) 
A.shape:
 torch.Size([2, 3]) 

B:
 tensor([[2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.]]) 
B.shape:
 torch.Size([4, 3]) 

C:
 tensor([[1., 1., 1.],
        [1., 1., 1.],
        [2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.]]) 
C.shape:
 torch.Size([6, 3]) 


Process finished with exit code 0

6.permute()函數

torch.Tensor.permute (Python method, in torch.Tensor)

作用:將tensor的維度換位。

permute是更靈活的transpose,可以靈活的對原數據的維度進行調換,而數據本身不變。

import torch
x = torch.randn(2,3,4)
print(x.size())
x_p = x.permute(1,0,2) # 將原來第1維變為0維,同理,0→1,2→2 print(x_p.size())
print(x_p.size())

控制台輸出:

參考文獻:
https://www.jianshu.com/p/b23367ec9097
https://blog.csdn.net/Jane_JXR/article/details/98341092
https://zhuanlan.zhihu.com/p/86763381
https://www.pytorchtutorial.com/docs/  (pytorch 中文官方文檔)
 
 

 


免責聲明!

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



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