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

PyTorch torch.addbmm Function

PyTorch torch.addbmm Function


PyTorch torch reference manual PyTorch torch reference manual

torch.addbmm is a function in PyTorch used to add the result of batched matrix multiplication to an input matrix. It performs matrix multiplication on each pair of matrices in batch1 and batch2, then sums all the results and adds them to input.

Function definition



torch.addbmm(input, batch1, batch2, *, beta=1.0, alpha=1.0, out=None)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create the input matrix and the batches of matrices

input

=

torch.

randn

(

3

,

3

)

batch1

=

torch.

randn

(

10

,

3

,

4

)

batch2

=

torch.

randn

(

10

,

4

,

3

)

Perform addbmm

result

=

torch.

addbmm

(

input

,

batch1

,

batch2

)

print

(

"Input matrix shape:"

,

input

.

shape

)

print

(

"Batch 1 matrix shape:"

,

batch1.

shape

)

print

(

"Batch 2 matrix shape:"

,

batch2.

shape

)

print

(

"Result shape:"

,

result.

shape

)

print

(

result

)

The output is:



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

批量矩阵1形状: torch.Size([10, 3, 4])

批量矩阵2形状: torch.Size([10, 4, 3])

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

tensor([[-0.2875, -0.6518,  0.1207],

        [-0.2746,  0.5563, -0.2281],

        [ 0.3315,  0.1649,  0.2145]])


PyTorch torch reference manual PyTorch torch reference manual