Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.mul

torch.mul performs elementwise multiplication between a tensor and another tensor or number. It supports broadcasting and returns a tensor containing the products.

How it works

torch.mul performs element-by-element multiplication between a tensor and either another tensor or a number. Tensor operands can be broadcast to a common shape, and PyTorch applies type promotion when their types differ; the optional out argument receives the result tensor.

Syntax

torch.mul(input, other, *, out=None) → Tensor

Parameters

NameTypeDescription
inputTensorThe input tensor.
otherTensor or NumberThe tensor or number to multiply input by.
outTensor, optionalOptional output tensor.

Returns

Tensor

Key points

  • The function performs pointwise multiplication rather than matrix multiplication.
  • Tensor operands may use broadcasting, and supported numeric categories include integer, floating-point, and complex values.
  • The second operand can be a tensor or a scalar number.
  • An optional output tensor can be supplied through the keyword-only out argument.
  • A scalar number can multiply every element of a tensor.

Common mistakes

  • Broadcasting must produce a common shape; incompatible tensor shapes cannot be multiplied directly.

Example: Elementwise multiplication with broadcasting

import torch

a = torch.tensor([1., 2., 3.])
b = torch.tensor([[10.], [20.]])
result = torch.mul(b, a)
print(result)

Multiplies each tensor element and broadcasts the operands to a common shape.

When to use it

Scalar scaling

Apply a constant scale factor to every element of an activation, feature tensor, or parameter tensor.

Broadcasted feature weighting

Combine tensors whose dimensions are broadcast-compatible, such as a batch tensor and a feature vector.

Explicit output storage

Provide a preallocated destination tensor when integrating the operation into memory-conscious tensor code.

Interview questions

QWhat operation does torch.mul perform, and what types of operands does it accept?

torch.mul computes elementwise multiplication: each output element is the corresponding input element multiplied by the other operand. The second operand may be a tensor or a number, and tensor operands support broadcasting.

Follow-up: How can operands with different shapes work? Broadcasting expands compatible dimensions conceptually to a common shape before the elementwise products are computed.

QWhat are torch.mul's parameters and return value?

The function accepts input as a tensor and other as either a tensor or Number. It returns a Tensor containing the elementwise products.

Follow-up: What happens when the operands have different numeric types? PyTorch applies type promotion while computing the result.

QWhat is the purpose of the out argument?

torch.mul accepts the keyword-only optional out parameter, which is the output tensor. The documented result is written to or represented by that supplied tensor when used.

Follow-up: What shape must out have? It must be suitable for the broadcasted result shape and compatible with the operation's output.

  • torch.multiply
  • torch.Tensor.mul
  • torch.foreach.mul_

Source: torch.mul — PyTorch 2.14 documentation. Checked 2026-10-16.