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 basic Operation of Tensor in Pytorch

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of the basic operation of Tensor in Pytorch. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

First, the establishment of tensor 1. Use tensor

The lowercase tensor receives the specific data and can enter the array directly in the way of numpy.

two。 Use Tensor

Uppercase letters can receive shapes, and random numbers are generated, but there are no rules. Different types can be generated.

Automatically generated data has a default type, FloatTensor, which can be modified using torch.set_default_tensor_type

3. Random initialization

Uniform sampling between rand:0-1

Randn: random sampling of normal distribution with mean 0 and variance 1

The above two kinds can input the shape directly, and then produce the random number of the corresponding shape.

Randint: parameters are-start value, end value, shape

At the same time, each one will have a _ like method, and enter a tensor to generate a new tensor for the corresponding shape.

4. Other data generate ① torch.full

The parameters are-shape, data. If the position of the shape is given to an empty list, a scalar is generated.

② torch.arange

The parameter is-start, end, step size. Does not contain a termination value.

③ linspace and logspace

The parameter is-start, stop, quantity. Include termination valu

④ ones, zeros, eye

Just enter the shape. Ones and zeros also have a _ like method

⑤ torch.randperm

Break up at random. Enter a number, will automatically generate the length of this number, starting from 0 random arrangement of numbers, can be used as an index. It is very useful when you need to use the same index for different data and disrupt the order.

Second, the index and slice of tensor. How to use index and slice

Consistent with the use of index slices in python

① index_select

The parameter entered-the location of the dimension and the index of the dimension corresponding to the content to be selected. It is difficult to understand, you can take a look at the following example.

②...

The three dots are equivalent to integrated consecutive colons.

③ mask

You need to generate a mask based on the data, such as picking out a number greater than or equal to 0.3. at this time, you will get a mask with the same shape as the data. The position that meets the condition is 1, and the position that does not meet the condition is 0. You can select the data through masked_select.

Third, the transformation of tensor dimension 1. Dimension transformation ① torch.view

Same as the reshape method in numpy. The actual physical meaning needs to be taken into account in the conversion.

② squeeze/unsqueeze

The parameter entered by squeeze-the location of the dimension to be reduced

The parameter entered by unsqueeze-the location of the dimension to be added

③ expand,repeat

The parameter entered by expand-the dimension that you want to expand to form

The parameters entered by repeat-the number of times each dimension needs to be repeated

Expand is usually used because data is not actively replicated.

④ t,transpose,permute

T: like transpose in numpy, only for two-dimensional matrix operations

Transpose: enter the location of the dimension to be exchanged. But when you want to restore, you need to remember the physical meaning of each location after conversion, and convert it again according to the actual meaning. See the example below.

Permute: enter the location index of the dimension you want to convert. It is equivalent to using transpose many times

This is the end of the article on "sample Analysis of the basic Operation of Tensor in Pytorch". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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