In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to splice matrices in pytorch", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to splice matrices in pytorch" this article.
1 squeeze (): removes dimensions with a size of 1, including rows and columns.
Squeeze () does not work when the dimension is greater than or equal to 2.
Line, example: > 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] column, example: > 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] unchanged, example: > > 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 .) = 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 function > t1=torch.FloatTensor (torch.randn (2)) > T1-1.9405 1.2009 0.0018 0.9463 0.4409-1.9017 [torch.FloatTensor of size 2x3] > > t2=torch.FloatTensor (torch.randn (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]
Add: max (), view (), squeeze (), unsqueeze () in pytorch
Check a lot of blogs seem to understand, and then write a few small examples, instantly clear.
1. Torch.max () import torch a=torch.randn (3) print ("a:", a) print ('max (a):', torch.max (a)) b=torch.randn (3) print ("b:", b) print ('max (bpen0):', torch.max (bpen0)) print ('max (bL1):', torch.max (bL1)
Output:
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 (bline 0): (tensor ([0.2765, 0.6477, 0.8259, 1.5334]), tensor ([0,2,2,0]))
Max (bjournal 1): (tensor ([1.5334, 0.2616, 0.8259]), tensor ([3,2,2]))
Max (a), for one-dimensional data, to find the maximum value.
Max (aPol 0), which calculates the maximum value of a column in the data and outputs the row number where the maximum value is located.
Max (aPol 1), which calculates the maximum value of a row in the data and outputs the column number where the maximum value is located.
Print ('max (bMage1):', torch.max (bMagne1) [1])
Output: output only the column number in which the row maximum is located
Max (bjournal 1): tensor ([3, 2, 2])
Torch.max (bPower1) [0], only returns each number of the maximum value
2. View ()
A.view (iQuery j) means to convert the original matrix into the form of I rows and j columns.
I =-1 means there is no limit to the number of rows, and 1 column is output.
A=torch.randn (3pr 4) print (a)
Output:
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 (- 1pl 1)
Print (b)
Output:
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 means there is no limit to the number of columns, output 1 line
B=a.view (1) print (b)
Output:
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 and j = 2 means there is no limit to the number of rows, output 2 columns
B=a.view (- 1) print (b)
Output:
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 means there is no limit to the number of rows, output 3 columns
I = 4, j = 3 means output 4 rows and 3 columns
B=a.view (- 1pr 3) print (b) b=a.view (4pr 3) print (b)
Output:
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]])
3.
1.torch.squeeze ()
Compression matrix, which I understand as dimensionality reduction.
A.squeeze (I) compress the first dimension. If this dimension is 1, then this dimension is optional and can be compressed.
Import torch a=torch.randn (1 and 3) print (a) b=a.squeeze (0) print (b) c=a.squeeze (1) print (c)
Output:
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]])
A matrix with three rows and four columns on a page
If dimension 0 is 1, it can be deleted by squeeze (0) and transformed into a matrix with three rows and four columns.
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])
If the first dimension is not 1, it cannot be compressed.
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) indicates that dimension I is set to 1
Operate on the matrix b compressed to 3 rows and 4 columns, and set the 0 dimension to 1
C=b.unsqueeze (0) print (c)
Output a matrix with three rows and four columns on one page
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]])
Set the first dimension to 1
C=b.unsqueeze (1) print (c)
Output a matrix of 3 pages, one row, and 4 columns
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]])
In addition, the squeeze and unsqueeze operations do not change the original matrix.
The above is all the content of the article "how to splice matrices in pytorch". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.