Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do product of matrices in PyTorch

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 matrix multiplication can be conducted in PyTorch?

like image 700
blckbird Avatar asked Jun 13 '17 14:06

blckbird


People also ask

How do you multiply matrices in PyTorch?

Define two or more PyTorch tensors and print them. If you want to multiply a scalar quantity, define it. Multiply two or more tensors using torch. mul() and assign the value to a new variable.

What is BMM in PyTorch?

PyTorch bmm is used for matrix multiplication in batches where the scenario involves that the matrices to be multiplied have the size of 3 dimensions that is x, y, and z and the dimension of the first dimension for matrices to be multiplied should be the same.

What is matrix vector product?

Matrix-vector product If we let Ax=b, then b is an m×1 column vector. In other words, the number of rows in A (which can be anything) determines the number of rows in the product b. The general formula for a matrix-vector product is Ax=[a11a12…


2 Answers

If you want to do a matrix (rank 2 tensor) multiplication you can do it in four equivalent ways:

AB = A.mm(B) # computes A.B (matrix multiplication) # or AB = torch.mm(A, B) # or AB = torch.matmul(A, B) # or, even simpler AB = A @ B # Python 3.5+ 

There are a few subtleties. From the PyTorch documentation:

torch.mm does not broadcast. For broadcasting matrix products, see torch.matmul().

For instance, you cannot multiply two 1-dimensional vectors with torch.mm, nor multiply batched matrices (rank 3). To this end, you should use the more versatile torch.matmul. For an extensive list of the broadcasting behaviours of torch.matmul, see the documentation.

For element-wise multiplication, you can simply do (if A and B have the same shape)

A * B # element-wise matrix multiplication (Hadamard product) 
like image 41
BiBi Avatar answered Oct 11 '22 18:10

BiBi


You're looking for

torch.mm(a,b) 

Note that torch.dot() behaves differently to np.dot(). There's been some discussion about what would be desirable here. Specifically, torch.dot() treats both a and b as 1D vectors (irrespective of their original shape) and computes their inner product. The error is thrown, because this behaviour makes your a a vector of length 6 and your b a vector of length 2; hence their inner product can't be computed. For matrix multiplication in PyTorch, use torch.mm(). Numpy's np.dot() in contrast is more flexible; it computes the inner product for 1D arrays and performs matrix multiplication for 2D arrays.

By popular demand, the function torch.matmul performs matrix multiplications if both arguments are 2D and computes their dot product if both arguments are 1D. For inputs of such dimensions, its behaviour is the same as np.dot. It also lets you do broadcasting or matrix x matrix, matrix x vector and vector x vector operations in batches. For more info, see its docs.

# 1D inputs, same as torch.dot a = torch.rand(n) b = torch.rand(n) torch.matmul(a, b) # torch.Size([])  # 2D inputs, same as torch.mm a = torch.rand(m, k) b = torch.rand(k, j) torch.matmul(a, b) # torch.Size([m, j]) 
like image 90
mbpaulus Avatar answered Oct 11 '22 16:10

mbpaulus