Broadcast mechanism_torch numpy

reference blog post

Some key points to remember
1. When is the broadcast mechanism generated? (A necessary condition for the broadcast mechanism to occur)
When adding and subtracting two arrays of different shapes in numpy, broadcasting will occur when adding two elements. There will also be a broadcasting mechanism when multiplying.
Example:
insert image description here

2. How to judge whether the broadcast is compatible:
when the dimensions of the two calculation arrays are the same: it is necessary to compare the axis lengths of each axis from the back to the front. If they are the same or the length of a certain axis is 1, the broadcasting compatibility can be processed by the broadcasting mechanism Perform broadcast calculation to get the result;
if the dimensions of the two arrays are different: Then extend the low-dimensional array to one dimension, the axis length of the extended dimension is 1, and then perform broadcast mechanism processing on the expanded dimension.

In addition: there are also examples of broadcast mechanism addition broadcast in torch

Guess you like

Origin blog.csdn.net/m0_45290027/article/details/127200047