How to use the view () function in pytorch
This article mainly introduces the relevant knowledge of how to use the view () function in pytorch, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this article on how to use the view () function in pytorch. Let's take a look.
Common usage (manual adjustment of size)
View () is the equivalent of reshape and resize, reshaping Tensor.
Import torcha1 = torch.arange (0jue 16) print (A1) # tensor ([0Jet 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) a2 = a1.view (8,2) A3 = a1.view (2,8) A4 = a1.view (4,4) print (a2) # tensor ([[0Jue 1], # [2,3], # [4,5] # [6, 7], # [8, 9], # [10, 11], # [12, 13], # [14, 15]) print (A3) # tensor ([[0, 1, 2, 3, 4, 5, 6, 7], # [8, 9, 10, 11, 12, 13, 14, 15]) print (a4) # tensor ([[0, 1, 1] 2, 3], # [4, 5, 6, 7], # [8, 9, 10, 11], # [12, 13, 14, 15]) II. Special usage: parameter-1 (auto-adjust size)
A parameter in view is set to-1, which means that the number of elements on this dimension is automatically adjusted to ensure that the total number of elements remains the same.
V1 = torch.arange (0jue 16) print (v1) # tensor ([0Magne1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) v2 = v1.view (- 1, 16) vminute tensor ([[0dhore1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) v2 = v1.view (- 1) 8) v2#tensor ([0, 1, 2, 3, 4, 5, 6, 7], # [8, 9, 10, 11, 12, 13, 14, 15]) v2 = v1.view (- 1, 4) v2#tensor ([[0, 1, 2, 3], # [4, 5, 6, 7], # [8, 9, 10, 11], # [12, 13 14, 15]]) v2 = v1.view (- 1,2) v2#tensor ([0,1], # [2,3], # [4,5], # [6,7], # [8,9], # [10,11], # [12,13], # [14,15]) v3 = v1.view (4x4) -1) vested tensor ([[0], # [1], # [2], # [3], # [4], # [5], # [6], # [7], # [8], # [9], # [10], # [11] # [12], # [13], # [14], # [15]) this is the end of the article on how to use the view () function in pytorch Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the view () function in pytorch". If you want to learn more, you are welcome to follow the industry information channel.