Is there a simple way to compute outer products?

I am looking for something that works like

(x=[1,2], y=[1,3])

torch.outer(x,y) = [1,3;2,6]

Perfect, just what i needed. Thanks!

Is there a way to do this in batch mode? So if I have two batches of vectors, is there an easy way to compute the (batch) outer products?

Assuming you have two dimensions, you could use

`u.unsqueeze(2)*v.unsqueeze(1)`

(with latest master) or with explicit expansion for torch <= 0.1.12

```
b = u.size(0)
m = u.size(1)
n = v.size(1)
u.unsqueeze(2).expand(b,m,n)*v.unsqueeze(1).expand(b,m,n)
```

Best regards

Thomas

@Cysu torch.ger works for 2 rank 1 tensors. Is there any inbuilt torch function for calculating outer product for any number of rank 1 tensors and not just limited to 2 tensors?

einsum.

Best regards

Thomas

For two 2D tensors `a`

and `b`

(of size `[b,n]`

and `[b,m]`

respectively),

`a[:, :, None] @ b[:, None, :]`

(of size `[b,n,m]`

) gives the outer product operated on each item in the batch.

It’s easy to extend this to higher dimensions, for example, for two 3D tensors `a`

and `b`

(of size `[b,n,m]`

and `[b,n,k]`

),

`a[:, :, :, None] @ b[:,:, None, :]`

gives the desired outer product of size `[b,n,m,k]`

Hope this helps!