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

Example Analysis of Pytorch Advanced Operation

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

Share

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

Editor to share with you the example analysis of the advanced operation of Pytorch, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. merging and splitting 1.cat splicing

Merge directly according to the specified dim dimensions, which requires that all dimensions need to be the same except those that need to be merged.

2.stack stacking

Example: create a tensor that is the same as a, and stack according to a dimension, and a new dimension will be generated in front of the stacked dimensions for selection. For example, if a new 2-D is generated, you can choose through 0Person1. The exact meaning depends on the actual problem.

For example, if the transcripts of the two classes are merged with stack, the new dimension can be selected by selecting 0 or 1, so as to achieve the purpose of selecting the class.

3. Split ① Split split by length

The first parameter can be a separate number a, which means that each split part has a piece of data; it can be a list-like object b, which means that the data is split into len (b) tensor according to the way in b.

② Chunk split by quantity

The first parameter passed in is to split it into several chunk, and then divide the original dimension by this number.

For example, in the following example, the original dimension is [2Magne32 chunk 8], and if the parameter 2 is passed in, it needs to be split into two, then the dimension of each of them becomes [1 record32 paramel 8].

Second, the basic operation 1. add , subtract , multiply and divide

Consistent with the one in numpy. You can also use methods such as torch.add.

two。 Matrix multiplication

Note that * is the multiplication of elements by elements, while matrix multiplication can be done in two ways: torch.matmul,@

If it is the multiplication of high-dimensional matrices, the matrix multiplication of the last two dimensions is actually calculated.

3. Power calculation

As in numpy, you can use * to calculate any power. In addition, .pow () can also be calculated.

Exponential and logarithmic calculations are also basically the same, and log is based on e by default.

4. Clamp

It is usually used to cut the gradient when the gradient is too large. By entering the maximum and minimum value, those in the target that exceed the maximum value are determined by the maximum value, and those below the minimum value by the minimum value.

Third, attribute statistics 1. Find the norm

One thing to note: if you seek the norm of which dimension, which dimension will be eliminated.

two。 Extreme value, summation, cumulative multiplication

3. Dim and keepdim

In many ways, dim can be set. If it is not set, it is to expand all the data and seek the overall situation.

Note that the shape of the dim,a here is [4jue 10]. When calculating the maximum value, if you set dim=1, that is, a column, which means that the dimension of the result needs to be a column, then the value of the whole row is calculated to find the maximum value, and finally a column is returned as the result.

4.topk and kthvalue

Topk parameter: K (the first k maximum), dim (returns the result in the dimension of dim)

This method defaults to the maximum values returned and returns their indexes.

Kthvalue parameter: K (the smallest value of k), dim

5. Comparison operation

Consistent with the one in Numpy. If you use the torch.eq method, you return the results of each corresponding position; if you use the torch.equal method, you return the results of the overall comparison.

6. High-order operation ① where

② gather

The number of index rows is less than or equal to the number of rows in the table. That is, since you want to use the index to find the contents of the table, you cannot exceed the index length of the table. When the index is passed into the gather method, it must be converted to the type of Long.

Examples are as follows:

The shape of the return value must be the shape of the index, because it is the value taken according to the index. The content of the return value comes from the input input, which is obtained according to the index.

The above is all the contents of the article "sample Analysis of Pytorch Advanced Operations". 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.

Share To

Development

Wechat

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

12
Report