I don't understand broadcasting. The documentation explains the rules of broadcasting but doesn't seem to define it in English. My guess is that broadcasting is when NumPy fills a smaller dimensional array with dummy data in order to perform an operation. But this doesn't work:
>>> x = np.array([1,3,5])
>>> y = np.array([2,4])
>>> x+y
*** ValueError: operands could not be broadcast together with shapes (3,) (2,)
The error message hints that I'm on the right track, though. Can someone define broadcasting and then provide some simple examples of when it works and when it doesn't?
1.What is Broadcasting? Broadcasting is a Tensor operation. Helpful in Neural Network (ML, AI)
2.What is the use of Broadcasting?
Without Broadcasting addition of only identical Dimension(shape) Tensors is supported.
Broadcasting Provide us the Flexibility to add two Tensors of Different Dimension.
for Example: adding a 2D Tensor with a 1D Tensor is not possible without broadcasting see the image explaining Broadcasting pictorially
Run the Python example code understand the concept
You are almost correct about smaller Tensor, no ambiguity, the smaller tensor will be broadcasted to match the shape of the larger tensor.(Small vector is repeated but not filled with Dummy Data or Zeros to Match the Shape of larger).
3. How broadcasting happens? Broadcasting consists of two steps:
1 Broadcast axes are added to the smaller tensor to match the ndim of the larger tensor.
2 The smaller tensor is repeated alongside these new axes to match the full shape of the larger tensor.
4. Why Broadcasting not happening in your code? your code is working but Broadcasting can not happen here because both Tensors are different in shape but Identical in Dimensional(1D). Broadcasting occurs when dimensions are nonidentical. what you need to do is change Dimension of one of the Tensor, you will experience Broadcasting.
5. Going in Depth. Broadcasting(repetition of smaller Tensor) occurs along broadcast axes but since both the Tensors are 1 Dimensional there is no broadcast Axis. Don't Confuse Tensor Dimension with the shape of tensor, Tensor Dimensions are not same as Matrices Dimension.
The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations.
It's basically a way numpy can expand the domain of operations over arrays.
The only requirement for broadcasting is a way aligning array dimensions such that either:
So, for example if:
You could not align x and y like so:
But you could like so:
How would an operation like this result?
Suppose we have:
The operation
x + y
would result in:I hope you caught the drift. If you did not, you can always check the official documentation here.
Cheers!
Broadcasting is numpy trying to be smart when you tell it to perform an operation on arrays that aren't the same dimension. For example:
Here it decided you wanted to apply the operation using the lower dimensional array (0-D) on each item in the higher-dimensional array (1-D).
You can also add a 0-D array (scalar) or 1-D array to a 2-D array. In the first case, you just add the scalar to all items in the 2-D array, as before. In the second case, numpy will add row-wise:
There are ways to tell numpy to apply the operation along a different axis as well. This can be taken even further with applying an operation between a 3-D array and a 1-D, 2-D, or 0-D array.
Broadcasting is how numpy do math operations with array of different shapes. Shapes are the format the array has, for example the array you used, x , has 3 elements of 1 dimension; y has 2 elements and 1 dimension.
To perform broadcasting there are 2 rules: 1) Array have the same dimensions(shape) or 2)The dimension that doesn't match equals one.
for example x has shape(2,3) [or 2 lines and 3 columns]; y has shape(2,1) [or 2 lines and 1 column]
Can you add them? x + y? Answer: Yes, because the mismatched dimension is equal to 1 (the column in y). If y had shape(2,4) broadcasting would not be possible, because the mismatched dimension is not 1.
In the case you posted: operands could not be broadcast together with shapes (3,) (2,); it is because 3 and 2 mismatched altough both have 1 line.