PyTorch torch.addmv Function
PyTorch torch reference manual
torch.addmv is a function in PyTorch used to add the result of matrix-vector multiplication to an input vector. It performs the multiplication of a matrix and a vector, then adds the result to input.
Function definition
torch.addmv(input, mat, vec, *, beta=1.0, alpha=1.0, out=None)
Parameters:
input(Tensor): the input vector or matrix, which is added to the result.mat(Tensor): the input matrix, with shape (n, m).vec(Tensor): the input vector, with shape (m,) or (m, 1).beta(float, optional): a coefficient by which input is multiplied, defaulting to 1.0.alpha(float, optional): a coefficient by which the result of mat @ vec is multiplied, defaulting to 1.0.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: returns the sum of the matrix-vector multiplication result and the input vector.
Usage examples
Example
import
torch
Create the input vector, the matrix, and the vector
input
=
torch.
randn
(
3
)
mat
=
torch.
randn
(
3
,
4
)
vec
=
torch.
randn
(
4
)
Perform addmv
result
=
torch.
addmv
(
input
,
mat
,
vec
)
(
"Input vector shape:"
,
input
.
shape
)
(
"Matrix shape:"
,
mat.
shape
)
(
"Vector shape:"
,
vec.
shape
)
(
"Result shape:"
,
result.
shape
)
(
result
)
The output is:
输入向量形状: torch.Size([3])
矩阵形状: torch.Size([3, 4])
向量形状: torch.Size([4])
结果形状: torch.Size([3])
tensor([-1.0182, -0.4901, -0.9490])