YouTip LogoYouTip

Pytorch Torch Mul

# PyTorch torch.mul Function * * * [![Image 3: 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**: * `input` (Tensor): The first input tensor. * `other` (Tensor or float): The second input tensor or scalar. * `out` (Tensor, optional): The output tensor. **Return Value**: * `torch.Tensor`: Returns the multiplied tensor. * * * ## 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) Output result: tensor([ 4, 10, 18]) ### Example 2: Multiply by Scalar ## Example import torch a = torch.tensor([1,2,3]) # Multiply by 2 b = torch.mul(a,2) print(b) # Can also use * operator c = a * 2 print(c) Output result: tensor([2, 4, 6]) tensor([2, 4, 6]) * * * [![Image 4: Pytorch torch Reference Manual]( Pytorch torch Reference Manual](
← Pytorch Torch MultiplyPytorch Torch Movedim β†’