lcelona
(Luigi)
February 9, 2017, 7:57pm
1
I am trying to sum two tensors with dimensions:
a: 10 x 49 x 1024
b: 10 x 1024
Using the following code:
a + b.unsqueeze(1)
But it seems to expect both inputs with equal dimensions resulting in a RuntimeError:
RuntimeError: inconsistent tensor size at home/soumith/local/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:601
Thanks
apaszke
(Adam Paszke)
February 9, 2017, 8:01pm
2
How do you want to add these matrices? They have different numbers of elements.
lcelona
(Luigi)
February 9, 2017, 8:14pm
3
Don’t hate me. In Tensorflow, the following code works:
a = tf.placeholder(tf.float32, [10,49,1024])
b = tf.placeholder(tf.float32, [10,1024])
b + tf.expand_dims(a, 1)
I’ll replace my code using the following:
c = a + b.unsqueeze(1).repeat(1,a.size(1),1)
Correct?
apaszke
(Adam Paszke)
February 9, 2017, 8:33pm
4
I’m not hating anyone! I was just asking, sorry if you understood it that way.
Also, I’ve just realized that I have misread your question!
This should do it for you:
c = a + b.unsqueeze(1).expand_as(a)
It’s better to use expand
in most cases, as it doesn’t allocate any new memory, while repeat
does.
apaszke
(Adam Paszke)
February 9, 2017, 8:33pm
5
But your code would work too.
lcelona
(Luigi)
February 9, 2017, 8:45pm
6
Don’t hate me refers to the comparison with “competitors”
Thank you for your help!