Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.mv function

PyTorch torch.mv function


PyTorch torch reference manual PyTorch torch reference manual

torch.mv is a function in PyTorch used to perform matrix-vector multiplication. It computes the product of a matrix and a vector.

Function definition

torch.mv(input, vec, out=None)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create a matrix and a 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

)

The output is:

矩阵形状: torch.Size([3, 4])

向量形状: torch.Size([4])

结果形状: torch.Size([3])

tensor([-0.1861,  0.2482, -0.4375])

Example - Neural network layer computation

import

torch

Simulating a fully connected layer of a neural network

W

=

torch.

randn

(

512

,

256

)

 

Weight matrix

x

=

torch.

randn

(

256

)

       

Input vector

Compute 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 reference manual