Suppose the size of a is (128,) and the size of b is (128, 3, 64, 64). How to do broadcast multiplication with these two tensors?

# How to broadcast a 1D tensor with a 4D tensor?

**kelam_goutam**(Kelam Goutam) #2

I guess u need to convert tensor A to size (128,1) and then multiply with tensor B.

```
output = A. unsqueeze(1) * B
```

**671fc6655b8b9292a04b**(henrykuo) #3

@kelam_goutam Thanks for your reply. It works if the size of b is (128,3), i.e, b is 2-dimensional tensor, but doesnâ€™t work when the dimension of b is more than 2 dimensions.

**justusschock**(Justus Schock) #4

the most generic way would be to do something like this (which is not broadcasting, but explicit expanding):

```
output = a.expand_as(b)*b
```

Edit:

if you want to do it with broadcasting, you can simply do

```
output = a * b
```

if the shapes cannot be broadcasted, an error will be raised, but you will also receive this error if you use the explicit approach

**671fc6655b8b9292a04b**(henrykuo) #5

Thanks for your apply. I tried the solution but it does not work as I expected.

A related question. Suppose I have a = torch.tensor([1,2,3]), how to expand it to

tensor([[[ 1, 1, 1],

[ 1, 1, 1]],

```
[[ 2, 2, 2],
[ 2, 2, 2]],
[[ 3, 3, 3],
[ 3, 3, 3]]])
```

Thanks!

**kelam_goutam**(Kelam Goutam) #6

A crude way to solve your problem could be the extension of my previous solution.

```
output = A.unsqueeze(1).unsqueeze(2).unsqueeze(3) * B
```

This basically makes A a 4D tensor.

**colesbury**(colesbury) #8

These also work:

```
output = a.reshape(128, 1, 1, 1) * b
```

or

```
output = a.reshape(-1, 1, 1, 1) * b
```