Broadcasting and Element-wise Operations with PyTorch
Element-wise tensor operations for deep learning
An element-wise operation is an operation between two tensors that operates on corresponding elements within the respective tensors.元素操作是两个张量之间的操作,它对各自张量内的对应元素进行操作。
An element-wise operation operates on corresponding elements between tensors
Two elements are said to be corresponding if the two elements occupy the same position within the tensor. The position is determined by the indexes used to locate each element.两个元素如果在相同的位置上,则说这两个元素对应。
> t1 = torch.tensor([
[1,2],
[3,4]
], dtype=torch.float32)
> t2 = torch.tensor([
[9,8],
[7,6]
], dtype=torch.float32)
# Example of the first axis
> print(t1[0])
tensor([1., 2.])
# Example of the second axis
> print(t1[0][0])
tensor(1.)
> t1[0][0]
tensor(1.)
> t2[0][0]
tensor(9.)
This allows us to see that the corresponding element for the 1 in t1 is the 9 in t2.
The correspondence is defined by the indexes.
Addition is an element-wise operation
> t1 + t2
tensor([[10., 10.],
[10., 10.]])
Arithmetic operations are element-wise operations
> print(t1 + 2)
tensor([[3., 4.],
[5., 6.]])
> print(t1 - 2)
tensor([[-1., 0.],
[ 1., 2.]])
> print(t1 * 2)
tensor([[2., 4.],
[6., 8.]