1.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))
控制台輸出:
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 中的元素,返回較大的那個值
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])
控制台輸出:
torch.cat(inputs, dimension=0) → Tensor
cat是concatnate的意思:拼接,聯系在一起。
參數:
- inputs (sequence of Tensors) – 可以是任意相同Tensor 類型的python 序列
- dimension (int, optional) – 沿着此維連接張量序列
注意:輸入數據必須是序列,序列中數據是任意相同的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())
控制台輸出: