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

What is the broadcast semantics in pytorch

2025-01-19 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 "what is the broadcast semantics in pytorch". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the broadcast semantics in pytorch" can help you solve the problem.

1. What is broadcast semantics?

There is an explanation in the official document:

In short, if a PyTorch operation supports broadcast, then its Tensor arguments can be automatically expanded to be of equal sizes (without making copies of the data).

To put it simply, if a pytorch operation supports broadcasting, its Tensor parameters can be automatically extended to the same size (there is no need to copy data).

According to my understanding, it should refer to the calculation process of the algorithm, different Tensor if the size is different, but in line with certain rules, then you can automatically expand the dimensions to achieve Tensor calculation. In the process of dimension expansion, it is not true to copy the small dimension Tensor to the same as the large dimension Tensor, because this is a waste of memory.

2. Rules of broadcast semantics

First of all, let's take a look at the standard. If the size of two Tensor is the same, it can be calculated directly:

X = torch.empty ((4,2,3)) y = torch.empty ((4,2,3)) print ((xonomy) .size ())

Output:

Torch.Size ([4,2,3])

However, if the dimensions of the two Tensor are not the same, the pytorch can also be calculated according to the following two rules:

(1) Each tensor has at least one dimension.

(2) When iterating over the dimension sizes, starting at the trailing dimension, the dimension sizes must either be equal, one of them is 1, or one of them does not exist.

Each Tensor has at least one dimension.

When iterating dimensions, start with the following dimensions

The first rule requires each Tensor participating in the calculation to have at least one dimension, and the second rule means that when the dimension iterates, starting from the last dimension, there can be three situations:

Equal dimensions

One of the dimensions is 1.

One of the dimensions does not exist.

3. Example x = torch.empty ((0,)) y = torch.empty ((2,3)) print ((x + y) .size ())

Output:

RuntimeError: The size of tensor a (0) must match the size of tensor b (3) at non-singleton dimension 1

Here, the first rule "each Tensor participating in the calculation has at least one dimension" is not satisfied.

X = torch.empty (5,2,4,1) y = torch.empty (3,1,1) print ((x + y) .size ())

Output:

RuntimeError: The size of tensor a (2) must match

The size of tensor b (3) at non-singleton dimension 1

Here, the second rule is not satisfied, because in the process of iterating from the last dimension, the penultimate dimension: X is 2 and y is 3. This does not conform to the three cases of the second rule, so broadcast semantics cannot be used.

4. An example of broadcast semantics x = torch.empty (5,3,4,1) y = torch.empty (3,1,1) print ((x + y) .size ())

Output:

Torch.Size ([5,3,4,1])

X is four-dimensional and y is three-dimensional, iterating from the last dimension:

The last dimension: X is 1, and y is 1, which satisfies Rule 2.

The penultimate dimension: X is 4, and y is 1, which satisfies rule 2.

The penultimate dimension: X is 3, and y is 3, which satisfies rule 1

The penultimate dimension: X is 5, and y is 0, which satisfies rule one.

This is the end of the content about "what is the broadcast semantics in pytorch". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

*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