How to do dot product of two tensors

In numpy I can do a simple matrix multiplication like this:

a = numpy.arange(2*3).reshape(3,2)
b = numpy.arange(2).reshape(2,1)
print(a)
print(b)
print(a.dot(b))

However, when I am trying this with PyTorch Tensors, this does not work:

a = torch.Tensor([[1, 2, 3], [1, 2, 3]]).view(-1, 2)
b = torch.Tensor([[2, 1]]).view(2, -1)
print(a)
print(a.size())

print(b)
print(b.size())

print(torch.dot(a, b))

This code throws the following error: RuntimeError: inconsistent tensor size at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:503

Any ideas how a simple dot product can be conducted in PyTorch?

1 Like

torch.dot() means inner product, it needs two tensor 1 D. If you want to do matrix product, you can use torch.mm(a, b)

6 Likes

when I use

std::vector<at::Tensor>  dcnv2_forward(
        at::Tensor im,
        at::Tensor weights,
        at::Tensor Offset,
        at::Tensor Mask,
        int64_t num_deformable_group,
        int64_t kernel,
        int64_t pad,
        int64_t stride,
        int64_t dilation,
        bool use_bias,
        at::Tensor bias
        )
{
....
 at::Tensor out = at::mm(weights,
                        col.reshape(col2d_reshape));
...
}

Bug report :
RuntimeError: /opt/conda/conda-bld/pytorch_1532581333611/work/torch/csrc/autograd/variable.h:127: Variable: Assertion is_variable() || !defined() failed: Tensor that was converted to Variable was not actually a Variable

I guess at::mm need Variable input , so which function should use to do multiplication´╝čat::mm or something else?

I guess you can use einsum as well. Here is an example:

a = torch.rand(2, 3)
b = torch.rand(2, 3)
torch.einsum('ij,ij->i', a, b)

I checked and the result is correct. Hope it helps.

sometimes @ operation come in handy when doing dot products.
a@b