學習機器學習必然會學習到線性代數(shù)(里面的矩陣),因為不管是numpy還是tensor,又或者是torch,它們的數(shù)據(jù)結構和運算方式都類似于矩陣。然而有些函數(shù)我們是不太熟悉的,今天我們就來介紹一下pytorch矩陣拼接的函數(shù)——cat()和pytorch矩陣降維的函數(shù)——squeeze()吧。
1 squeeze(): 去除size為1的維度,包括行和列。
至于維度大于等于2時,squeeze()不起作用。
行、例:
>>> torch.rand(4, 1, 3)
(0 ,.,.) =
0.5391 0.8523 0.9260
(1 ,.,.) =
0.2507 0.9512 0.6578
(2 ,.,.) =
0.7302 0.3531 0.9442
(3 ,.,.) =
0.2689 0.4367 0.6610
[torch.FloatTensor of size 4x1x3]
>>> torch.rand(4, 1, 3).squeeze()
0.0801 0.4600 0.1799
0.0236 0.7137 0.6128
0.0242 0.3847 0.4546
0.9004 0.5018 0.4021
[torch.FloatTensor of size 4x3]
列、例:
>>> torch.rand(4, 3, 1)
(0 ,.,.) =
0.7013
0.9818
0.9723
(1 ,.,.) =
0.9902
0.8354
0.3864
(2 ,.,.) =
0.4620
0.0844
0.5707
(3 ,.,.) =
0.5722
0.2494
0.5815
[torch.FloatTensor of size 4x3x1]
>>> torch.rand(4, 3, 1).squeeze()
0.8784 0.6203 0.8213
0.7238 0.5447 0.8253
0.1719 0.7830 0.1046
0.0233 0.9771 0.2278
[torch.FloatTensor of size 4x3]
不變、例:
>>> torch.rand(4, 3, 2)
(0 ,.,.) =
0.6618 0.1678
0.3476 0.0329
0.1865 0.4349
(1 ,.,.) =
0.7588 0.8972
0.3339 0.8376
0.6289 0.9456
(2 ,.,.) =
0.1392 0.0320
0.0033 0.0187
0.8229 0.0005
(3 ,.,.) =
0.2327 0.6264
0.4810 0.6642
0.8625 0.6334
[torch.FloatTensor of size 4x3x2]
>>> torch.rand(4, 3, 2).squeeze()
(0 ,.,.) =
0.0593 0.8910
0.9779 0.1530
0.9210 0.2248
(1 ,.,.) =
0.7938 0.9362
0.1064 0.6630
0.9321 0.0453
(2 ,.,.) =
0.0189 0.9187
0.4458 0.9925
0.9928 0.7895
(3 ,.,.) =
0.5116 0.7253
0.0132 0.6673
0.9410 0.8159
[torch.FloatTensor of size 4x3x2]
2 cat函數(shù)
>>> t1=torch.FloatTensor(torch.randn(2,3))
>>> t1
-1.9405 1.2009 0.0018
0.9463 0.4409 -1.9017
[torch.FloatTensor of size 2x3]
>>> t2=torch.FloatTensor(torch.randn(2,2))
>>> t2
0.0942 0.1581
1.1621 1.2617
[torch.FloatTensor of size 2x2]
>>> torch.cat((t1, t2), 1)
-1.9405 1.2009 0.0018 0.0942 0.1581
0.9463 0.4409 -1.9017 1.1621 1.2617
[torch.FloatTensor of size 2x5]
補充:pytorch中 max()、view()、 squeeze()、 unsqueeze()
查了好多博客都似懂非懂,后來寫了幾個小例子,瞬間一目了然。
一、torch.max()
import torch
a=torch.randn(3)
print("a:
",a)
print('max(a):',torch.max(a))
b=torch.randn(3,4)
print("b:
",b)
print('max(b,0):',torch.max(b,0))
print('max(b,1):',torch.max(b,1))
輸出:
a:
tensor([ 0.9558, 1.1242, 1.9503])
max(a): tensor(1.9503)
b:
tensor([[ 0.2765, 0.0726, -0.7753, 1.5334],
[ 0.0201, -0.0005, 0.2616, -1.1912],
[-0.6225, 0.6477, 0.8259, 0.3526]])
max(b,0): (tensor([ 0.2765, 0.6477, 0.8259, 1.5334]), tensor([ 0, 2, 2, 0]))
max(b,1): (tensor([ 1.5334, 0.2616, 0.8259]), tensor([ 3, 2, 2]))
max(a),用于一維數(shù)據(jù),求出最大值。
max(a,0),計算出數(shù)據(jù)中一列的最大值,并輸出最大值所在的行號。
max(a,1),計算出數(shù)據(jù)中一行的最大值,并輸出最大值所在的列號。
print('max(b,1):',torch.max(b,1)[1])
輸出:只輸出行最大值所在的列號
max(b,1): tensor([ 3, 2, 2])
torch.max(b,1)[0], 只返回最大值的每個數(shù)
二、view()
a.view(i,j)表示將原矩陣轉化為i行j列的形式
i為-1表示不限制行數(shù),輸出1列
a=torch.randn(3,4)
print(a)
輸出:
tensor([[-0.8146, -0.6592, 1.5100, 0.7615],
[ 1.3021, 1.8362, -0.3590, 0.3028],
[ 0.0848, 0.7700, 1.0572, 0.6383]])
b=a.view(-1,1)
print(b)
輸出:
tensor([[-0.8146],
[-0.6592],
[ 1.5100],
[ 0.7615],
[ 1.3021],
[ 1.8362],
[-0.3590],
[ 0.3028],
[ 0.0848],
[ 0.7700],
[ 1.0572],
[ 0.6383]])
i為1,j為-1表示不限制列數(shù),輸出1行
b=a.view(1,-1)
print(b)
輸出:
tensor([[-0.8146, -0.6592, 1.5100, 0.7615, 1.3021, 1.8362, -0.3590,
0.3028, 0.0848, 0.7700, 1.0572, 0.6383]])
i為-1,j為2表示不限制行數(shù),輸出2列
b=a.view(-1,2)
print(b)
輸出:
tensor([[-0.8146, -0.6592],
[ 1.5100, 0.7615],
[ 1.3021, 1.8362],
[-0.3590, 0.3028],
[ 0.0848, 0.7700],
[ 1.0572, 0.6383]])
i為-1,j為3表示不限制行數(shù),輸出3列
i為4,j為3表示輸出4行3列
b=a.view(-1,3)
print(b)
b=a.view(4,3)
print(b)
輸出:
tensor([[-0.8146, -0.6592, 1.5100],
[ 0.7615, 1.3021, 1.8362],
[-0.3590, 0.3028, 0.0848],
[ 0.7700, 1.0572, 0.6383]])
tensor([[-0.8146, -0.6592, 1.5100],
[ 0.7615, 1.3021, 1.8362],
[-0.3590, 0.3028, 0.0848],
[ 0.7700, 1.0572, 0.6383]])
三、
1.torch.squeeze()
壓縮矩陣,我理解為降維
a.squeeze(i) 壓縮第i維,如果這一維維數(shù)是1,則這一維可有可無,便可以壓縮
import torch
a=torch.randn(1,3,4)
print(a)
b=a.squeeze(0)
print(b)
c=a.squeeze(1)
print(c
輸出:
tensor([[[ 0.4627, 1.6447, 0.1320, 2.0946],
[-0.0080, 0.1794, 1.1898, -1.2525],
[ 0.8281, -0.8166, 1.8846, 0.9008]]])
一頁三行4列的矩陣
第0維為1,則可以通過squeeze(0)刪掉,轉化為三行4列的矩陣
tensor([[ 0.4627, 1.6447, 0.1320, 2.0946],
[-0.0080, 0.1794, 1.1898, -1.2525],
[ 0.8281, -0.8166, 1.8846, 0.9008]])
第1維不為1,則不可以壓縮
tensor([[[ 0.4627, 1.6447, 0.1320, 2.0946],
[-0.0080, 0.1794, 1.1898, -1.2525],
[ 0.8281, -0.8166, 1.8846, 0.9008]]])
2.torch.unsqueeze()
unsqueeze(i) 表示將第i維設置為1
對壓縮為3行4列后的矩陣b進行操作,將第0維設置為1
c=b.unsqueeze(0)
print(c)
輸出一個一頁三行四列的矩陣
tensor([[[ 0.0661, -0.2386, -0.6610, 1.5774],
[ 1.2210, -0.1084, -0.1166, -0.2379],
[-1.0012, -0.4363, 1.0057, -1.5180]]])
將第一維設置為1
c=b.unsqueeze(1)
print(c)
輸出一個3頁,一行,4列的矩陣
tensor([[[-1.0067, -1.1477, -0.3213, -1.0633]],
[[-2.3976, 0.9857, -0.3462, -0.3648]],
[[ 1.1012, -0.4659, -0.0858, 1.6631]]])
另外,squeeze、unsqueeze操作不改變原矩陣。
以上就是pytorch矩陣拼接和矩陣降維的全部內容了,希望能給大家一個參考,也希望大家多多支持W3Cschool。