PyTorch torch.addmv Function


Pytorch torch 参考手册PyTorch torch Reference Manual

torch.addmvIt is a function in PyTorch used to add the result of matrix-vector multiplication to the input vector. It performs matrix-vector multiplication and then adds the result to the input.

Function Definition

torch.addmv(input, mat, vec, *, beta=1.0, alpha=1.0, out=None)

Parameters:

  • input(Tensor): The input vector or matrix, added to the result.
  • mat(Tensor): Input matrix with shape (n, m).
  • vec(Tensor): Input vector with shape (m,) or (m, 1).
  • beta(float, optional): Coefficient multiplied by the input, defaults to 1.0.
  • alpha(float, optional): Coefficient multiplied by the mat @ vec result, defaults to 1.0.
  • out(Tensor, optional): Output tensor.

Return Value:

  • torch.Tensor: Returns the sum of the matrix-vector multiplication result and the input vector.

Usage Example

Example

import torch

# Create input vector, matrix, and vector
input = torch.randn(3)
mat = torch.randn(3, 4)
vec = torch.randn(4)

# Perform addmv
result = torch.addmv(input, mat, vec)

print("Input vector shape:", input.shape)
print("Matrix shape:", mat.shape)
print("Vector shape:", vec.shape)
print("Result shape:", result.shape)
print(result)

The output result is:

输入向量形状: torch.Size([3])
矩阵形状: torch.Size([3, 4])
向量形状: torch.Size([4])
结果形状: torch.Size([3])
tensor([-1.0182, -0.4901, -0.9490])

Pytorch torch 参考手册PyTorch torch Reference Manual

Other Extensions