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

PyTorch torch.mul function

PyTorch torch.mul function


PyTorch torch reference manual PyTorch torch reference manual

torch.mul is a function in PyTorch used to perform element-wise multiplication. It multiplies two tensors, or a tensor and a scalar.

This is one of the most fundamental mathematical operations in deep learning.

Function definition

torch.mul(input, other, out=None)

Parameters:

Return Value:


Usage examples

Example 1: Element-wise tensor multiplication

Example

import

torch

a

=

torch.

tensor

(

[

1

,

2

,

3

]

)

b

=

torch.

tensor

(

[

4

,

5

,

6

]

)

c

=

torch.

mul

(

a

,

b

)

print

(

c

)

The output is:

tensor([ 4, 10, 18])

Example 2: Multiplication by a scalar

Example

import

torch

a

=

torch.

tensor

(

[

1

,

2

,

3

]

)

Multiply by 2

b

=

torch.

mul

(

a

,

2

)

print

(

b

)

You can also use the * operator

c

=

a *

2

print

(

c

)

The output is:

tensor([2, 4, 6])

tensor([2, 4, 6])


PyTorch torch reference manual PyTorch torch reference manual