In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to apply squeeze () and unsqueeze () in PyTorch". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to apply squeeze () and unsqueeze () in PyTorch.
1.torch.squeeze
The main use of squeeze is to compress or decompress the dimensions of the data.
First look at torch.squeeze () this function mainly compresses the dimension of the data, removing the dimension with dimension 1, such as a row or a column, and the number of a row with three columns (1) becomes (3) rows after removing the first dimension with one dimension. Squeeze (a) is to delete all dimensions that are 1s in a. Dimensions that are not 1 have no effect. A.squeeze (N) is to remove the dimension specified in a with a dimension of one. Another form is to remove a specified dimension with a fixed dimension of one from b=torch.squeeze (aheline N) a.
In other words:
Indicates that if the dimension value of the Arg dimension is 1, the dimension is removed, otherwise the tensor remains unchanged. (that is, if tensor.shape () [arg] = = 1, remove the dimension)
For example:
A tensor whose dimension is 2x1x2x1x2, you don't have to think about what it looks like, squeeze (0) is the same, squeeze (1) is to become 2x2x1x2. (0 is calculated from the leftmost dimension)
> x = torch.zeros (2,1,2,1,2) > > x.size () torch.Size ([2,1,2,1,2]) > > y = torch.squeeze (x) > y.size () torch.Size ([2,2,2]) > > y = torch.squeeze (x, 0) > y.size () torch.Size ([2,1,2,1,2]) > > y = torch.squeeze (x, 1) > > y.size () torch.Size ([2,2,1]) 2]) 2.torch.unsqueeze
Torch.unsqueeze () is a function that expands the data dimension. Add a dimension with a dimension of one to the specified location, for example, there are three rows of data (3), and add one dimension to the position of 0 to become a row and three columns (1J3). A.squeeze (N) is to specify the position N in a plus a dimension of 1. Another form is b=torch.squeeze (aMagol N) a, which specifies the position N in an and adds a dimension with a dimension of 1.
> x = torch.tensor ([1,2,3,4]) > > torch.unsqueeze (x0) tensor ([[1,2,3,4]]) > torch.unsqueeze (x1) tensor ([[1], [2], [3], [4]) 3. Examples
Give an example of using the above two functions and convolution:
From torchvision.transforms import ToTensorimport torch as tfrom torch import nnimport cv2import numpy as npimport cv2to_tensor = ToTensor () # load the image lena = cv2.imread ('lena.jpg', cv2.IMREAD_GRAYSCALE) cv2.imshow (' lena', lena) # input = to_tensor (lena) to convert ndarray to tensor, and automatically normalize [0255] to [0mem1]. Input = to_tensor (lena) .unsqueeze (0) # initialization convolution parameter kernel = t.ones (1,1,3,3) /-9kernel [:, 1,1] = 1conv = nn.Conv2d (1,1,3,1, padding=1, bias=False) conv.weight.data = kernel.view (1,1,3) 3) # output out = conv (input) out = out.squeeze (0) print (out.shape) out = out.unsqueeze (3) print (out.shape) out = out.squeeze (0) print (out.shape) out = out.detach (). Numpy () # zooms to 0 ~ maximum cv2.normalize (out, out, 1.0,0, cv2.NORM_INF) cv2.imshow ("lena-result", out) cv2.waitKey ()
The result is as follows:
At this point, I believe you have a deeper understanding of "how to apply squeeze () and unsqueeze () in PyTorch". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.