Element-wise Mathematical Operations on Tensors
In this lesson, we will discuss element-wise mathematical operations between tensors and scalar values.
Math operation with scalar
If you have used the NumPy array before, you may already know that we can perform a math operation between a NumPy array and a scalar. PyTorch tensor supports almost the same operations. In PyTorch, we perform it in two ways:
- The operators, such as
+
,-
, and*
. - The functions, such as
add
,sub
, andmul
.
a = torch.tensor([1,2,3])
b = a + 3
b = a.add(3)
PyTorch supports most of the math functions under the native Python math
module. You could find a complete list from the official sites.
Notice: These functions wouldn’t change the original tensor, the changed tensor would be returned as a new tensor. PyTorch provides another version of these functions, which allows us to change the tensor in-place. These functions’ names remain the same, except that the function names are suffixed with an
underscore
. Such asadd_
.
Get hands-on with 1300+ tech skills courses.