Sum of matrices with different dimensions

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


How do you want to add these matrices? They have different numbers of elements.

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)


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.

But your code would work too.

Don’t hate me refers to the comparison with “competitors” :slight_smile:

Thank you for your help!