Division of tensors that don't have the same size

When you divide a tensor of size (64,128,32,32) by a tensor of size (64,1,32,32) you get an error because the size must match, yet in situations like these expand() is implicit and numpy does it this way. torch’s div() doesn’t operate that way, am I missing something or is this intentional?

The solution is of course expand_as() but the fact that it’s not default leads me to believe that perhaps I missed something.

1 Like

We want to add broadcasting, but didn’t have time to implement that yet.

Hi, @apaszke, Is there any schedule for that function? I am also looking forwards to that.

if you are still wondering, it’s been implemented but it should be done properly.


>>> a = torch.rand(64, 20)
>>> b = torch.rand(64, 1)
>>> a/b
tensor([[ 5.0057e-01,  3.5622e-01,  3.1053e-01,  ...,  3.2856e-01,
          1.0888e+00,  9.7678e-01],
        [ 4.7883e+00,  5.7695e+00,  2.8125e+00,  ...,  1.6500e+01,
          3.5257e+00,  1.5637e+01],
        [ 6.8112e-01,  1.8881e+00,  2.0702e+00,  ...,  4.2512e-01,
          1.4803e+00,  8.5795e-01],
        ...,
        [ 4.1682e-01,  9.6458e-01,  1.1828e+00,  ...,  9.9901e-01,
          1.0716e+00,  1.4875e+00],
        [ 1.2503e-01,  1.2347e+00,  6.0802e-01,  ...,  5.0439e-01,
          1.2536e+00,  1.3501e+00],
        [ 9.1765e-01,  6.7741e-01,  1.0928e+00,  ...,  8.1460e-01,
          9.7924e-01,  3.8059e-01]])
>>> torch.div(a,b)
tensor([[ 5.0057e-01,  3.5622e-01,  3.1053e-01,  ...,  3.2856e-01,
          1.0888e+00,  9.7678e-01],
        [ 4.7883e+00,  5.7695e+00,  2.8125e+00,  ...,  1.6500e+01,
          3.5257e+00,  1.5637e+01],
        [ 6.8112e-01,  1.8881e+00,  2.0702e+00,  ...,  4.2512e-01,
          1.4803e+00,  8.5795e-01],
        ...,
        [ 4.1682e-01,  9.6458e-01,  1.1828e+00,  ...,  9.9901e-01,
          1.0716e+00,  1.4875e+00],
        [ 1.2503e-01,  1.2347e+00,  6.0802e-01,  ...,  5.0439e-01,
          1.2536e+00,  1.3501e+00],
        [ 9.1765e-01,  6.7741e-01,  1.0928e+00,  ...,  8.1460e-01,
          9.7924e-01,  3.8059e-01]])

note: b should be of shape (64,1) not (64,) that’s what RuntimeError: The size of tensor a (20) must match the size of tensor b (64) at non-singleton dimension 1 means.

1 Like