Matrices addition - inconsistent tensor size error

I’m transferring a matrices addition code from Tensorflow to Pytorch. The matrices shapes are (1,25,256) and (1,1,256). On Tensorflow it works but on Pytorch I get the error:

RuntimeError: inconsistent tensor size

I know that the shapes doesn’t match but this code works on Tensorflow and I wonder if it can work on Pytorch as well.

Seems to work for me:

In [1]: import torch

In [2]: x = torch.randn(1, 25, 256)

In [3]: y = torch.randn(1, 1, 256)

In [4]: x + y
Out[4]:
tensor([[[ 1.4846, -1.2704, -1.5809,  ...,  0.9014, -0.8867,  0.8496],
         [ 4.1737,  2.4117, -0.0261,  ...,  0.0534, -0.5655,  0.9513],
         [ 0.3474,  0.2071, -3.2485,  ...,  0.4155, -0.6435,  1.7778],
         ...,
         [ 2.6008,  0.7829, -2.2995,  ...,  1.4929, -0.8345,  1.1157],
         [ 2.9556,  1.7386, -0.8274,  ...,  1.0852, -0.4116,  2.2215],
         [ 2.6638,  0.6301, -3.5564,  ...,  0.7788,  1.2027,  2.0694]]])

what version of pytorch are you on? Could you provide a code snippet?

1 Like

Maybe it’s because I’m using Pytorch 0.1.12? (I have to use this version)

Yeah, I tested it with 0.4 and it works. The question now is, how to make it work with 0.1.12?

0.1.12 doesn’t have broadcasting, so you have to use expand/expand_as.
In Richard’s example x + y.expand_as(x).

Best regards

Thomas

1 Like