I would like to add two tensors with one different dimmension.
For example:
x = torch.ones(4,5)
y = torch.ones(4,3,5)
In numpy I'm cappable of doing this with:
import numpy as np
x = np.ones((4,5))
y = np.ones((4,3,5))
y + x[:,None,:]
#Prints out
array([[[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.]],
[[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.]],
[[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.]],
[[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.],
[ 2., 2., 2., 2., 2.]]])
It has a shape of (4,3,5)
Is there any way to reproduce this on a nn.CMulTable()
? When I view x
tensor like this x:view(4,1,5)
it give me an error inconsistent tensor size
.
m = nn.CAddTable()
m:forward({y, x:view(4,1,5)})
Any ideas?
Use expandAs to obtain a 4x3x5 tensor:
m:forward({y, x:view(4,1,5):expandAs(y)})