# Broadcasting (in numpy or pytorch) [course22p2/nbs/01_matmul.ipynb at master · fastai/course22p2 · GitHub](https://github.com/fastai/course22p2/blob/master/nbs/01_matmul.ipynb) When operating on two arrays/tensors, Numpy/PyTorch compares their shapes element-wise. It starts with the **trailing dimensions**, and works its way forward. Two dimensions are **compatible** when: - they are equal, or - one of them is 1, in which case that dimension is broadcasted to make it the same size If it gets to a point where one tensor does not have an additional dimension but the other does, torch will insert a unit axis of 1 as the first axis of the tensor. ``` x = [100, 200, 300] y = [200, 300] # y will have a unit axis of 1 inserted y_broadcasted = [1, 200, 300] ``` Arrays do not need to have the same number of dimensions. For example, if you have a `256*256*3` array of RGB values, and you want to scale each color in the image by a different value, you can multiply the image by a one-dimensional array with 3 values. Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, shows that they are compatible: ``` Image (3d array): 256 x 256 x 3 Scale (1d array): 3 Result (3d array): 256 x 256 x 3 ``` The [numpy documentation](https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html#general-broadcasting-rules) includes several examples of what dimensions can and can not be broadcast together. ![](IMG_957667BE5CBF-1.jpeg) ![](IMG_B62047635C82-1.jpeg) --- Date: 20230806 Links to: Tags: References: * []()