NumPy 默认是元素与元素的操作
所以当我们使用常规的运算符,如 +、-、*、/。默认是元素与元素相操作(pointwise operation)。
import numpy as np
a = np.array([1.0, 2.0, 3.0])
b = np.array([2.0, 2.0, 2.0])
print(a + b)
print(a - b)
print(a * b)
print(a / b)[3. 4. 5.]
[-1. 0. 1.]
[2. 4. 6.]
[0.5 1. 1.5]
广播规则
两个向量能够进行元素级的操作需要满足以下两个条件中的一个:
- 它们所有轴上的维度相同
- 在每个轴上,如果维度不同,那么有一个维度应该为 1,或这个轴不存在
注: 关于轴的理解,如一个向量的shape为(3,256,256),那么它的轴个数为 3,第一个轴维度为3,第二个轴维度为 256,第三个轴维度为 256
比较轴的维度时,我们是从右向左看,如:
# 此向量有三个轴,从右到左的维度为 256 256 3
img = np.random.randn(3,256,256)
img .shape
(3, 256, 256)
当我们想让 img 乘以标量 2 会发生什么呢
img*2img:(3, 256, 256)
# 标量 2,会首先变为与img轴数相同的数据(1,1,1)
# 与 img 最后一个维度进行对比 img 最后一个维度为256,所以将标量最后一个维度变为256
# 比较倒数2个维度,将标量最后一个维度变为256
# 比较倒数3个维度,将标量最后一个维度变为256
# 此时标量伸展的维度与img相同,两者可以直接进行元素运算
scale:2
2->(1,1,1)->(1,1,256,)->(1,256,256)->(3,256,256)
更多例子:
# 满足所有轴维度相同
>>> x=torch.empty(5,7,3)
>>> y=torch.empty(5,7,3)
# same shapes are always broadcastable (i.e. the above rules always hold)>>> x=torch.empty((0,))
>>> y=torch.empty(2,2)
# x and y are not broadcastable, because x does not have at least 1 dimension# can line up trailing dimensions
>>> x=torch.empty(5,3,4,1)
>>> y=torch.empty( 3,1,1)
# x and y are broadcastable.
# 1st trailing dimension: both have size 1
# 2nd trailing dimension: y has size 1
# 3rd trailing dimension: x size == y size
# 4th trailing dimension: y dimension doesn't exist# but:
>>> x=torch.empty(5,2,4,1)
>>> y=torch.empty( 3,1,1)
# x and y are not broadcastable, because in the 3rd trailing dimension 2 != 3
参考:https://numpy.org/doc/stable/user/basics.broadcasting.html
https://pytorch.org/docs/stable/notes/broadcasting.html