Product of two vectors return a matrix

Hi,
I have v1 and v2 two vector:
v1=torch.tensor([1 ,0 ,0 ,0])
v2=torch.tensor([1, 0, 0, 0])
The resultat will be v3=v1xv2=[[1 0 ,0 ,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] ,v3 de size(4x4).
hawo I implement this in PyTorch.

Thanks

Hi Gues!

You are asking for the outer product of two vectors. pytorch has this
function under the name ger(). So try:

torch.ger (v1, v2)

Best.

K. Frank

result = v1[..., None] @ v2[None, ...]

I think you should reshape your tensor first.
**convert your tensor as shape of ** (4,1) and (1,4)


In [16]: a = torch.rand((4,1))                                                  

In [17]: b = torch.rand((4,1))                                                  

In [18]: b.T.mm(a)                                                              
Out[18]: tensor([[0.7928]])

In [19]: a.mm(b.T)                                                              
Out[19]: 
tensor([[0.2572, 0.3554, 0.0888, 0.1154],
        [0.2528, 0.3493, 0.0873, 0.1134],
        [0.4203, 0.5808, 0.1451, 0.1886],
        [0.0919, 0.1270, 0.0317, 0.0412]])

Thanks @Hong_Cheng
It works