YouTip LogoYouTip

Pytorch Torch Mv

# PyTorch torch.mv Function * * PyTorch torch Reference Manual](#) `torch.mv` is a PyTorch function for performing matrix-vector multiplication. It calculates the product of a matrix and a vector. ### Function Definition torch.mv(input, vec, out=None) **Parameters**: * `input` (Tensor): Input matrix with shape (n, m). * `vec` (Tensor): Input vector with shape (m,) or (m, 1). * `out` (Tensor, optional): Output tensor. **Returns**: * `torch.Tensor`: Returns the matrix-vector product with shape (n,). * * * ## Usage Examples ## Example import torch # Create matrix and vector mat = torch.randn(3,4) vec = torch.randn(4) # Matrix-vector multiplication result = torch.mv(mat, vec) print("Matrix shape:", mat.shape) print("Vector shape:", vec.shape) print("Result shape:", result.shape) print("Result:", result) Output: Matrix shape: torch.Size([3, 4])Vector shape: torch.Size()Result shape: torch.Size() tensor([-0.1861, 0.2482, -0.4375]) ## Example - Neural Network Layer Calculation import torch # Simulate fully connected layer W = torch.randn(512,256) # Weight matrix x = torch.randn(256) # Input vector # Calculate y = W @ x y = torch.mv(W, x) print("Weight matrix shape:", W.shape) print("Input vector shape:", x.shape) print("Output vector shape:", y.shape) * * PyTorch torch Reference Manual](#)
← Pytorch Torch Nan_To_NumPytorch Torch Multinomial β†’