Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the view () function in pytorch

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

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: 214

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report