# Numpy implementation in Pytorch

Hi,

In numpy, we use `x[:, None]`. This is frequently used to elementwise matrix multiplication.

E.g.

``````x = np.array([1.8507 ,-2.7324])
y = np.array([0.9722 , 0.4470 , 1.0000 , 0.0000 , 0.0000 , 0.0000])
``````

If we do, `x * y`, we will get the error:

ValueError: operands could not be broadcast together with shapes (2,) (6,)

But `x * y[:,None]`, will give us

``````array([[ 1.79925054, -2.65643928],
[ 0.8272629 , -1.2213828 ],
[ 1.8507    , -2.7324    ],
[ 0.        , -0.        ],
[ 0.        , -0.        ],
[ 0.        , -0.        ]])
``````

Is there pytorch implementation of this kind of functionality?

Thank you!

maybe `unsqueeze` is what you want

@chenchr: Thanks for your suggestion. I tried unsqueeze. But I could not get the desired answer. Will it be possible for you to show me the code, how to get the final result I’m getting using numpy?

Thank you!

There was a bug when doing `[:, None]` indexing in PyTorch.
It was fixed 2 days ago in https://github.com/pytorch/pytorch/pull/2779

If we compile with latest master, we get the same result in PyTorch too:

``````import numpy as np
x = np.array([1.8507 ,-2.7324])
y = np.array([0.9722 , 0.4470 , 1.0000 , 0.0000 , 0.0000 , 0.0000])
print(x * y[:, None])
# array([[ 1.79925054, -2.65643928],
#       [ 0.8272629 , -1.2213828 ],
#       [ 1.8507    , -2.7324    ],
#       [ 0.        , -0.        ],
#       [ 0.        , -0.        ],
#       [ 0.        , -0.        ]])

print(torch.from_numpy(x) * torch.from_numpy(y)[:, None])

# 1.7993 -2.6564
# 0.8273 -1.2214
# 1.8507 -2.7324
# 0.0000 -0.0000
# 0.0000 -0.0000
# 0.0000 -0.0000
#[torch.DoubleTensor of size 6x2]
``````
1 Like

Thanks @smth: With new version (0.2.0) I can run the code. Even with torch tensor object. This is awesome!

``````x = np.array([1.8507 ,-2.7324])
y = np.array([0.9722 , 0.4470 , 1.0000 , 0.0000 , 0.0000 , 0.0000])
x = torch.from_numpy(x)
y = torch.from_numpy(y)
z = x.unsqueeze(0)*y.unsqueeze(1)
print(z)
``````

or

``````x = np.array([1.8507 ,-2.7324])
y = np.array([0.9722 , 0.4470 , 1.0000 , 0.0000 , 0.0000 , 0.0000])
x = torch.from_numpy(x)
y = torch.from_numpy(y)
z = x*y.unsqueeze(1)
print(z)
``````

both work on my computer. I prefer the former as it explicit specifies the tensor shape.

1 Like

@chenchr: Thanks for your suggestion. I could solve the problem using Smith’s method. But this alternative solution also helped me to understand performance of “unsqueeze” better. I’m still new to pytorch. So taking sometime to grab the new concepts.