numpy的广播原理及示例演示

欢迎访问我的个人主页
研读faster rcnn的源码时发现numpy的广播非常重要,故总结一波
以数组A和数组B的相加为例,先牢记两点原则:
核心:广播时先使A.shape=B.shape,然后对应位置进行相加
返回结果的shape是:A.shape和B.shape对应位置的最大值,比如:A.shape=(1,9,4),B.shape=(15,1,4),那么A+B的shape是(14,9,4)
有两种情况能够进行广播

  1. A.ndim 大于 B.ndim, 并且A.shape最后几个元素包含B.shape, 比如:A.shape=(2,3,4,5),B.shape=(3,4,5)或者B.shape=(4,5)或者B.shape=(5).
  2. A.ndim 等于 B.ndim, 并且A.shape和B.shape对应位置的元素要么相同要么其中一个是1,比如:A.shape=(1,9,4),B.shape=(15,1,4), 再比如A.shape=(1,9,4),B.shape=(15,1,1)

下面分别进行举例

A.ndim 大于 B.ndim

# a.shape=(2,2,3,4)
a = np.arange(1,49).reshape((2,2,3,4))
# b.shape=(3,4)
b = np.arange(1,13).reshape((3,4))
# numpy会将b.shape调整至(2,2,3,4), 这一步相当于numpy自动实现np.tile(b,[2,2,1,1])
res = a + b
print('===================================')
print(a)
print(a.shape)
print('===================================')
print(b)
print(b.shape)
print('===================================')
print(res)
print(res.shape)
print('===================================')
print(a+b == a + np.tile(b,[2,2,1,1]) )

A.ndim 等于 B.ndim

#示例1
# a.shape=(4,3)
a = np.arange(12).reshape(4,3)
# b.shape=(4,1)
b = np.arange(4).reshape(4,1)
# numpy会将b.shape调整至(4,3), 这一步相当于numpy自动实现np.tile(b,[1,3])
res = a + b
print('===================================')
print(a)
print(a.shape)
print('===================================')
print(b)
print(b.shape)
print('===================================')
print(res)
print(res.shape)
print('===================================')
print((a+b == a + np.tile(b,[1,3])) )  # 打印结果都是True

#示例2
# a.shape=(1,9,4)
a = np.arange(1,37).reshape((1,9,4))
# b.shape=(15,1,4)
b = np.arange(1,61).reshape((15,1,4))
res = a + b
print('===================================')
# print(a)
print(a.shape)
print('===================================')
# print(b)
print(b.shape)
print('===================================')
# print(res)
print(res.shape)
print('===================================')
q = np.tile(a,[15,1,1]) + np.tile(b,[1,9,1])
print(q == res)	# 打印结果都是True

你可能感兴趣的:(Python)