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

Introduction to dim Operation in Pytorch

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "introduction to dim Operation in Pytorch". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Many operations on tensor in Pytorch, such as sum, argmax, and so on, can set the dim parameter to specify the dimension in which the operation is performed. Dim in Pytorch is similar to axis in numpy. This article summarizes the dim operations in Pytorch.

The relationship between dim and square brackets

Create a matrix

A = torch.tensor ([[1,2], [3,4]]) print (a)

Output

Tensor ([[1,2], [3,4]])

Because an is a matrix, there are two parentheses on the left side of a

There is a nested relationship between parentheses, representing different dimensions. Counting from left to right, the dimensions represented by the two parentheses are 0 and 1, respectively, traversing the vector in the 0th dimension and the scalar in the 1st dimension.

Similarly, for 3D tensor

B = torch.tensor ([3,2], [1,4]], [[5,6], [7,8]) print (b)

Output

Tensor ([3,2], [1,4]], [[5,6], [7,8])

The dimensions represented by the three parentheses are 0,1,2 from left to right, respectively. The matrix is traversed in the 0th dimension, the vector is obtained in the 1st dimension, and the scalar is obtained in the 2nd dimension.

A little more detail.

Operate on a specified dimension

To sum (or do something else) in a dimension is to sum the elements in that dimension.

For matrix a

A = torch.tensor ([[1,2], [3,4]]) print (a)

Output

Tensor ([[1,2], [3,4]])

Find the sum of an in dimension 0, because dimension 0 represents the outermost parenthesis, the elements in parentheses are vectors [1, 2], [3, 4], and the sum of dimension 0 is the addition of elements in dimension 0, that is, the sum of two vectors [1, 2], [3, 4], so the result is

S = torch.sum (a, dim=0) print (s)

Output

Tensor ([4,6])

As you can see, an is a two-dimensional matrix, and the result of addition is an one-dimensional vector. The parameter keepdim=True can be used to ensure that the shape remains the same.

S = torch.sum (a, dim=0, keepdim=True) print (s)

Output

Tensor ([[4,6]])

The summation in dimension 0 of an is the addition of the elements (vectors) in dimension 0. Similarly, the summation of the first dimension of an is the addition of the elements (scalars) in the first dimension of a, and the first dimensional elements of an are scalar 1meme2 and 3prime4, then the result is

S = torch.sum (a, dim=1) print (s)

Output

Tensor ([3,7])

Keep the dimension unchanged

S = torch.sum (a, dim=1, keepdim=True) print (s)

Output

Tensor ([[3], [7]])

The same is true for 3D tensor operations.

B = torch.tensor ([3,2], [1,4]], [[5,6], [7,8]) print (b)

Output

Tensor ([3,2], [1,4]], [[5,6], [7,8])

Add b in dimension 0, which is the outermost parenthesis, and the elements in the outermost parentheses are matrices [[3,2], [1,4] and [[5,6], [7,8]]. To sum in dimension 0 is to add the elements (matrix) in dimension 0.

S = torch.sum (b, dim=0) print (s)

Output

Tensor ([[8,8], [8,12]])

To find the sum of b in dimension 1 is to add the elements [3, 2] and [1, 4], [5, 6] and [7, 8] in dimension 1, so

S = torch.sum (b, dim=1) print (s)

Output

Tensor ([[4,6], [12,14]])

Then the sum in the second dimension of b is the sum of scalars 3 and 2, 1 and 4, 5 and 6, 7 and 8.

S = torch.sum (b, dim=2) print (s)

The result is

Tensor ([[5,5], [11,15])

In addition to summation, other operations are similar, such as finding the maximum value of b in a specified dimension

M = torch.max (b, dim=0) print (m)

The maximum value of b in dimension 0 is the maximum value of the elements in dimension 0 (two matrices [[3, 2], [1, 4] and [[5, 6], [7, 8]]). Take the maximum value of the corresponding position of the matrix.

The result is

Torch.return_types.max (values=tensor ([[5,6], [7,8]), indices=tensor ([[1,1], [1,1]]))

The maximum value of b in the 1st dimension is the maximum value of the 1st dimensional element (4 (2 pairs) vectors).

M = torch.max (b, dim=1) print (m)

Output as

Torch.return_types.max (values=tensor ([[3,4], [7,8]), indices=tensor ([[0,1], [1,1])

The maximum value of b in dimension 0 is the maximum value of the element (8 (4 pairs) scalars).

M = torch.max (b, dim=2) print (m)

Output

Torch.return_types.max (values=tensor ([[3,4], [6,8])), indices=tensor ([[0,1], [1,1]]) summary

The specified dimension operation in tensor is to operate on the elements contained in the specified dimension. If you want to keep the resulting dimension unchanged, you can set the parameter keepdim=True.

This is the end of the introduction to dim Operation in Pytorch. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

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

12
Report