Scalar scaling
Apply a constant scale factor to every element of an activation, feature tensor, or parameter tensor.
PyTorch · Function
torch.mul performs elementwise multiplication between a tensor and another tensor or number. It supports broadcasting and returns a tensor containing the products.
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.
torch.mul(input, other, *, out=None) → Tensor| Name | Type | Description |
|---|---|---|
input | Tensor | The input tensor. |
other | Tensor or Number | The tensor or number to multiply input by. |
out | Tensor, optional | Optional output tensor. |
Tensor
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.
Apply a constant scale factor to every element of an activation, feature tensor, or parameter tensor.
Combine tensors whose dimensions are broadcast-compatible, such as a batch tensor and a feature vector.
Provide a preallocated destination tensor when integrating the operation into memory-conscious tensor code.
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.
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.
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.multiplytorch.Tensor.multorch.foreach.mul_