Let us assume that we want to multiply 2 tensors:
t_a = torch.randn(2, 3, 5, 5) # 2 batch, 3 channels, 5 rows, 5 cols
t_b = torch.tensor([0.26, 0.28, 0.45]) # 1D tensor
In the problem I am reading about they use unsqueeze and unsqueeze in-place before multiplying the tensors as such:
t_c = t_b.unsqueeze(-1).unsqueeze_(-1) # first expand dims
result = t_a * t_c # then multiply
I assume we are expanding the tensor t_b
to be the same in at least one dimension as t_a
, for the purpose of broadcasting. So t_c
should end up being what shape in the end?
What does t_b.unsqueeze(-1)
do? Where does it add a dimension?
Why are we using unsqueeze_(-1)
in-place here? Is this because the first unsqueeze creates a new object in memory and the second one is just modifying the second object instead of creating a third?
Thanks for the help!