ragas
(ragas)
September 20, 2017, 11:56am
1
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!
chenchr
September 20, 2017, 1:00pm
2
maybe unsqueeze
is what you want
ragas
(ragas)
September 20, 2017, 1:02pm
3
@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!
smth
September 20, 2017, 1:39pm
4
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
ragas
(ragas)
September 20, 2017, 2:13pm
5
Thanks @smth : With new version (0.2.0) I can run the code. Even with torch tensor object. This is awesome!
chenchr
September 21, 2017, 6:39am
6
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
ragas
(ragas)
September 22, 2017, 3:52pm
7
@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.