# Broadcasting - Simple

``````x = torch.tensor([[1, 2, 3], [4, 5, 6]])    # x has shape (2, 3)  to convert to (1 ,2, 3,)
c = torch.tensor([1, 10, 11, 100])        # c has shape (4,)   to convert to  (4, 1, 1)

# x * y will have shape of (4, 2, 3)
y = c.view(-1, 1, 1)
``````

so when we have (2, 3 ) for x and ( 4, ) for constant
first, we extend (4) to (4,1)

but

x shape (2,3)
y shape (4,1)

will not work because of broadcasting semantics

hence, we need to

(1, 2, 3)
(4, 1 ,1)

it can’t be this
( 1 at the rear instead at the front for x is because it will fail the broadcast semantics? )

( 2 , 3 , 1 ) and
( 4, 1 , 1 )

Am I correct up to here?
Is there an easier way to work on broadcasting?

Thanks.

Hi,

If I understand correctly you want to do this:

``````x_expanded = x.expand(1, -1, -1)
x_expanded * y
``````

and it will give you a the broadcasted `(4,2,3)` Tensor.

But I thought we need to convert them to

x has shape of (2, 3) to convert to (1 ,2, 3,)
c which is a constant has shape of (4,) to convert to (4, 1, 1)

and we get by

``````y = c.view(-1, 1, 1)

tensor([[[  1,   2,   3],
[  4,   5,   6]],

[[ 10,  20,  30],
[ 40,  50,  60]],

[[ 11,  22,  33],
[ 44,  55,  66]],

[[100, 200, 300],
[400, 500, 600]]])
``````