Pytorch学习笔记14----torch中相关函数使用:view函数、max()函数、squeeze()函数
1.View函數(shù)
把原先tensor中的數(shù)據(jù)按照行優(yōu)先的順序排成一個一維的數(shù)據(jù)(這里應該是因為要求地址是連續(xù)存儲的),然后按照參數(shù)組合成其他維度的tensor。比如說是不管你原先的數(shù)據(jù)是[[[1,2,3],[4,5,6]]]還是[1,2,3,4,5,6],因為它們排成一維向量都是6個元素,所以只要view后面的參數(shù)一致,得到的結果都是一樣的。
小案例:
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))
控制臺輸出:
參數(shù)不可為空。參數(shù)中的-1就代表這個位置由其他位置的數(shù)字來推斷,只要在不致歧義的情況下,view參數(shù)就可以推斷出來,也就是人可以推斷出形狀的情況下,view函數(shù)也可以推斷出來。比如a tensor的數(shù)據(jù)個數(shù)是6個,如果view(1,-1),我們就可以根據(jù)tensor的元素個數(shù)推斷出-1代表6。而如果是view(-1,-1,2),人不知道怎么推斷,機器也不知道。還有一種情況是人可以推斷出來,但是機器推斷不出來的:view(-1,-1,6),人可以知道-1都代表1,但是機器不允許同時有兩個-1。
2.max函數(shù)
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] )# 只返回最大值的每個數(shù) print(torch.max(a,0)[1])#只返回最大值的每個索引
控制臺輸出:
D:softwaretoolsanacondapython.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函數(shù)用法總結:
torch.max(a)返回輸入tensora中所有元素的最大值
torch.max(a,0) 返回每一列中最大值的那個元素,且返回索引(返回最大元素在這一列的行索引)
torch.max(a,1) 返回每一行中最大值的那個元素,且返回其索引(返回最大元素在這一行的列索引)
torch.max()[0], 只返回最大值的每個數(shù)
troch.max()[1], 只返回最大值的每個索引
torch.max()[1].data 只返回variable中的數(shù)據(jù)部分(去掉Variable containing:)
torch.max()[1].data.numpy() 把數(shù)據(jù)轉(zhuǎn)化成numpy ndarry
torch.max()[1].data.numpy().squeeze() 把數(shù)據(jù)條目中維度為1 的刪除掉
torch.max(tensor1,tensor2) element-wise 比較tensor1 和tensor2 中的元素,返回較大的那個值
3.squeeze()函數(shù)
torch.unsqueeze(input, dim, out=None)
作用:擴展維度
返回一個新的張量,對輸入的既定位置插入維度 1
如果dim為負,則將會被轉(zhuǎn)化dim+input.dim()+1
參數(shù):
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:softwaretoolsanacondapython.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)。
參數(shù): 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()函數(shù)
torch.cat(inputs, dimension=0) → Tensor
cat是concatnate的意思:拼接,聯(lián)系在一起。
參數(shù):
inputs (sequence of Tensors) – 可以是任意相同Tensor 類型的python 序列
dimension (int,optional) – 沿著此維連接張量序列
注意:輸入數(shù)據(jù)必須是序列,序列中數(shù)據(jù)是任意相同的shape的同類型tensor
按維數(shù)0拼接(豎著拼)
C =torch.cat( (A,B),0 )
按維數(shù)1拼接(橫著拼)
C =torch.cat( (A,B),1 )
案例:
import torch
A=torch.ones(2,3) #2x3的張量(矩陣)
print("A:
",A,"
A.shape:
",A.shape,"
")
B=2*torch.ones(4,3) #4x3的張量(矩陣)
print("B:
",B,"
B.shape:
",B.shape,"
")
C=torch.cat((A,B),0) #按維數(shù)0(行)拼接
print("C:
",C,"
C.shape:
",C.shape,"
")
控制臺輸出:
D:softwaretoolsanacondapython.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()函數(shù)
torch.Tensor.permute (Python method, in torch.Tensor)
作用:將tensor的維度換位。
permute是更靈活的transpose,可以靈活的對原數(shù)據(jù)的維度進行調(diào)換,而數(shù)據(jù)本身不變。
import torch x = torch.randn(2,3,4) print(x.size()) x_p = x.permute(1,0,2) # 將原來第1維變?yōu)?維,同理,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 中文官方文檔)
總結
以上是生活随笔為你收集整理的Pytorch学习笔记14----torch中相关函数使用:view函数、max()函数、squeeze()函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HaaS UI小程序
- 下一篇: 「英雄」