torch.add(torch.ones(4,1), torch.randn(4))
produces a Tensor with size: torch.Size([4,4])
.
Can someone provide a logic behind this?
torch.add(torch.ones(4,1), torch.randn(4))
produces a Tensor with size: torch.Size([4,4])
.
Can someone provide a logic behind this?
PyTorch
broadcasting
is based on numpy broadcasting semantics which can be understood by readingnumpy broadcasting rules
or PyTorch broadcasting guide. Expounding the concept with an example would be intuitive to understand it better. So, please see the example below:Now for
torch.add(t_rand, t_ones)
, visualize it like:which should give the output with tensor of shape
(4,3)
as:Also, note that we get exactly the same result even if we pass the arguments in a reverse order as compared to the previous one:
Anyway, I prefer the former way of understanding for more straightforward intuitiveness.
For pictorial understanding, I culled out more examples which are enumerated below:
Example-1:
Example-2:
:T
andF
respectively stand forTrue
andFalse
and indicate along which dimensions we allow broadcasting (source: Theano).Example-3:
Here are some shapes where array
b
is broadcasted appropriately to match the shape of arraya
.