broadcasting Theano vs. Numpy

本文详细解释了Numpy中的广播机制,这是一种允许不同形状数组进行算术运算的特性。通过具体例子展示了如何将较小的数组扩展到与较大数组兼容的形状,以便进行加法、乘法等操作。同时对比了Theano和Numpy在广播机制上的区别。
摘要由CSDN通过智能技术生成

broadcasting Theano vs. Numpy

broadcast mechanism allows a scalar may be added to a matrix, a vector to a matrix or a scalar to a vecotor.

Examples

../../_images/bcast.png

T and F stands for True and False respectively, denoting which dimension can be broadcasted.

Diference

  1. numpy broadcast dynamically;
  2. theano needs to knows, for any operations which supports broadcasting, which dimensions will need to be broadcasted.

Numpy Broadcasting

broadcasting describe how numpy treats arrays with difference shapes during arithmetic operations:

the smaller array is broadcast across the larger array so that they have compatible shapes

Simple Case

in this case, the two arrays must have exactly the same shape:

a=np.array([1.0,2.0,3.0])
b=np.array([2.0,2.0,2.0])
print a*b
>>> array([2., 4., 6.])

numpy broadcast mechanism relaxes this constraint when the arrays' shape meet certain constraints:

  1. they are equal, or
  2. one of them is 1
a=np.array([1.0,2.0,3.0])
b=2
print a*b
>>> array([2., 4., 6.])

rules

Image  (3d array): 256 x 256 x 3
Scale  (1d array):             3
Result (3d array): 256 x 256 x 3
A      (4d array):  8 x 1 x 6 x 1
B      (3d array):      7 x 1 x 5
Result (4d array):  8 x 7 x 6 x 5

more examples

A      (2d array):  5 x 4
B      (1d array):      1
Result (2d array):  5 x 4

A      (2d array):  5 x 4
B      (1d array):      4
Result (2d array):  5 x 4

A      (3d array):  15 x 3 x 5
B      (3d array):  15 x 1 x 5
Result (3d array):  15 x 3 x 5

A      (3d array):  15 x 3 x 5
B      (2d array):       3 x 5
Result (3d array):  15 x 3 x 5

A      (3d array):  15 x 3 x 5
B      (2d array):       3 x 1
Result (3d array):  15 x 3 x 5

转载于:https://www.cnblogs.com/ZJUT-jiangnan/p/6186078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值