Matrix Vector Multiplication
In this lesson, we will look at some basic multiplication operations between matrices and vectors.
We'll cover the following
Matrix multiplication with vectors
Let us first see how we can multiply a matrix with a vector. PyTorch
provides the mv()
function for this purpose.
We can use mv()
in two ways. mv()
could be called from a tensor, or just call it from torch.mv()
. The first parameter for torch.mv()
is a matrix.
result = torch.mv(mat, vec)
result = mat.mv(vec)
The code above shows two methods to multiply a matrix with a vector. You could try it below.
Note: The order of the matrix and vector is critical, otherwise, you may trigger the runtime error. The first argument of
mv
should be the matrix, the second should be the vector. For example, if the matrix is an (n×m
) tensor, then the vector should be is a 1-D tensor of sizem
.
Get hands-on with 1400+ tech skills courses.